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