2
$Id: Configuration.cxx 15564 2013-01-07 14:25:32Z sloot $
3
$URL: https://ilk.uvt.nl/svn/sources/libticcutils/trunk/src/Configuration.cxx $
4
Copyright (c) 2006 - 2013
7
This file is part of ticcutils.
9
ticcutils is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 3 of the License, or
12
(at your option) any later version.
14
ticcutils is distributed in the hope that it will be useful,
15
but WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
GNU General Public License for more details.
19
You should have received a copy of the GNU General Public License
20
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
For questions and suggestions, see:
23
http://ilk.uvt.nl/software.html
34
#include "ticcutils/StringOps.h"
35
#include "ticcutils/Configuration.h"
40
Configuration::Configuration(){
41
myMap["global"] = ssMap();
44
string dirname( const string& f ){
45
string::size_type pos = f.find_last_of("/");
46
if ( pos != string::npos ){
47
return f.substr( 0, pos+1 );
53
string fixControl( const string& s, char c ){
70
throw logic_error("invalid char for fixControl" );
72
string::size_type pos1 = s.find( sString );
73
if ( pos1 == string::npos ){
77
string result = s.substr( 0, pos1 );
79
string::size_type pos2 = s.find( sString, pos1+1 );
80
while ( pos2 != string::npos ){
81
result += s.substr( pos1+2, pos2-pos1-2 );
84
pos2 = s.find( sString, pos1+1 );
86
result += s.substr( pos1+2 );
91
string fixControls( const string& s ){
93
result = fixControl( result, 'n' );
94
result = fixControl( result, 'r' );
95
result = fixControl( result, 't' );
99
bool Configuration::fill( const string& fileName ){
100
cdir = dirname( fileName );
101
// cerr << "dirname= " << cdir << endl;
102
ifstream is( fileName.c_str() );
104
cerr << "unable to read configuration from " << fileName << endl;
108
string section = "global";
109
while ( getline( is, inLine ) ){
110
string line = TiCC::trim(inLine);
113
if ( line[0] == '#' )
115
if ( line.find( "[[") == 0 )
116
if ( line[line.length()-1] == ']' &&
117
line[line.length()-2] == ']' ){
118
section = line.substr(2,line.length()-4);
119
// cerr << "GOT section = " << section << endl;
122
cerr << "invalid section: in line '" << line << "'" << endl;
126
vector<string> parts;
127
int num = TiCC::split_at( line, parts, "=" );
129
string val = parts[1];
130
if ( val[0] == '"' && val[val.length()-1] == '"' )
131
val = val.substr(1, val.length()-2);
132
val = fixControls( val );
133
myMap[section][parts[0]] = val;
134
if ( section == "global"
135
&& parts[0] == "configDir" )
139
cerr << "invalid attribute value pair in line '" << line << "'" << endl;
147
bool Configuration::fill( const string& fileName, const string& section ){
148
ifstream is( fileName.c_str() );
150
cerr << "unable to read configuration from " << fileName << endl;
156
// cerr << "looking for section = " << section << endl;
157
while ( getline( is, inLine ) ){
158
string line = TiCC::trim(inLine);
161
if ( line[0] == '#' )
163
if ( line.find( "[[") == 0 )
164
if ( line[line.length()-1] == ']' &&
165
line[line.length()-2] == ']' ){
166
localsection = line.substr(2,line.length()-4);
167
// cerr << "GOT section = " << localsection << endl;
170
cerr << "invalid section: in line '" << line << "'" << endl;
173
else if ( localsection == section ){
175
vector<string> parts;
176
int num = TiCC::split_at( line, parts, "=" );
178
string val = parts[1];
179
if ( val[0] == '"' && val[val.length()-1] == '"' )
180
val = val.substr(1, val.length()-2);
181
myMap[section][parts[0]] = val;
184
cerr << "invalid attribute value pair in line '" << line << "'" << endl;
190
cerr << "unable to find a section [[" << section << "]] in file: "
197
void Configuration::dump( ostream& os ) const {
198
sssMap::const_iterator it1 = myMap.find("global");
199
if ( it1 == myMap.end() ){
200
os << "empty" << endl;
203
os << "[global]" << endl;
204
os << "configDir=" << cdir << endl;
205
ssMap::const_iterator it2 = it1->second.begin();
206
while ( it2 != it1->second.end() ){
207
os << it2->first << "=" << it2->second << endl;
211
while ( it1 != myMap.end() ){
212
if ( it1->first != "global" ){
213
os << "[" << it1->first << "]" << endl;
214
it2 = it1->second.begin();
215
while ( it2 != it1->second.end() ){
216
os << it2->first << "=" << it2->second << endl;
224
string Configuration::setatt( const string& att,
226
const string& sect ){
228
string section = sect;
229
if ( section.empty() )
231
sssMap::iterator it1 = myMap.find( section );
232
if ( it1 != myMap.end() ){
233
ssMap::iterator it2 = it1->second.find( att );
234
if ( it2 != it1->second.end() ){
235
oldVal = it2->second;
237
it1->second[att] = val;
242
string Configuration::lookUp( const string& att, const string& section ) const {
243
sssMap::const_iterator it1;
244
if ( section.empty() )
245
it1 = myMap.find( "global" );
247
it1 = myMap.find( section );
248
if ( it1 == myMap.end() ){
252
ssMap::const_iterator it2 = it1->second.find( att );
253
if ( it2 == it1->second.end() ){
254
if ( section.empty() || section == "global" )
257
return lookUp( att, "global" );
264
map<string,string> Configuration::lookUpAll( const string& section ) const {
265
map<string,string> result;
266
sssMap::const_iterator it1;
267
if ( section.empty() )
268
it1 = myMap.find( "global" );
270
it1 = myMap.find( section );
271
if ( it1 != myMap.end() ){
272
ssMap::const_iterator it2 = it1->second.begin();
273
while ( it2 != it1->second.end() ){
274
result[it2->first] = it2->second;
281
set<string> Configuration::lookUpSections() const {
283
result.insert("global");
284
sssMap::const_iterator it = myMap.begin();
285
while ( it != myMap.end() ){
286
result.insert( it->first );
292
bool Configuration::hasSection( const string& section ) const {
293
if ( !section.empty() ){
294
sssMap::const_iterator it1 = myMap.find( section );
295
if ( it1 != myMap.end() )