lib/LineTypeParser.cc

Code
Comments
Other
Rev Date Author Line
103 27 Jun 06 peter 1 // $Id$
103 27 Jun 06 peter 2
103 27 Jun 06 peter 3 /*
978 12 Dec 09 peter 4   Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
1635 30 Mar 23 peter 5   Copyright (C) 2009, 2010, 2023 Peter Johansson
103 27 Jun 06 peter 6
687 04 Aug 08 peter 7   This file is part of svndigest, http://dev.thep.lu.se/svndigest
103 27 Jun 06 peter 8
149 12 Aug 06 jari 9   svndigest is free software; you can redistribute it and/or modify it
103 27 Jun 06 peter 10   under the terms of the GNU General Public License as published by
693 11 Sep 08 jari 11   the Free Software Foundation; either version 3 of the License, or
103 27 Jun 06 peter 12   (at your option) any later version.
103 27 Jun 06 peter 13
149 12 Aug 06 jari 14   svndigest is distributed in the hope that it will be useful, but
103 27 Jun 06 peter 15   WITHOUT ANY WARRANTY; without even the implied warranty of
149 12 Aug 06 jari 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
103 27 Jun 06 peter 17   General Public License for more details.
103 27 Jun 06 peter 18
103 27 Jun 06 peter 19   You should have received a copy of the GNU General Public License
693 11 Sep 08 jari 20   along with svndigest. If not, see <http://www.gnu.org/licenses/>.
103 27 Jun 06 peter 21 */
103 27 Jun 06 peter 22
1619 12 Mar 23 peter 23 #include <config.h>
1619 12 Mar 23 peter 24
552 08 Jan 08 peter 25 #include "LineTypeParser.h"
512 08 Dec 07 peter 26 #include "Configuration.h"
118 03 Jul 06 peter 27 #include "utility.h"
103 27 Jun 06 peter 28
1675 26 Aug 23 peter 29 #include <yat/utility/utility.h>
1675 26 Aug 23 peter 30
103 27 Jun 06 peter 31 #include <algorithm>
299 10 May 07 jari 32 #include <cassert>
103 27 Jun 06 peter 33 #include <functional>
103 27 Jun 06 peter 34 #include <fstream>
103 27 Jun 06 peter 35 #include <iostream>
118 03 Jul 06 peter 36 #include <string>
261 30 Apr 07 peter 37 #include <utility>
261 30 Apr 07 peter 38 #include <vector>
103 27 Jun 06 peter 39
103 27 Jun 06 peter 40 namespace theplu{
149 12 Aug 06 jari 41 namespace svndigest{
103 27 Jun 06 peter 42
103 27 Jun 06 peter 43
552 08 Jan 08 peter 44   LineTypeParser::LineTypeParser(std::string path)
552 08 Jan 08 peter 45     : mode_(0), post_copyright_(false), copyright_found_(false)
103 27 Jun 06 peter 46   {
1675 26 Aug 23 peter 47     codon_ = Configuration::instance().codon(yat::utility::basename(path));
536 27 Dec 07 peter 48   }
536 27 Dec 07 peter 49
536 27 Dec 07 peter 50
552 08 Jan 08 peter 51   LineTypeParser::line_type LineTypeParser::parse(std::string line)
536 27 Dec 07 peter 52   {
552 08 Jan 08 peter 53     if (!post_copyright_) {
552 08 Jan 08 peter 54       if (copyright_found_) {
552 08 Jan 08 peter 55         // check if line is end of copyright statement, i.e. contains
552 08 Jan 08 peter 56         // no alphanumerical character (except in copyright_prefix).
556 16 Jan 08 peter 57         post_copyright_ = true;
552 08 Jan 08 peter 58         for (size_t i=0; i<line.size()&&post_copyright_; ++i)
1513 23 Sep 12 peter 59           if (isalnum(line[i]) &&
552 08 Jan 08 peter 60               !(i<copyright_prefix_.size() && copyright_prefix_[i]==line[i])){
552 08 Jan 08 peter 61             post_copyright_ = false;
552 08 Jan 08 peter 62             block_ += line + "\n";
552 08 Jan 08 peter 63           }
552 08 Jan 08 peter 64         if (post_copyright_)
552 08 Jan 08 peter 65           end_line_ = type_.size()+1;
552 08 Jan 08 peter 66       }
552 08 Jan 08 peter 67       else {
552 08 Jan 08 peter 68         // check whether copyright starts on this line
1154 07 Aug 10 peter 69         std::string::iterator i =
1154 07 Aug 10 peter 70           search(line.begin(), line.end(),
1154 07 Aug 10 peter 71                  Configuration::instance().copyright_string());
552 08 Jan 08 peter 72         if (i!=line.end()) {
552 08 Jan 08 peter 73           start_line_ = type_.size()+1;
552 08 Jan 08 peter 74           copyright_prefix_ = line.substr(0, distance(line.begin(), i));
552 08 Jan 08 peter 75           copyright_found_ = true;
552 08 Jan 08 peter 76           block_ = line+"\n";
552 08 Jan 08 peter 77         }
552 08 Jan 08 peter 78       }
552 08 Jan 08 peter 79     }
552 08 Jan 08 peter 80     // we are in copyright block
552 08 Jan 08 peter 81     if (copyright_found_ && !post_copyright_)
552 08 Jan 08 peter 82       type_.push_back(LineTypeParser::copyright);
552 08 Jan 08 peter 83
552 08 Jan 08 peter 84     else if (codon_)
536 27 Dec 07 peter 85       type_.push_back(code_mode(line));
261 30 Apr 07 peter 86     else
536 27 Dec 07 peter 87       type_.push_back(text_mode(line));
536 27 Dec 07 peter 88     return type_.back();
1513 23 Sep 12 peter 89
234 09 Apr 07 peter 90   }
234 09 Apr 07 peter 91
234 09 Apr 07 peter 92
552 08 Jan 08 peter 93   LineTypeParser::line_type LineTypeParser::code_mode(const std::string& str)
260 30 Apr 07 peter 94   {
260 30 Apr 07 peter 95     // mode zero means we are currently not in a comment
260 30 Apr 07 peter 96     // if mode!=0 comment is closed by codon[mode-1].second -> mode=0
1513 23 Sep 12 peter 97     // if codon[x-1].start is found and x >= mode -> mode=x
536 27 Dec 07 peter 98     line_type lt=other;
536 27 Dec 07 peter 99     {
536 27 Dec 07 peter 100       for (std::string::const_iterator iter=str.begin();iter!=str.end();++iter){
536 27 Dec 07 peter 101         for (size_t i=mode_; i<codon_->size(); ++i) {
1513 23 Sep 12 peter 102           if ( iter==str.begin() && (*codon_)[i].first[0] == '\n' &&
536 27 Dec 07 peter 103                match_begin(iter, str.end(), (*codon_)[i].first.substr(1)) ) {
536 27 Dec 07 peter 104             iter += (*codon_)[i].first.size()-1;
536 27 Dec 07 peter 105             mode_ = i+1;
514 09 Dec 07 peter 106             break;
514 09 Dec 07 peter 107           }
536 27 Dec 07 peter 108           if (match_begin(iter, str.end(), (*codon_)[i].first)) {
536 27 Dec 07 peter 109             iter += (*codon_)[i].first.size();
536 27 Dec 07 peter 110             mode_ = i+1;
260 30 Apr 07 peter 111             break;
260 30 Apr 07 peter 112           }
260 30 Apr 07 peter 113         }
506 08 Dec 07 peter 114         if (iter==str.end())
506 08 Dec 07 peter 115           break;
536 27 Dec 07 peter 116         assert(mode_==0 || mode_-1<(*codon_).size());
536 27 Dec 07 peter 117         if (mode_ && match_begin(iter,str.end(), (*codon_)[mode_-1].second)){
536 27 Dec 07 peter 118           iter += (*codon_)[mode_-1].second.size();
536 27 Dec 07 peter 119           mode_=0;
506 08 Dec 07 peter 120           if (iter==str.end())
506 08 Dec 07 peter 121             break;
260 30 Apr 07 peter 122         }
536 27 Dec 07 peter 123         else if (!mode_ && isgraph(*iter))
505 06 Dec 07 peter 124           lt=code;
536 27 Dec 07 peter 125         else if (mode_ && lt!=code && isalnum(*iter))
505 06 Dec 07 peter 126           lt=comment;
260 30 Apr 07 peter 127       }
536 27 Dec 07 peter 128       if (mode_ && (*codon_)[mode_-1].second==std::string("\n"))
536 27 Dec 07 peter 129         mode_=0;
260 30 Apr 07 peter 130     }
536 27 Dec 07 peter 131     return lt;
260 30 Apr 07 peter 132   }
260 30 Apr 07 peter 133
260 30 Apr 07 peter 134
552 08 Jan 08 peter 135   LineTypeParser::line_type LineTypeParser::text_mode(const std::string& str)
218 01 Oct 06 peter 136   {
536 27 Dec 07 peter 137     for (std::string::const_iterator iter=str.begin(); iter!=str.end(); ++iter)
536 27 Dec 07 peter 138       if (isalnum(*iter))
536 27 Dec 07 peter 139         return comment;
536 27 Dec 07 peter 140     return other;
218 01 Oct 06 peter 141   }
218 01 Oct 06 peter 142
218 01 Oct 06 peter 143
149 12 Aug 06 jari 144 }} // end of namespace svndigest and namespace theplu