source: trunk/lib/Configuration.cc @ 446

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

fixes #243

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1// $Id: Configuration.cc 446 2007-08-16 15:57:18Z 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
141  Configuration& Configuration::instance(void)
142  {
143    if (!instance_)
144      instance_ = new Configuration;
145    return *instance_;
146  }
147
148
149  bool Configuration::missing_copyright_warning(void) const
150  {
151    return missing_copyright_warning_;
152  }
153
154
155  void Configuration::set_default(void)
156  {
157    copyright_alias_.clear();
158    missing_copyright_warning_=false;
159    trac_root_ = "";
160  }
161
162
163  std::string Configuration::trac_root(void) const
164  {
165    return trac_root_;
166  }
167
168
169  std::ostream& operator<<(std::ostream& os, const Configuration& conf)
170  {
171    os << "### This file configures various behaviors for svndigest\n"
172       << "### The commented-out below are intended to demonstrate how to use\n"
173       << "### this file.\n"
174       << "\n"
175       << "### Section for setting behaviour of copyright update\n"
176       << "[copyright]\n"
177       << "# if true svndigest will warn if file has no copyright statement.\n"
178       << "missing-copyright-warning = ";
179   
180    if (conf.missing_copyright_warning())
181      os << "yes\n";
182    else
183      os << "no\n";
184
185    os << "\n"
186       << "### Section for setting aliases used in copyright update\n"
187       << "[copyright-alias]\n"
188       << "# jdoe = John Doe\n";
189
190    typedef std::vector<std::pair<std::string, Alias> > vector;
191    vector vec;
192    std::back_insert_iterator<vector> back_insert_iterator(vec);
193    vec.reserve(conf.copyright_alias().size());
194    std::copy(conf.copyright_alias().begin(), conf.copyright_alias().end(),
195              back_insert_iterator);
196    // sort with respect to Alias.id
197    IdCompare id;
198    PairSecondCompare<const std::string, Alias, IdCompare> comp(id);
199    std::sort(vec.begin(),vec.end(), comp);
200             
201
202    for (vector::const_iterator i(vec.begin()); i!=vec.end(); ++i) {
203      os << i->first << " = " << i->second.name() << " \n";
204    }
205
206    os << "\n"
207       << "### Section for setting trac environment\n"
208       << "[trac]\n"
209       << "# If trac-root is set, svndigest will create anchors to "
210       << "the Trac page.\n"
211       << "# trac-root = http://trac.thep.lu.se/trac/svndigest/\n";
212    if (!conf.trac_root().empty())
213      os << "trac-root = " << conf.trac_root() << "\n";
214
215    return os;
216  }
217
218
219}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.