source: trunk/lib/Configuration.cc @ 264

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

turned Configuration class into a singleton

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.5 KB
Line 
1// $Id: Configuration.cc 264 2007-04-30 21:54:25Z 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
34namespace theplu{
35namespace svndigest{
36
37  Configuration* Configuration::instance_=NULL;
38
39
40  Configuration::Configuration(void)
41  {
42  }
43
44
45  void Configuration::load(void)
46  {
47    set_default();
48  }
49
50
51  void Configuration::load(std::istream& is)
52  {
53    assert(is.good());
54    set_default();
55
56    std::string line;
57    std::string section;
58    std::string tmp;
59    while (getline(is, line)) {
60      line = ltrim(line);
61      if (line.empty() || line[0]=='#')
62        continue;
63      std::stringstream ss(line);
64      if (line[0] == '[') {
65        getline(ss, tmp, '[');
66        getline(ss, section, ']');
67      }
68      else if (section == "copyright-alias"){
69        getline(ss, tmp, '=');
70        std::string key = trim(tmp);
71        getline(ss, tmp);
72        std::string name = trim(tmp);
73        copyright_alias_[key] = name;
74      }
75    }
76  }
77
78
79  Configuration* Configuration::instance(void)
80  {
81    if (!instance_)
82      instance_ = new Configuration;
83    return instance_;
84  }
85
86
87  void Configuration::set_default(void)
88  {
89    copyright_alias_.clear();
90  }
91
92
93  std::ostream& operator<<(std::ostream& os, const Configuration& conf)
94  {
95    os << "### This file configures various behaviors for svndigest\n"
96       << "### The commented-out below are intended to demonstrate how to use\n" 
97       << "### this file.\n"
98       << "\n"
99       << "### Section for setting aliases used in copyright update\n"
100       << "[copyright-alias]\n"
101       << "# jdoe = John Doe\n";
102   
103    typedef std::map<std::string, std::string> map; 
104    for (map::const_iterator i=conf.copyright_alias().begin();
105         i!=conf.copyright_alias().end(); ++i) {
106      os << i->first << " = " << i->second << " \n";
107    }
108    return os;
109  }
110
111
112}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.