source: trunk/lib/Configuration.cc @ 1133

Last change on this file since 1133 was 1133, checked in by Peter Johansson, 13 years ago

new function Configuration::output_blame_information. refs #330

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 16.8 KB
Line 
1// $Id: Configuration.cc 1133 2010-07-18 18:30:27Z 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 <cassert>
30#include <fstream>
31#include <map>
32#include <string>
33#include <sstream>
34#include <stdexcept>
35#include <utility>
36
37namespace theplu{
38namespace svndigest{
39
40  Configuration* Configuration::instance_=NULL;
41
42
43  Configuration::Configuration(void)
44  {
45  }
46
47
48  void Configuration::add_codon(std::string key, std::string start, 
49                                std::string end)
50  {
51    std::pair<std::string, std::string> p(start,end);
52    String2Codons::iterator iter = string2codons_.end();
53    for (String2Codons::iterator i=string2codons_.begin();
54         i!=string2codons_.end(); ++i)
55      if (i->first == key)
56        iter = i;
57   
58    if (iter==string2codons_.end())
59      string2codons_.push_back(std::make_pair(key, VectorPair(1,p)));
60    else
61      iter->second.push_back(p);
62  }
63
64
65  const std::map<std::string, std::string>&
66  Configuration::author_colors(void) const
67  {
68    return author_color_;
69  }
70
71  std::string Configuration::author_str_color(const std::string& author) const
72  {
73    std::string res;
74    std::map<std::string, std::string>::const_iterator iterator;
75    if ( (iterator=author_color_.find(author)) != author_color_.end())
76      res = iterator->second;
77    return res;
78  }
79
80
81  const std::vector<std::pair<std::string, std::string> >* 
82  Configuration::codon(std::string file_name) const 
83  {
84    if (const std::pair<std::string,std::string>* dict=dictionary(file_name))
85      file_name = translate(file_name, *dict);
86    for (String2Codons::const_iterator i(string2codons_.begin());
87         i!=string2codons_.end(); ++i) {
88      if (fnmatch(i->first.c_str(), file_name.c_str()))
89        return &i->second;
90    }
91    return NULL;
92  }
93
94
95  const std::map<std::string,Alias>& Configuration::copyright_alias(void) const
96  {
97    return copyright_alias_;
98  }
99
100
101  const std::pair<std::string,std::string>* 
102  Configuration::dictionary(std::string lhs) const
103  {
104    for (size_t i=0; i<dictionary_.size(); ++i)
105      if (fnmatch(lhs.c_str(), dictionary_[i].first.c_str()))
106        return &dictionary_[i];
107    return NULL;
108  }
109
110
111  bool Configuration::equal_false(const std::string& str) const
112  {
113    return str=="false" || str=="False" || str=="FALSE" ||
114      str=="no" || str=="No" || str=="NO";
115  }
116
117
118  bool Configuration::equal_true(const std::string& str) const
119  {
120    return str=="true" || str=="True" || str=="TRUE" ||
121      str=="yes" || str=="Yes" || str=="YES";
122  }
123
124
125  const std::string& Configuration::image_anchor_format(void) const
126  {
127    return image_anchor_format_;
128  }
129
130
131  const std::string& Configuration::image_format(void) const
132  {
133    return image_format_;
134  }
135
136
137  void Configuration::load(void)
138  {
139    set_default();
140    validate_dictionary();
141  }
142
143
144  void Configuration::load(std::istream& is)
145  {
146    assert(is.good());
147
148    bool parsing_found=false;
149    bool dictionary_found=false;
150    std::string line;
151    std::string section;
152    std::string tmp;
153    while (getline(is, line)) {
154      line = ltrim(line);
155      if (line.empty() || line[0]=='#')
156        continue;
157      std::stringstream ss(line);
158      if (line[0] == '[') {
159        getline(ss, tmp, '[');
160        getline(ss, section, ']');
161        continue;
162      }
163      std::string lhs;
164      getline(ss, lhs, '=');
165      lhs = trim(lhs);
166      std::string rhs;
167      getline(ss, rhs);
168      rhs = trim(rhs);
169      if (rhs.empty()){
170        throw Config_error(line, "expected format: <lhs> = <rhs>");
171      }
172      if (section == "copyright-alias"){
173        std::map<std::string,Alias>::iterator iter = 
174          copyright_alias_.lower_bound(lhs);
175        if (iter!=copyright_alias_.end() && iter->first==lhs){
176          std::stringstream mess;
177          mess << "in copright-alias section " << lhs << " defined twice.";
178          throw Config_error(line, mess.str());
179        }
180       
181        // insert alias
182        copyright_alias_.insert(iter,std::make_pair(lhs, Alias(rhs,copyright_alias_.size())));
183      }
184      else if (section == "trac"){
185        if (lhs=="trac-root")
186          trac_root_=rhs;
187        else {
188          std::stringstream mess;
189          mess << "in trac section" << lhs + " is invalid option.";
190          throw Config_error(line, mess.str());
191        }
192      }
193      else if (section == "copyright") {
194        if (lhs=="missing-copyright-warning") {
195          if (equal_false(rhs))
196            missing_copyright_warning_ = false;
197          else if (equal_true(rhs))
198            missing_copyright_warning_ = true;
199          else {
200            throw Config_error(line, "");
201          }
202        }
203      }
204      else if (section == "author-color") {
205        unsigned char r,g,b;
206        try {
207          str2rgb(rhs, r,g,b);
208        }
209        catch (std::runtime_error& e) {
210          throw Config_error(line, e.what());
211        }
212        author_color_[lhs] = rhs;
213      }     
214      else if (section == "parsing-codons") {
215        if (!parsing_found) {
216          parsing_found=true;
217          // clearing the default setting
218          string2codons_.clear();
219        }
220       
221        if (codon(lhs)) {
222          std::stringstream mess;
223          mess << "clashes with previous given file name pattern: ";
224          // find previous file-name-pattern
225          for (String2Codons::const_iterator i(string2codons_.begin());
226               i!=string2codons_.end(); ++i) {
227            if (fnmatch(lhs.c_str(), i->first.c_str())) {
228              mess << "`" << i->first << "'";
229              break;
230            }
231          }
232          throw Config_error(line, mess.str());
233        }
234        std::stringstream ss(rhs);
235        std::string start;
236        while (getline(ss, start, ':')) {
237          start = trim(start);
238          std::string end;
239          getline(ss, end, ';');
240          end = trim(end);
241          if (start.empty() && end.empty())
242            continue;
243          try {
244            if (start.empty() || start=="\"\"") {
245              throw std::runtime_error("start-code is empty");
246            }
247            else if (start.size()<3) {
248              std::stringstream mess;
249              mess << "start-code `" << start << "' is invalid";
250              throw std::runtime_error(mess.str());
251            }
252            start = trim(start, '"');
253            if (end.empty() || end=="\"\"") {
254              throw std::runtime_error("end-code is empty");
255            }
256            else if (end.size()<3) {
257              std::stringstream mess;
258              mess << "end-code `" << end << "' is invalid";
259              throw std::runtime_error(mess.str());
260            }
261            end = trim(end, '"');
262          }
263          catch (std::runtime_error& e){
264            throw Config_error(line, e.what());
265          }
266          replace(start, "\\n", "\n");
267          replace(end, "\\n", "\n");
268          add_codon(lhs, start, end);
269        }
270      } 
271      else if (section == "file-name-dictionary") {
272        if (!dictionary_found) {
273          dictionary_found=true;
274          // clearing the default setting
275          dictionary_.clear();
276        }
277       
278        if (const std::pair<std::string, std::string>* entry=dictionary(lhs)) {
279          std::stringstream mess;
280          mess << "clashes with previous given file name pattern: "
281               << "`" << entry->first << "'";
282          throw Config_error(line, mess.str());
283        }
284        lhs = trim(lhs);
285        rhs = trim(rhs);
286        if (!lhs.empty() && !rhs.empty()) 
287          dictionary_.push_back(std::make_pair(lhs, rhs));
288        else if (!lhs.empty() || !rhs.empty()) {
289          throw Config_error(line, "");
290        }
291      } 
292      else if (section == "image") {
293        if (lhs == "format") {
294          try {
295            image_format(rhs);
296          }
297          catch (std::runtime_error e) {
298            throw Config_error(line, 
299                               "unknown format: " + rhs + "\n" + e.what());
300          }
301        }
302        else if (lhs == "image_format") {
303          try {
304            image_anchor_format(rhs);
305          }
306          catch (std::runtime_error e) {
307            throw Config_error(line, 
308                               "unknown format: " + rhs + "\n" + e.what());
309          }
310        }
311      }
312    }
313    validate_dictionary();
314  }
315
316
317  void Configuration::image_anchor_format(const std::string& format)
318  {
319    if (format!="none" && format!="pdf" && format!="png" && format!="svg") {
320      std::ostringstream oss;
321      oss << "Valid arguments are:\n"
322          << "  - `none'\n"
323          << "  - `pdf'\n"
324          << "  - `png'\n"
325          << "  - `svg'";
326      throw std::runtime_error(oss.str());
327    }
328    image_anchor_format_ = format;
329  }
330
331
332  void Configuration::image_format(const std::string& format)
333  {
334    if (format!="none" && format!="png" && format!="svg") {
335      std::ostringstream oss;
336      oss << "Valid arguments are:\n"
337          << "  - `none'\n"
338          << "  - `png'\n"
339          << "  - `svg'";
340      throw std::runtime_error(oss.str());
341    }
342    image_format_ = format;
343  }
344
345
346  Configuration& Configuration::instance(void)
347  {
348    if (!instance_){
349      instance_ = new Configuration;
350      instance_->load();
351    }
352    return *instance_;
353  }
354
355
356  bool Configuration::missing_copyright_warning(void) const
357  {
358    return missing_copyright_warning_;
359  }
360
361
362  std::string
363  Configuration::translate(const std::string& str,
364                           const std::pair<std::string, std::string>& dic) const
365  {
366    std::string res;
367    std::vector<std::string> vec;
368    if (!regexp(dic.first, str, vec)) {
369      std::stringstream mess;
370      mess << "invalid config file: "
371           << "expression " << dic.first << " is invalid";
372      throw std::runtime_error(mess.str());       
373    }
374    for (std::string::const_iterator i(dic.second.begin()); 
375         i!=dic.second.end(); ++i) {
376      if (*i == '$') {
377        std::stringstream ss(std::string(i+1, dic.second.end()));
378        size_t n = 0;
379        ss >> n;
380        if (n>vec.size() || n==0){
381          std::stringstream mess;
382          mess << "invalid config file: "
383               << "expression " << dic.second << " is invalid";
384          if (n)
385            mess << "because " << n << " is a too large.";
386          throw std::runtime_error(mess.str());       
387        }
388        res += vec[n-1];
389        ++i;
390        if (n>9){
391          ++i;
392          if (n>99)
393            ++i;
394
395        }
396      }
397      else
398        res += *i;
399    }
400
401    return res;
402  }
403
404
405  std::string trans_end_code(std::string str)
406  {
407    if (str.size()>0 && str[str.size()-1]=='\n')
408      return str.substr(0, str.size()-1) + std::string("\\n");
409    return str;
410  }
411
412
413  std::string trans_beg_code(std::string str)
414  {
415    if (str.size()>0 && str[0]=='\n')
416      return std::string("\\n") + str.substr(1); 
417    return str;
418  }
419
420
421  std::string trim(std::string str, char c)
422  {
423    if (str.size()<2 || str[0]!=c || str[str.size()-1]!=c){
424      std::stringstream mess;
425      mess << "expected `" << str << "' to be surrounded by `" << c << "'";
426      throw std::runtime_error(mess.str());
427    }
428    return str.substr(1, str.size()-2);
429  }
430
431
432  void Configuration::set_default(void)
433  {
434    copyright_alias_.clear();
435    missing_copyright_warning_=false;
436    trac_root_ = "";
437
438    add_codon("*.ac", "#", "\n");
439    add_codon("*.ac", "dnl", "\n");
440    add_codon("*.am", "#", "\n");
441    add_codon("*.as", "#", "\n");
442    add_codon("*.as", "dnl", "\n");
443    add_codon("*.bat", "\nREM", "\n");
444    add_codon("*.bat", "\nrem", "\n");
445    add_codon("*.c", "//", "\n");
446    add_codon("*.c", "/*", "*/");
447    add_codon("*.cc", "//", "\n");
448    add_codon("*.cc", "/*", "*/");
449    add_codon("*.cpp", "//", "\n");
450    add_codon("*.cpp", "/*", "*/");
451    add_codon("*.css", "<!--", "-->");
452    add_codon("*.cxx", "//", "\n");
453    add_codon("*.cxx", "/*", "*/");
454    add_codon("*.h", "//", "\n");
455    add_codon("*.h", "/*", "*/");
456    add_codon("*.hh", "//", "\n");
457    add_codon("*.hh", "/*", "*/");
458    add_codon("*.hpp", "//", "\n");
459    add_codon("*.hpp", "/*", "*/");
460    add_codon("*.html", "<%--", "--%>");
461    add_codon("*.java", "//", "\n");
462    add_codon("*.java", "/*", "*/");
463    add_codon("*.jsp", "<!--", "-->");
464    add_codon("*.m", "%", "\n");
465    add_codon("*.m4", "#", "\n");
466    add_codon("*.m4", "dnl", "\n");
467    add_codon("*.pl", "#", "\n");
468    add_codon("*.pm", "#", "\n");
469    add_codon("*.R", "#", "\n");
470    add_codon("*.rss", "<!--", "-->");
471    add_codon("*.sgml", "<!--", "-->");
472    add_codon("*.sh", "#", "\n");
473    add_codon("*.shtml", "<!--", "-->");
474    add_codon("*.tex", "%", "\n");
475    add_codon("*.xhtml", "<!--", "-->");
476    add_codon("*.xml", "<!--", "-->");
477    add_codon("*.xsd", "<!--", "-->");
478    add_codon("*.xsl", "<!--", "-->");
479    add_codon("*config", "#", "\n");
480    add_codon("bootstrap", "#", "\n");
481    add_codon("Makefile", "#", "\n");
482
483    dictionary_ = VectorPair(1, std::make_pair("*.in", "$1"));
484    image_format_ = "png";
485    image_anchor_format_ = "png";
486    output_blame_information_ = true;
487  }
488
489
490  bool Configuration::output_blame_information(void) const
491  {
492    return output_blame_information_;
493  }
494
495
496  std::string Configuration::trac_root(void) const
497  {
498    return trac_root_;
499  }
500
501
502  void Configuration::validate_dictionary(void) const
503  {
504    VectorPair::const_iterator end(dictionary_.end());
505    for (VectorPair::const_iterator iter(dictionary_.begin());iter!=end;++iter){
506      std::string word(iter->first);
507      replace(word, "*", "");
508      replace(word, "?", "");
509      // throws if dictionary is invalid
510      translate(word, *iter);
511    }
512  }
513
514
515  std::ostream& operator<<(std::ostream& os, const Configuration& conf)
516  {
517    os << "### This file configures various behaviors for svndigest\n"
518       << "### The commented-out below are intended to demonstrate how to use\n"
519       << "### this file.\n"
520       << "\n"
521       << "### Section for setting behaviour of copyright update\n"
522       << "[copyright]\n"
523       << "# if true svndigest will warn if file has no copyright statement.\n"
524       << "missing-copyright-warning = ";
525   
526    if (conf.missing_copyright_warning())
527      os << "yes\n";
528    else
529      os << "no\n";
530
531    os << "\n"
532       << "### Section for setting aliases used in copyright update\n"
533       << "[copyright-alias]\n"
534       << "# jdoe = John Doe\n";
535
536    typedef std::vector<std::pair<std::string, Alias> > vector;
537    vector vec;
538    std::back_insert_iterator<vector> back_insert_iterator(vec);
539    vec.reserve(conf.copyright_alias().size());
540    std::copy(conf.copyright_alias().begin(), conf.copyright_alias().end(),
541              back_insert_iterator);
542    // sort with respect to Alias.id
543    IdCompare id;
544    PairSecondCompare<const std::string, Alias, IdCompare> comp(id);
545    std::sort(vec.begin(),vec.end(), comp);
546             
547
548    for (vector::const_iterator i(vec.begin()); i!=vec.end(); ++i) {
549      os << i->first << " = " << i->second.name() << "\n";
550    }
551
552    os << "\n"
553       << "### Section for images\n"
554       << "[image]\n"
555       << "format = " << conf.image_format() << "\n";
556    os << "anchor_format = " << conf.image_anchor_format() << "\n";
557
558
559    os << "\n"
560       << "### Section for author color in plots and blame output.\n"
561       << "[author-color]\n"
562       << "# jdoe = 000000\n";
563    typedef std::map<std::string,std::string> str_map;
564    for (str_map::const_iterator i(conf.author_color_.begin());
565         i!=conf.author_color_.end(); ++i) {
566      os << i->first << " = " << i->second << "\n";
567    }
568
569    os << "\n"
570       << "### Section for setting trac environment\n"
571       << "[trac]\n"
572       << "# If trac-root is set, svndigest will create anchors to "
573       << "the Trac page.\n"
574       << "# trac-root = http://dev.thep.lu.se/svndigest/\n";
575    if (!conf.trac_root().empty())
576      os << "trac-root = " << conf.trac_root() << "\n";
577
578    if (!conf.dictionary_.empty()) {
579      os << "\n"
580         << "### Section for setting dictionary for file names.\n"
581         << "### Prior looking for file name pattern in section " 
582         << "[parsing-codons],\n"
583         << "### the file name may be translated according to the rules \n"
584         << "### in this section. In default setting there is, for example,\n"
585         << "### a rule to translate `<FILENAME>.in' to `<FILENAME>'.\n"
586         << "### The format of the entries is:\n"
587         << "###    file-name-pattern = new-name\n"
588         << "### Left hand side may contain wildcards (such as '*' and '?').\n"
589         << "### Right hand side may contain \"$i\", which will be replaced \n"
590         << "### with the ith wild card in lhs string.\n"
591         << "[file-name-dictionary]\n";
592      for (size_t i=0; i<conf.dictionary_.size(); ++i)
593        os << conf.dictionary_[i].first << " = " 
594           << conf.dictionary_[i].second << "\n"; 
595    }
596    if (!conf.string2codons_.empty()) {
597      os << "\n"
598         << "### Section for setting parsing modes\n"
599         << "### The format of the entries is:\n"
600         << "###   file-name-pattern = \"start-code\" : \"end-code\"\n"
601         << "### The file-name-pattern may contain wildcards (such as '*' "
602         << "and '?').\n"
603         << "### String \"\\n\" can be used for codons containing newline"
604         << "\n### character.\n"
605         << "[parsing-codons]\n";
606      for (size_t i=0; i<conf.string2codons_.size(); ++i) {
607        os << conf.string2codons_[i].first << " = "; 
608        for (size_t j=0; j<conf.string2codons_[i].second.size(); ++j) {
609          if (j)
610            os << "  ;  ";
611          os << "\"" << trans_beg_code(conf.string2codons_[i].second[j].first) 
612             << "\":\"" 
613             << trans_end_code(conf.string2codons_[i].second[j].second) 
614             << "\""; 
615        }
616        os << "\n";
617      }
618    }
619    return os;
620  }
621
622 
623  Config_error::Config_error(const std::string& line,const std::string& message)
624    : std::runtime_error(std::string("line: `") + line + 
625                         std::string("' is invalid.\n") + message)
626  {}
627
628}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.