1 | // $Id: Configuration.cc 304 2007-05-11 20:42:04Z 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 | |
---|
36 | namespace theplu{ |
---|
37 | namespace 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-root") |
---|
99 | trac_root_=value; |
---|
100 | else { |
---|
101 | std::stringstream mess; |
---|
102 | mess << "svndigest: invalid config file: " |
---|
103 | << "in trac section" << key + " is invalid option."; |
---|
104 | throw std::runtime_error(mess.str()); |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | Configuration& Configuration::instance(void) |
---|
112 | { |
---|
113 | if (!instance_) |
---|
114 | instance_ = new Configuration; |
---|
115 | return *instance_; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | void Configuration::set_default(void) |
---|
120 | { |
---|
121 | copyright_alias_.clear(); |
---|
122 | trac_root_ = ""; |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | std::string Configuration::trac_root(void) const |
---|
127 | { |
---|
128 | return trac_root_; |
---|
129 | } |
---|
130 | |
---|
131 | |
---|
132 | std::ostream& operator<<(std::ostream& os, const Configuration& conf) |
---|
133 | { |
---|
134 | os << "### This file configures various behaviors for svndigest\n" |
---|
135 | << "### The commented-out below are intended to demonstrate how to use\n" |
---|
136 | << "### this file.\n" |
---|
137 | << "\n" |
---|
138 | << "### Section for setting aliases used in copyright update\n" |
---|
139 | << "[copyright-alias]\n" |
---|
140 | << "# jdoe = John Doe\n"; |
---|
141 | |
---|
142 | typedef std::vector<std::pair<std::string, Alias> > vector; |
---|
143 | vector vec; |
---|
144 | std::back_insert_iterator<vector> back_insert_iterator(vec); |
---|
145 | vec.reserve(conf.copyright_alias().size()); |
---|
146 | std::copy(conf.copyright_alias().begin(), conf.copyright_alias().end(), |
---|
147 | back_insert_iterator); |
---|
148 | // sort with respect to Alias.id |
---|
149 | IdCompare id; |
---|
150 | PairSecondCompare<const std::string, Alias, IdCompare> comp(id); |
---|
151 | std::sort(vec.begin(),vec.end(), comp); |
---|
152 | |
---|
153 | |
---|
154 | for (vector::const_iterator i(vec.begin()); i!=vec.end(); ++i) { |
---|
155 | os << i->first << " = " << i->second.name() << " \n"; |
---|
156 | } |
---|
157 | |
---|
158 | os << "\n" |
---|
159 | << "### Section for setting trac environment\n" |
---|
160 | << "[trac]\n" |
---|
161 | << "# If trac-root is set, svndigest will create anchors to " |
---|
162 | << "the Trac page.\n" |
---|
163 | << "# trac-root = http://lev.thep.lu.se/trac/svndigest/\n"; |
---|
164 | if (!conf.trac_root().empty()) |
---|
165 | os << "trac-root = " << conf.trac_root() << "\n"; |
---|
166 | |
---|
167 | return os; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | }} // end of namespace svndigest and namespace theplu |
---|