source: trunk/lib/Configuration.cc @ 303

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

Sorting authors in copyright statement in same order as stated in config file. fixes #172

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1// $Id: Configuration.cc 303 2007-05-11 20:13:00Z peter $
2
3/*
4  Copyright (C) 2007 Peter Johansson
5
6  This file is part of svndigest, http://lev.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  void Configuration::load(void)
54  {
55    set_default();
56  }
57
58
59  void Configuration::load(std::istream& is)
60  {
61    assert(is.good());
62    set_default();
63
64    std::string line;
65    std::string section;
66    std::string tmp;
67    while (getline(is, line)) {
68      line = ltrim(line);
69      if (line.empty() || line[0]=='#')
70        continue;
71      std::stringstream ss(line);
72      if (line[0] == '[') {
73        getline(ss, tmp, '[');
74        getline(ss, section, ']');
75      }
76      else if (section == "copyright-alias"){
77        getline(ss, tmp, '=');
78        std::string key = trim(tmp);
79        getline(ss, tmp);
80        std::string name = trim(tmp);
81        std::map<std::string,Alias>::iterator iter = 
82          copyright_alias_.lower_bound(key);
83        if (iter!=copyright_alias_.end() && iter->first==key){
84          std::stringstream mess;
85          mess << "svndigest: invalid config file: "
86               << "in copright-alias section" << key + "defined twice.";
87          throw std::runtime_error(mess.str());
88        }
89       
90        // insert alias
91        copyright_alias_.insert(iter,std::make_pair(key, Alias(name,copyright_alias_.size())));
92      }
93      else if (section == "trac"){
94        getline(ss, tmp, '=');
95        std::string key = trim(tmp);
96        getline(ss, tmp);
97        std::string value = trim(tmp);
98        if (key=="trac-ticket"){
99          std::cerr << "svndigest: warning: config variable trac-ticket "
100                    << "is deprecated. Use trac-root instead.\n";
101          trac_root_ = value.substr(0,value.size()-7);
102        }
103        if (key=="trac-revision"){
104          std::cerr << "svndigest: warning: config variable trac-revision "
105                    << "is deprecated. Use trac-root instead.\n";
106          trac_root_ = value.substr(0,value.size()-10);
107        }
108        if (key=="trac-root")
109          trac_root_=value;
110      }
111    }
112  }
113
114
115  Configuration& Configuration::instance(void)
116  {
117    if (!instance_)
118      instance_ = new Configuration;
119    return *instance_;
120  }
121
122
123  void Configuration::set_default(void)
124  {
125    copyright_alias_.clear();
126    trac_root_ = "";
127  }
128
129
130  std::string Configuration::trac_root(void) const
131  {
132    return trac_root_;
133  }
134
135
136  std::ostream& operator<<(std::ostream& os, const Configuration& conf)
137  {
138    os << "### This file configures various behaviors for svndigest\n"
139       << "### The commented-out below are intended to demonstrate how to use\n" 
140       << "### this file.\n"
141       << "\n"
142       << "### Section for setting aliases used in copyright update\n"
143       << "[copyright-alias]\n"
144       << "# jdoe = John Doe\n";
145
146    typedef std::vector<std::pair<std::string, Alias> > vector;
147    vector vec;
148    std::back_insert_iterator<vector> back_insert_iterator(vec);
149    vec.reserve(conf.copyright_alias().size());
150    std::copy(conf.copyright_alias().begin(), conf.copyright_alias().end(),
151              back_insert_iterator);
152    // sort with respect to Alias.id
153    IdCompare id;
154    PairSecondCompare<const std::string, Alias, IdCompare> comp(id);
155    std::sort(vec.begin(),vec.end(), comp);
156             
157
158    for (vector::const_iterator i(vec.begin()); i!=vec.end(); ++i) {
159      os << i->first << " = " << i->second.name() << " \n";
160    }
161
162    os << "\n"
163       << "### Section for setting trac environment\n"
164       << "[trac]\n"
165       << "# If trac-root is set, svndigest will create anchors to "
166       << "the Trac page.\n"
167       << "# trac-root = http://lev.thep.lu.se/trac/svndigest/\n";
168    if (!conf.trac_root().empty())
169      os << "trac-root = " << conf.trac_root() << "\n";
170
171    return os;
172  }
173
174
175}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.