1 | // $Id: Configuration.cc 1156 2010-08-09 04:18:30Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2007, 2008, 2009, 2010 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
7 | |
---|
8 | svndigest is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 3 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | svndigest is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <config.h> |
---|
23 | |
---|
24 | #include "Configuration.h" |
---|
25 | |
---|
26 | #include "Colors.h" |
---|
27 | #include "Functor.h" |
---|
28 | |
---|
29 | #include "yat/split.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | #include <cassert> |
---|
33 | #include <cstdlib> |
---|
34 | #include <fstream> |
---|
35 | #include <map> |
---|
36 | #include <string> |
---|
37 | #include <sstream> |
---|
38 | #include <stdexcept> |
---|
39 | #include <utility> |
---|
40 | |
---|
41 | namespace theplu{ |
---|
42 | namespace svndigest{ |
---|
43 | |
---|
44 | Configuration* Configuration::instance_=NULL; |
---|
45 | |
---|
46 | |
---|
47 | Configuration::Configuration(void) |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | void Configuration::add_codon(std::string key, std::string start, |
---|
53 | std::string end) |
---|
54 | { |
---|
55 | std::pair<std::string, std::string> p(start,end); |
---|
56 | String2Codons::iterator iter = string2codons_.end(); |
---|
57 | for (String2Codons::iterator i=string2codons_.begin(); |
---|
58 | i!=string2codons_.end(); ++i) |
---|
59 | if (i->first == key) |
---|
60 | iter = i; |
---|
61 | |
---|
62 | if (iter==string2codons_.end()) |
---|
63 | string2codons_.push_back(std::make_pair(key, VectorPair(1,p))); |
---|
64 | else |
---|
65 | iter->second.push_back(p); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | const std::map<std::string, std::string>& |
---|
70 | Configuration::author_colors(void) const |
---|
71 | { |
---|
72 | return author_color_; |
---|
73 | } |
---|
74 | |
---|
75 | std::string Configuration::author_str_color(const std::string& author) const |
---|
76 | { |
---|
77 | std::string res; |
---|
78 | std::map<std::string, std::string>::const_iterator iterator; |
---|
79 | if ( (iterator=author_color_.find(author)) != author_color_.end()) |
---|
80 | res = iterator->second; |
---|
81 | return res; |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | const std::vector<std::pair<std::string, std::string> >* |
---|
86 | Configuration::codon(std::string file_name) const |
---|
87 | { |
---|
88 | if (const std::pair<std::string,std::string>* dict=dictionary(file_name)) |
---|
89 | file_name = translate(file_name, *dict); |
---|
90 | for (String2Codons::const_iterator i(string2codons_.begin()); |
---|
91 | i!=string2codons_.end(); ++i) { |
---|
92 | if (fnmatch(i->first.c_str(), file_name.c_str())) |
---|
93 | return &i->second; |
---|
94 | } |
---|
95 | return NULL; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | const std::map<std::string,Alias>& Configuration::copyright_alias(void) const |
---|
100 | { |
---|
101 | return copyright_alias_; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | const std::string& Configuration::copyright_string(void) const |
---|
106 | { |
---|
107 | return copyright_string_; |
---|
108 | } |
---|
109 | |
---|
110 | const std::pair<std::string,std::string>* |
---|
111 | Configuration::dictionary(std::string lhs) const |
---|
112 | { |
---|
113 | for (size_t i=0; i<dictionary_.size(); ++i) |
---|
114 | if (fnmatch(lhs.c_str(), dictionary_[i].first.c_str())) |
---|
115 | return &dictionary_[i]; |
---|
116 | return NULL; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | bool Configuration::equal_false(std::string str) const |
---|
121 | { |
---|
122 | transform(str.begin(), str.end(), str.begin(), tolower); |
---|
123 | return str=="false" || str=="no" || str=="off" || str=="0"; |
---|
124 | } |
---|
125 | |
---|
126 | |
---|
127 | bool Configuration::equal_true(std::string str) const |
---|
128 | { |
---|
129 | transform(str.begin(), str.end(), str.begin(), tolower); |
---|
130 | return str=="true" || str=="yes" || str=="on" || str=="1"; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | const std::string& Configuration::image_anchor_format(void) const |
---|
135 | { |
---|
136 | return image_anchor_format_; |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | const std::string& Configuration::image_format(void) const |
---|
141 | { |
---|
142 | return image_format_; |
---|
143 | } |
---|
144 | |
---|
145 | |
---|
146 | void Configuration::load(void) |
---|
147 | { |
---|
148 | set_default(); |
---|
149 | validate_dictionary(); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | void Configuration::load(std::istream& is) |
---|
154 | { |
---|
155 | assert(is.good()); |
---|
156 | |
---|
157 | bool parsing_found=false; |
---|
158 | bool dictionary_found=false; |
---|
159 | std::string line; |
---|
160 | std::string section; |
---|
161 | std::string tmp; |
---|
162 | while (getline(is, line)) { |
---|
163 | line = ltrim(line); |
---|
164 | if (line.empty() || line[0]=='#') |
---|
165 | continue; |
---|
166 | std::stringstream ss(line); |
---|
167 | if (line[0] == '[') { |
---|
168 | getline(ss, tmp, '['); |
---|
169 | getline(ss, section, ']'); |
---|
170 | continue; |
---|
171 | } |
---|
172 | std::string lhs; |
---|
173 | getline(ss, lhs, '='); |
---|
174 | lhs = trim(lhs); |
---|
175 | std::string rhs; |
---|
176 | getline(ss, rhs); |
---|
177 | rhs = trim(rhs); |
---|
178 | if (rhs.empty()){ |
---|
179 | throw Config_error(line, "expected format: <lhs> = <rhs>"); |
---|
180 | } |
---|
181 | if (section == "copyright-alias"){ |
---|
182 | std::map<std::string,Alias>::iterator iter = |
---|
183 | copyright_alias_.lower_bound(lhs); |
---|
184 | if (iter!=copyright_alias_.end() && iter->first==lhs){ |
---|
185 | std::stringstream mess; |
---|
186 | mess << "in copright-alias section " << lhs << " defined twice."; |
---|
187 | throw Config_error(line, mess.str()); |
---|
188 | } |
---|
189 | |
---|
190 | // insert alias |
---|
191 | copyright_alias_.insert(iter,std::make_pair(lhs, Alias(rhs,copyright_alias_.size()))); |
---|
192 | } |
---|
193 | else if (section == "trac"){ |
---|
194 | if (lhs=="trac-root") |
---|
195 | trac_root_=rhs; |
---|
196 | else { |
---|
197 | std::stringstream mess; |
---|
198 | mess << "in trac section" << lhs + " is invalid option."; |
---|
199 | throw Config_error(line, mess.str()); |
---|
200 | } |
---|
201 | } |
---|
202 | else if (section == "output") { |
---|
203 | if (lhs=="blame-information") { |
---|
204 | if (equal_false(rhs)) |
---|
205 | output_blame_information_ = false; |
---|
206 | else if (equal_true(rhs)) |
---|
207 | output_blame_information_ = true; |
---|
208 | else { |
---|
209 | throw Config_error(line, ""); |
---|
210 | } |
---|
211 | } |
---|
212 | else if (lhs=="file") { |
---|
213 | if (equal_false(rhs)) |
---|
214 | output_file_ = false; |
---|
215 | else if (equal_true(rhs)) |
---|
216 | output_file_ = true; |
---|
217 | else { |
---|
218 | throw Config_error(line, ""); |
---|
219 | } |
---|
220 | } |
---|
221 | else if (lhs=="tab-size") { |
---|
222 | tab_size_ = strtoul(rhs.c_str(), NULL, 10); |
---|
223 | if (tab_size_==0) { |
---|
224 | throw Config_error(line, |
---|
225 | "invalid value: tab-size must be a positive integer"); |
---|
226 | } |
---|
227 | else if (tab_size_ == ULONG_MAX) { |
---|
228 | throw Config_error(line, "invalid value: out of range"); |
---|
229 | } |
---|
230 | } |
---|
231 | } |
---|
232 | else if (section == "copyright") { |
---|
233 | if (lhs=="missing-copyright-warning") { |
---|
234 | if (equal_false(rhs)) |
---|
235 | missing_copyright_warning_ = false; |
---|
236 | else if (equal_true(rhs)) |
---|
237 | missing_copyright_warning_ = true; |
---|
238 | else { |
---|
239 | throw Config_error(line, ""); |
---|
240 | } |
---|
241 | } |
---|
242 | else if (lhs=="copyright-string") { |
---|
243 | copyright_string_ = rhs; |
---|
244 | } |
---|
245 | } |
---|
246 | else if (section == "author-color") { |
---|
247 | unsigned char r,g,b; |
---|
248 | try { |
---|
249 | str2rgb(rhs, r,g,b); |
---|
250 | } |
---|
251 | catch (std::runtime_error& e) { |
---|
252 | throw Config_error(line, e.what()); |
---|
253 | } |
---|
254 | author_color_[lhs] = rhs; |
---|
255 | } |
---|
256 | else if (section == "parsing-codons") { |
---|
257 | if (!parsing_found) { |
---|
258 | parsing_found=true; |
---|
259 | // clearing the default setting |
---|
260 | string2codons_.clear(); |
---|
261 | } |
---|
262 | |
---|
263 | if (codon(lhs)) { |
---|
264 | std::stringstream mess; |
---|
265 | mess << "clashes with previous given file name pattern: "; |
---|
266 | // find previous file-name-pattern |
---|
267 | for (String2Codons::const_iterator i(string2codons_.begin()); |
---|
268 | i!=string2codons_.end(); ++i) { |
---|
269 | if (fnmatch(lhs.c_str(), i->first.c_str())) { |
---|
270 | mess << "`" << i->first << "'"; |
---|
271 | break; |
---|
272 | } |
---|
273 | } |
---|
274 | throw Config_error(line, mess.str()); |
---|
275 | } |
---|
276 | std::stringstream ss(rhs); |
---|
277 | std::string start; |
---|
278 | while (getline(ss, start, ':')) { |
---|
279 | start = trim(start); |
---|
280 | std::string end; |
---|
281 | getline(ss, end, ';'); |
---|
282 | end = trim(end); |
---|
283 | if (start.empty() && end.empty()) |
---|
284 | continue; |
---|
285 | try { |
---|
286 | if (start.empty() || start=="\"\"") { |
---|
287 | throw std::runtime_error("start-code is empty"); |
---|
288 | } |
---|
289 | else if (start.size()<3) { |
---|
290 | std::stringstream mess; |
---|
291 | mess << "start-code `" << start << "' is invalid"; |
---|
292 | throw std::runtime_error(mess.str()); |
---|
293 | } |
---|
294 | start = trim(start, '"'); |
---|
295 | if (end.empty() || end=="\"\"") { |
---|
296 | throw std::runtime_error("end-code is empty"); |
---|
297 | } |
---|
298 | else if (end.size()<3) { |
---|
299 | std::stringstream mess; |
---|
300 | mess << "end-code `" << end << "' is invalid"; |
---|
301 | throw std::runtime_error(mess.str()); |
---|
302 | } |
---|
303 | end = trim(end, '"'); |
---|
304 | } |
---|
305 | catch (std::runtime_error& e){ |
---|
306 | throw Config_error(line, e.what()); |
---|
307 | } |
---|
308 | replace(start, "\\n", "\n"); |
---|
309 | replace(end, "\\n", "\n"); |
---|
310 | add_codon(lhs, start, end); |
---|
311 | } |
---|
312 | } |
---|
313 | else if (section == "file-name-dictionary") { |
---|
314 | if (!dictionary_found) { |
---|
315 | dictionary_found=true; |
---|
316 | // clearing the default setting |
---|
317 | dictionary_.clear(); |
---|
318 | } |
---|
319 | |
---|
320 | if (const std::pair<std::string, std::string>* entry=dictionary(lhs)) { |
---|
321 | std::stringstream mess; |
---|
322 | mess << "clashes with previous given file name pattern: " |
---|
323 | << "`" << entry->first << "'"; |
---|
324 | throw Config_error(line, mess.str()); |
---|
325 | } |
---|
326 | lhs = trim(lhs); |
---|
327 | rhs = trim(rhs); |
---|
328 | if (!lhs.empty() && !rhs.empty()) |
---|
329 | dictionary_.push_back(std::make_pair(lhs, rhs)); |
---|
330 | else if (!lhs.empty() || !rhs.empty()) { |
---|
331 | throw Config_error(line, ""); |
---|
332 | } |
---|
333 | } |
---|
334 | else if (section == "svn_props") { |
---|
335 | svn_props_.push_back(std::make_pair(lhs, empty_str_map_)); |
---|
336 | std::vector<std::string> vec; |
---|
337 | yat::utility::split(vec, rhs, ';'); |
---|
338 | for (size_t i=0; i<vec.size(); ++i) { |
---|
339 | std::vector<std::string> vec2; |
---|
340 | yat::utility::split(vec2, vec[i], '='); |
---|
341 | std::string key = trim(vec2[0]); |
---|
342 | std::string value(""); |
---|
343 | if (vec2.size() >= 2) |
---|
344 | value = trim(vec2[1]); |
---|
345 | svn_props_.back().second[key] = value; |
---|
346 | } |
---|
347 | } |
---|
348 | else if (section == "image") { |
---|
349 | if (lhs == "format") { |
---|
350 | try { |
---|
351 | image_format(rhs); |
---|
352 | } |
---|
353 | catch (std::runtime_error e) { |
---|
354 | throw Config_error(line, |
---|
355 | "unknown format: " + rhs + "\n" + e.what()); |
---|
356 | } |
---|
357 | } |
---|
358 | else if (lhs == "image_format") { |
---|
359 | try { |
---|
360 | image_anchor_format(rhs); |
---|
361 | } |
---|
362 | catch (std::runtime_error e) { |
---|
363 | throw Config_error(line, |
---|
364 | "unknown format: " + rhs + "\n" + e.what()); |
---|
365 | } |
---|
366 | } |
---|
367 | } |
---|
368 | } |
---|
369 | validate_dictionary(); |
---|
370 | } |
---|
371 | |
---|
372 | |
---|
373 | void Configuration::image_anchor_format(const std::string& format) |
---|
374 | { |
---|
375 | if (format!="none" && format!="pdf" && format!="png" && format!="svg") { |
---|
376 | std::ostringstream oss; |
---|
377 | oss << "Valid arguments are:\n" |
---|
378 | << " - `none'\n" |
---|
379 | << " - `pdf'\n" |
---|
380 | << " - `png'\n" |
---|
381 | << " - `svg'"; |
---|
382 | throw std::runtime_error(oss.str()); |
---|
383 | } |
---|
384 | image_anchor_format_ = format; |
---|
385 | } |
---|
386 | |
---|
387 | |
---|
388 | void Configuration::image_format(const std::string& format) |
---|
389 | { |
---|
390 | if (format!="none" && format!="png" && format!="svg") { |
---|
391 | std::ostringstream oss; |
---|
392 | oss << "Valid arguments are:\n" |
---|
393 | << " - `none'\n" |
---|
394 | << " - `png'\n" |
---|
395 | << " - `svg'"; |
---|
396 | throw std::runtime_error(oss.str()); |
---|
397 | } |
---|
398 | image_format_ = format; |
---|
399 | } |
---|
400 | |
---|
401 | |
---|
402 | Configuration& Configuration::instance(void) |
---|
403 | { |
---|
404 | if (!instance_){ |
---|
405 | instance_ = new Configuration; |
---|
406 | instance_->load(); |
---|
407 | std::cerr << "construct config\n"; |
---|
408 | } |
---|
409 | return *instance_; |
---|
410 | } |
---|
411 | |
---|
412 | |
---|
413 | bool Configuration::missing_copyright_warning(void) const |
---|
414 | { |
---|
415 | return missing_copyright_warning_; |
---|
416 | } |
---|
417 | |
---|
418 | |
---|
419 | std::string |
---|
420 | Configuration::translate(const std::string& str, |
---|
421 | const std::pair<std::string, std::string>& dic) const |
---|
422 | { |
---|
423 | std::string res; |
---|
424 | std::vector<std::string> vec; |
---|
425 | if (!regexp(dic.first, str, vec)) { |
---|
426 | std::stringstream mess; |
---|
427 | mess << "invalid config file: " |
---|
428 | << "expression " << dic.first << " is invalid"; |
---|
429 | throw std::runtime_error(mess.str()); |
---|
430 | } |
---|
431 | for (std::string::const_iterator i(dic.second.begin()); |
---|
432 | i!=dic.second.end(); ++i) { |
---|
433 | if (*i == '$') { |
---|
434 | std::stringstream ss(std::string(i+1, dic.second.end())); |
---|
435 | size_t n = 0; |
---|
436 | ss >> n; |
---|
437 | if (n>vec.size() || n==0){ |
---|
438 | std::stringstream mess; |
---|
439 | mess << "invalid config file: " |
---|
440 | << "expression " << dic.second << " is invalid"; |
---|
441 | if (n) |
---|
442 | mess << "because " << n << " is a too large."; |
---|
443 | throw std::runtime_error(mess.str()); |
---|
444 | } |
---|
445 | res += vec[n-1]; |
---|
446 | ++i; |
---|
447 | if (n>9){ |
---|
448 | ++i; |
---|
449 | if (n>99) |
---|
450 | ++i; |
---|
451 | |
---|
452 | } |
---|
453 | } |
---|
454 | else |
---|
455 | res += *i; |
---|
456 | } |
---|
457 | |
---|
458 | return res; |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | std::string trans_end_code(std::string str) |
---|
463 | { |
---|
464 | if (str.size()>0 && str[str.size()-1]=='\n') |
---|
465 | return str.substr(0, str.size()-1) + std::string("\\n"); |
---|
466 | return str; |
---|
467 | } |
---|
468 | |
---|
469 | |
---|
470 | std::string trans_beg_code(std::string str) |
---|
471 | { |
---|
472 | if (str.size()>0 && str[0]=='\n') |
---|
473 | return std::string("\\n") + str.substr(1); |
---|
474 | return str; |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | std::string trim(std::string str, char c) |
---|
479 | { |
---|
480 | if (str.size()<2 || str[0]!=c || str[str.size()-1]!=c){ |
---|
481 | std::stringstream mess; |
---|
482 | mess << "expected `" << str << "' to be surrounded by `" << c << "'"; |
---|
483 | throw std::runtime_error(mess.str()); |
---|
484 | } |
---|
485 | return str.substr(1, str.size()-2); |
---|
486 | } |
---|
487 | |
---|
488 | |
---|
489 | void Configuration::set_default(void) |
---|
490 | { |
---|
491 | copyright_alias_.clear(); |
---|
492 | copyright_string_="Copyright (C)"; |
---|
493 | missing_copyright_warning_=false; |
---|
494 | trac_root_ = ""; |
---|
495 | tab_size_ = 2; |
---|
496 | |
---|
497 | add_codon("*.ac", "#", "\n"); |
---|
498 | add_codon("*.ac", "dnl", "\n"); |
---|
499 | add_codon("*.am", "#", "\n"); |
---|
500 | add_codon("*.as", "#", "\n"); |
---|
501 | add_codon("*.as", "dnl", "\n"); |
---|
502 | add_codon("*.bat", "\nREM", "\n"); |
---|
503 | add_codon("*.bat", "\nrem", "\n"); |
---|
504 | add_codon("*.c", "//", "\n"); |
---|
505 | add_codon("*.c", "/*", "*/"); |
---|
506 | add_codon("*.cc", "//", "\n"); |
---|
507 | add_codon("*.cc", "/*", "*/"); |
---|
508 | add_codon("*.cpp", "//", "\n"); |
---|
509 | add_codon("*.cpp", "/*", "*/"); |
---|
510 | add_codon("*.css", "<!--", "-->"); |
---|
511 | add_codon("*.cxx", "//", "\n"); |
---|
512 | add_codon("*.cxx", "/*", "*/"); |
---|
513 | add_codon("*.h", "//", "\n"); |
---|
514 | add_codon("*.h", "/*", "*/"); |
---|
515 | add_codon("*.hh", "//", "\n"); |
---|
516 | add_codon("*.hh", "/*", "*/"); |
---|
517 | add_codon("*.hpp", "//", "\n"); |
---|
518 | add_codon("*.hpp", "/*", "*/"); |
---|
519 | add_codon("*.html", "<%--", "--%>"); |
---|
520 | add_codon("*.java", "//", "\n"); |
---|
521 | add_codon("*.java", "/*", "*/"); |
---|
522 | add_codon("*.jsp", "<!--", "-->"); |
---|
523 | add_codon("*.m", "%", "\n"); |
---|
524 | add_codon("*.m4", "#", "\n"); |
---|
525 | add_codon("*.m4", "dnl", "\n"); |
---|
526 | add_codon("*.pl", "#", "\n"); |
---|
527 | add_codon("*.pm", "#", "\n"); |
---|
528 | add_codon("*.R", "#", "\n"); |
---|
529 | add_codon("*.rss", "<!--", "-->"); |
---|
530 | add_codon("*.sgml", "<!--", "-->"); |
---|
531 | add_codon("*.sh", "#", "\n"); |
---|
532 | add_codon("*.shtml", "<!--", "-->"); |
---|
533 | add_codon("*.tex", "%", "\n"); |
---|
534 | add_codon("*.xhtml", "<!--", "-->"); |
---|
535 | add_codon("*.xml", "<!--", "-->"); |
---|
536 | add_codon("*.xsd", "<!--", "-->"); |
---|
537 | add_codon("*.xsl", "<!--", "-->"); |
---|
538 | add_codon("*config", "#", "\n"); |
---|
539 | add_codon("bootstrap", "#", "\n"); |
---|
540 | add_codon("Makefile", "#", "\n"); |
---|
541 | |
---|
542 | dictionary_ = VectorPair(1, std::make_pair("*.in", "$1")); |
---|
543 | image_format_ = "png"; |
---|
544 | image_anchor_format_ = "png"; |
---|
545 | output_blame_information_ = true; |
---|
546 | output_file_ = true; |
---|
547 | |
---|
548 | } |
---|
549 | |
---|
550 | |
---|
551 | bool Configuration::output_blame_information(void) const |
---|
552 | { |
---|
553 | return output_blame_information_; |
---|
554 | } |
---|
555 | |
---|
556 | |
---|
557 | bool Configuration::output_file(void) const |
---|
558 | { |
---|
559 | return output_file_; |
---|
560 | } |
---|
561 | |
---|
562 | |
---|
563 | const std::map<std::string, std::string>& |
---|
564 | Configuration::svn_properties(const std::string& filename) const |
---|
565 | { |
---|
566 | // reverse backwards as we prefer to to pick properties defined later |
---|
567 | std::vector<props>::const_reverse_iterator iter = |
---|
568 | find_fn(svn_props_.rbegin(), svn_props_.rend(), filename); |
---|
569 | if (iter==svn_props_.rend()) |
---|
570 | return empty_str_map_; |
---|
571 | return iter->second; |
---|
572 | } |
---|
573 | |
---|
574 | |
---|
575 | size_t Configuration::tab_size(void) const |
---|
576 | { |
---|
577 | return tab_size_; |
---|
578 | } |
---|
579 | |
---|
580 | |
---|
581 | std::string Configuration::trac_root(void) const |
---|
582 | { |
---|
583 | return trac_root_; |
---|
584 | } |
---|
585 | |
---|
586 | |
---|
587 | void Configuration::validate_dictionary(void) const |
---|
588 | { |
---|
589 | VectorPair::const_iterator end(dictionary_.end()); |
---|
590 | for (VectorPair::const_iterator iter(dictionary_.begin());iter!=end;++iter){ |
---|
591 | std::string word(iter->first); |
---|
592 | replace(word, "*", ""); |
---|
593 | replace(word, "?", ""); |
---|
594 | // throws if dictionary is invalid |
---|
595 | translate(word, *iter); |
---|
596 | } |
---|
597 | } |
---|
598 | |
---|
599 | |
---|
600 | std::ostream& operator<<(std::ostream& os, const Configuration& conf) |
---|
601 | { |
---|
602 | os << "### This file configures various behaviors for svndigest\n" |
---|
603 | << "### The commented-out below are intended to demonstrate how to use\n" |
---|
604 | << "### this file.\n" |
---|
605 | << "\n"; |
---|
606 | |
---|
607 | os << "### Section for setting output\n" |
---|
608 | << "[output]\n" |
---|
609 | << "# if true svndigest will output blame information for each file.\n" |
---|
610 | << "blame-information = "; |
---|
611 | if (conf.output_blame_information()) |
---|
612 | os << "yes\n"; |
---|
613 | else |
---|
614 | os << "no\n"; |
---|
615 | os << "# if true report will have pages for files and not only " |
---|
616 | << "directories.\n" |
---|
617 | << "file = "; |
---|
618 | if (conf.output_file()) |
---|
619 | os << "yes\n"; |
---|
620 | else |
---|
621 | os << "no\n"; |
---|
622 | os << "# svndigest uses this value to replace tabs " |
---|
623 | << "with spaces in blame output\n" |
---|
624 | << "tab-size = " << conf.tab_size() << "\n"; |
---|
625 | |
---|
626 | |
---|
627 | os << "\n### Section for setting behaviour of copyright update\n" |
---|
628 | << "[copyright]\n" |
---|
629 | << "# if true svndigest will warn if file has no copyright statement.\n" |
---|
630 | << "missing-copyright-warning = "; |
---|
631 | if (conf.missing_copyright_warning()) |
---|
632 | os << "yes\n"; |
---|
633 | else |
---|
634 | os << "no\n"; |
---|
635 | os << "copyright-string = " << conf.copyright_string_ << "\n"; |
---|
636 | |
---|
637 | os << "\n" |
---|
638 | << "### Section for setting aliases used in copyright update\n" |
---|
639 | << "[copyright-alias]\n" |
---|
640 | << "# jdoe = John Doe\n"; |
---|
641 | |
---|
642 | typedef std::vector<std::pair<std::string, Alias> > vector; |
---|
643 | vector vec; |
---|
644 | std::back_insert_iterator<vector> back_insert_iterator(vec); |
---|
645 | vec.reserve(conf.copyright_alias().size()); |
---|
646 | std::copy(conf.copyright_alias().begin(), conf.copyright_alias().end(), |
---|
647 | back_insert_iterator); |
---|
648 | // sort with respect to Alias.id |
---|
649 | IdCompare id; |
---|
650 | PairSecondCompare<const std::string, Alias, IdCompare> comp(id); |
---|
651 | std::sort(vec.begin(),vec.end(), comp); |
---|
652 | |
---|
653 | |
---|
654 | for (vector::const_iterator i(vec.begin()); i!=vec.end(); ++i) { |
---|
655 | os << i->first << " = " << i->second.name() << "\n"; |
---|
656 | } |
---|
657 | |
---|
658 | os << "\n" |
---|
659 | << "### Section for images\n" |
---|
660 | << "[image]\n" |
---|
661 | << "format = " << conf.image_format() << "\n"; |
---|
662 | os << "anchor_format = " << conf.image_anchor_format() << "\n"; |
---|
663 | |
---|
664 | |
---|
665 | os << "\n" |
---|
666 | << "### Section for author color in plots and blame output.\n" |
---|
667 | << "[author-color]\n" |
---|
668 | << "# jdoe = 000000\n"; |
---|
669 | typedef Configuration::str_map str_map; |
---|
670 | for (str_map::const_iterator i(conf.author_color_.begin()); |
---|
671 | i!=conf.author_color_.end(); ++i) { |
---|
672 | os << i->first << " = " << i->second << "\n"; |
---|
673 | } |
---|
674 | |
---|
675 | typedef Configuration::props props; |
---|
676 | os << "\n" |
---|
677 | << "### Section for overriding svn properties.\n" |
---|
678 | << "### The format is the same as for section auto-props in subversion\n" |
---|
679 | << "### config file\n" |
---|
680 | << "[svn-props]\n"; |
---|
681 | os << "# COPYING = svndigest:ignore\n"; |
---|
682 | std::vector<props>::const_iterator p=conf.svn_props_.begin(); |
---|
683 | for ( ; p!=conf.svn_props_.end(); ++p) { |
---|
684 | os << p->first << " = "; |
---|
685 | const str_map& map = p->second; |
---|
686 | str_map::const_iterator end = map.end(); |
---|
687 | for (str_map::const_iterator i=map.begin(); i!=end; ++i) { |
---|
688 | if (i != map.begin()) |
---|
689 | os << ";"; |
---|
690 | os << i->first << "=" << i->second; |
---|
691 | } |
---|
692 | os << "\n"; |
---|
693 | } |
---|
694 | |
---|
695 | |
---|
696 | os << "\n" |
---|
697 | << "### Section for setting trac environment.\n" |
---|
698 | << "[trac]\n" |
---|
699 | << "# If trac-root is set, svndigest will create anchors to " |
---|
700 | << "the Trac page.\n" |
---|
701 | << "# trac-root = http://dev.thep.lu.se/svndigest/\n"; |
---|
702 | if (!conf.trac_root().empty()) |
---|
703 | os << "trac-root = " << conf.trac_root() << "\n"; |
---|
704 | |
---|
705 | os << "\n" |
---|
706 | << "### Section for setting dictionary for file names.\n" |
---|
707 | << "### Prior looking for file name pattern in section " |
---|
708 | << "[parsing-codons],\n" |
---|
709 | << "### the file name may be translated according to the rules \n" |
---|
710 | << "### in this section. In default setting there is, for example,\n" |
---|
711 | << "### a rule to translate `<FILENAME>.in' to `<FILENAME>'.\n" |
---|
712 | << "### The format of the entries is:\n" |
---|
713 | << "### file-name-pattern = new-name\n" |
---|
714 | << "### Left hand side may contain wildcards (such as '*' and '?').\n" |
---|
715 | << "### Right hand side may contain \"$i\", which will be replaced \n" |
---|
716 | << "### with the ith wild card in lhs string.\n"; |
---|
717 | |
---|
718 | if (!conf.dictionary_.empty()) { |
---|
719 | os << "\n" |
---|
720 | << "### Section for setting dictionary for file names.\n" |
---|
721 | << "### Prior looking for file name pattern in section " |
---|
722 | << "[parsing-codons],\n" |
---|
723 | << "### the file name may be translated according to the rules \n" |
---|
724 | << "### in this section. In default setting there is, for example,\n" |
---|
725 | << "### a rule to translate `<FILENAME>.in' to `<FILENAME>'.\n" |
---|
726 | << "### The format of the entries is:\n" |
---|
727 | << "### file-name-pattern = new-name\n" |
---|
728 | << "### Left hand side may contain wildcards (such as '*' and '?').\n" |
---|
729 | << "### Right hand side may contain \"$i\", which will be replaced \n" |
---|
730 | << "### with the ith wild card in lhs string.\n" |
---|
731 | << "[file-name-dictionary]\n"; |
---|
732 | for (size_t i=0; i<conf.dictionary_.size(); ++i) |
---|
733 | os << conf.dictionary_[i].first << " = " |
---|
734 | << conf.dictionary_[i].second << "\n"; |
---|
735 | } |
---|
736 | if (!conf.string2codons_.empty()) { |
---|
737 | os << "\n" |
---|
738 | << "### Section for setting parsing modes\n" |
---|
739 | << "### The format of the entries is:\n" |
---|
740 | << "### file-name-pattern = \"start-code\" : \"end-code\"\n" |
---|
741 | << "### The file-name-pattern may contain wildcards (such as '*' " |
---|
742 | << "and '?').\n" |
---|
743 | << "### String \"\\n\" can be used for codons containing newline" |
---|
744 | << "\n### character.\n" |
---|
745 | << "[parsing-codons]\n"; |
---|
746 | for (size_t i=0; i<conf.string2codons_.size(); ++i) { |
---|
747 | os << conf.string2codons_[i].first << " = "; |
---|
748 | for (size_t j=0; j<conf.string2codons_[i].second.size(); ++j) { |
---|
749 | if (j) |
---|
750 | os << " ; "; |
---|
751 | os << "\"" << trans_beg_code(conf.string2codons_[i].second[j].first) |
---|
752 | << "\":\"" |
---|
753 | << trans_end_code(conf.string2codons_[i].second[j].second) |
---|
754 | << "\""; |
---|
755 | } |
---|
756 | os << "\n"; |
---|
757 | } |
---|
758 | } |
---|
759 | return os; |
---|
760 | } |
---|
761 | |
---|
762 | |
---|
763 | Config_error::Config_error(const std::string& line,const std::string& message) |
---|
764 | : std::runtime_error(std::string("line: `") + line + |
---|
765 | std::string("' is invalid.\n") + message) |
---|
766 | {} |
---|
767 | |
---|
768 | }} // end of namespace svndigest and namespace theplu |
---|