source: trunk/lib/Configuration.cc @ 449

Last change on this file since 449 was 449, checked in by Peter Johansson, 14 years ago

fixes #250

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1// $Id: Configuration.cc 449 2007-08-16 19:03:36Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "Configuration.h"
25
26#include "utility.h"
27
28#include <cassert>
29#include <fstream>
30#include <map>
31#include <string>
32#include <sstream>
33#include <stdexcept>
34#include <utility>
35
36namespace theplu{
37namespace svndigest{
38
39  Configuration* Configuration::instance_=NULL;
40
41
42  Configuration::Configuration(void)
43  {
44  }
45
46
47  const std::map<std::string,Alias>& Configuration::copyright_alias(void) const
48  {
49    return copyright_alias_;
50  }
51
52
53  bool Configuration::equal_false(const std::string& str) const
54  {
55    return str=="false" || str=="False" || str=="FALSE" ||
56      str=="no" || str=="No" || str=="NO";
57  }
58
59
60  bool Configuration::equal_true(const std::string& str) const
61  {
62    return str=="true" || str=="True" || str=="TRUE" ||
63      str=="yes" || str=="Yes" || str=="YES";
64  }
65
66
67  void Configuration::load(void)
68  {
69    set_default();
70  }
71
72
73  void Configuration::load(std::istream& is)
74  {
75    assert(is.good());
76    set_default();
77
78    std::string line;
79    std::string section;
80    std::string tmp;
81    while (getline(is, line)) {
82      line = ltrim(line);
83      if (line.empty() || line[0]=='#')
84        continue;
85      std::stringstream ss(line);
86      if (line[0] == '[') {
87        getline(ss, tmp, '[');
88        getline(ss, section, ']');
89        continue;
90      }
91      getline(ss, tmp, '=');
92      std::string lhs = trim(tmp);
93      getline(ss, tmp);
94      std::string rhs = trim(tmp);
95      if (rhs.empty()){
96        std::stringstream mess;
97        mess << "svndigest: invalid config file: "
98             << "line: `" << line << "' is invalid.";
99        throw std::runtime_error(mess.str());
100      }
101      if (section == "copyright-alias"){
102        std::map<std::string,Alias>::iterator iter = 
103          copyright_alias_.lower_bound(lhs);
104        if (iter!=copyright_alias_.end() && iter->first==lhs){
105          std::stringstream mess;
106          mess << "svndigest: invalid config file: "
107               << "in copright-alias section " << lhs << " defined twice.";
108          throw std::runtime_error(mess.str());
109        }
110       
111        // insert alias
112        copyright_alias_.insert(iter,std::make_pair(lhs, Alias(rhs,copyright_alias_.size())));
113      }
114      else if (section == "trac"){
115        if (lhs=="trac-root")
116          trac_root_=rhs;
117        else {
118          std::stringstream mess;
119          mess << "svndigest: invalid config file: "
120               << "in trac section" << lhs + " is invalid option.";
121          throw std::runtime_error(mess.str());
122        }
123      }
124      else if (section == "copyright")
125        if (lhs=="missing-copyright-warning")
126          if (equal_false(rhs))
127            missing_copyright_warning_ = false;
128          else if (equal_true(rhs))
129            missing_copyright_warning_ = true;
130          else {
131            std::stringstream mess;
132            mess << "svndigest: invalid config file: "
133                 << "line: `" << line << "' is invalid.";
134            throw std::runtime_error(mess.str());
135          }
136    }
137  }
138
139
140  Configuration& Configuration::instance(void)
141  {
142    if (!instance_)
143      instance_ = new Configuration;
144    return *instance_;
145  }
146
147
148  bool Configuration::missing_copyright_warning(void) const
149  {
150    return missing_copyright_warning_;
151  }
152
153
154  void Configuration::set_default(void)
155  {
156    copyright_alias_.clear();
157    missing_copyright_warning_=false;
158    trac_root_ = "";
159  }
160
161
162  std::string Configuration::trac_root(void) const
163  {
164    return trac_root_;
165  }
166
167
168  std::ostream& operator<<(std::ostream& os, const Configuration& conf)
169  {
170    os << "### This file configures various behaviors for svndigest\n"
171       << "### The commented-out below are intended to demonstrate how to use\n"
172       << "### this file.\n"
173       << "\n"
174       << "### Section for setting behaviour of copyright update\n"
175       << "[copyright]\n"
176       << "# if true svndigest will warn if file has no copyright statement.\n"
177       << "missing-copyright-warning = ";
178   
179    if (conf.missing_copyright_warning())
180      os << "yes\n";
181    else
182      os << "no\n";
183
184    os << "\n"
185       << "### Section for setting aliases used in copyright update\n"
186       << "[copyright-alias]\n"
187       << "# jdoe = John Doe\n";
188
189    typedef std::vector<std::pair<std::string, Alias> > vector;
190    vector vec;
191    std::back_insert_iterator<vector> back_insert_iterator(vec);
192    vec.reserve(conf.copyright_alias().size());
193    std::copy(conf.copyright_alias().begin(), conf.copyright_alias().end(),
194              back_insert_iterator);
195    // sort with respect to Alias.id
196    IdCompare id;
197    PairSecondCompare<const std::string, Alias, IdCompare> comp(id);
198    std::sort(vec.begin(),vec.end(), comp);
199             
200
201    for (vector::const_iterator i(vec.begin()); i!=vec.end(); ++i) {
202      os << i->first << " = " << i->second.name() << " \n";
203    }
204
205    os << "\n"
206       << "### Section for setting trac environment\n"
207       << "[trac]\n"
208       << "# If trac-root is set, svndigest will create anchors to "
209       << "the Trac page.\n"
210       << "# trac-root = http://trac.thep.lu.se/trac/svndigest/\n";
211    if (!conf.trac_root().empty())
212      os << "trac-root = " << conf.trac_root() << "\n";
213
214    return os;
215  }
216
217
218}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.