source: trunk/lib/Configuration.cc @ 289

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

fixed trac link problem. fixes #178 and refs #180. Also changed format of config file.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1// $Id: Configuration.cc 289 2007-05-08 12:48:07Z 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 <utility>
34
35namespace theplu{
36namespace svndigest{
37
38  Configuration* Configuration::instance_=NULL;
39
40
41  Configuration::Configuration(void)
42  {
43  }
44
45
46  void Configuration::load(void)
47  {
48    set_default();
49  }
50
51
52  void Configuration::load(std::istream& is)
53  {
54    assert(is.good());
55    set_default();
56
57    std::string line;
58    std::string section;
59    std::string tmp;
60    while (getline(is, line)) {
61      line = ltrim(line);
62      if (line.empty() || line[0]=='#')
63        continue;
64      std::stringstream ss(line);
65      if (line[0] == '[') {
66        getline(ss, tmp, '[');
67        getline(ss, section, ']');
68      }
69      else if (section == "copyright-alias"){
70        getline(ss, tmp, '=');
71        std::string key = trim(tmp);
72        getline(ss, tmp);
73        std::string name = trim(tmp);
74        copyright_alias_[key] = name;
75      }
76      else if (section == "trac"){
77        getline(ss, tmp, '=');
78        std::string key = trim(tmp);
79        getline(ss, tmp);
80        std::string value = trim(tmp);
81        if (key=="trac-ticket"){
82          std::cerr << "svndigest: warning: config variable trac-ticket "
83                    << "is deprecated. Use trac-root instead.\n";
84          trac_root_ = value.substr(0,value.size()-7);
85        }
86        if (key=="trac-revision"){
87          std::cerr << "svndigest: warning: config variable trac-revision "
88                    << "is deprecated. Use trac-root instead.\n";
89          trac_root_ = value.substr(0,value.size()-10);
90        }
91        if (key=="trac-root")
92          trac_root_=value;
93      }
94    }
95  }
96
97
98  Configuration& Configuration::instance(void)
99  {
100    if (!instance_)
101      instance_ = new Configuration;
102    return *instance_;
103  }
104
105
106  void Configuration::set_default(void)
107  {
108    copyright_alias_.clear();
109    trac_root_ = "";
110  }
111
112
113  std::string Configuration::trac_root(void) const
114  {
115    return trac_root_;
116  }
117
118
119  std::ostream& operator<<(std::ostream& os, const Configuration& conf)
120  {
121    os << "### This file configures various behaviors for svndigest\n"
122       << "### The commented-out below are intended to demonstrate how to use\n" 
123       << "### this file.\n"
124       << "\n"
125       << "### Section for setting aliases used in copyright update\n"
126       << "[copyright-alias]\n"
127       << "# jdoe = John Doe\n";
128   
129    typedef std::map<std::string, std::string> map; 
130    for (map::const_iterator i=conf.copyright_alias().begin();
131         i!=conf.copyright_alias().end(); ++i) {
132      os << i->first << " = " << i->second << " \n";
133    }
134
135    os << "\n"
136       << "### Section for setting trac environment\n"
137       << "[trac]\n"
138       << "# If trac-root is set, svndigest will create anchors to "
139       << "the Trac page.\n"
140       << "# trac-root = http://lev.thep.lu.se/trac/svndigest/\n";
141    if (!conf.trac_root().empty())
142      os << "trac-root = " << conf.trac_root() << "\n";
143
144    return os;
145  }
146
147
148}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.