Merge branch 'daemon-ext'
[libi2ncommon] / src / i2n_configfile.cpp
1 /*
2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
4
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
7
8 As a special exception, if other files instantiate templates or use macros
9 or inline functions from this file, or you compile this file and link it
10 with other works to produce a work based on this file, this file
11 does not by itself cause the resulting work to be covered
12 by the GNU General Public License.
13
14 However the source code for this file must still be made available
15 in accordance with section (3) of the GNU General Public License.
16
17 This exception does not invalidate any other reasons why a work based
18 on this file might be covered by the GNU General Public License.
19 */
20 /** @file
21  *
22  * @author Reinhard Pfau
23  * 
24  * @copyright © Copyright 2007-2008 by Intra2net AG
25  */
26
27 //#define NOISEDEBUG
28
29 #include "i2n_configfile.hpp"
30
31 #include <algorithm>
32 #include <iterator>
33 #include <fstream>
34
35 #include <stringfunc.hxx>
36
37
38 #ifdef NOISEDEBUG
39 #include <iostream>
40 #define DOUT(msg) std::cout << msg << std::endl
41 #else
42 #define DOUT(msg) do {} while (0)
43 #endif
44
45
46
47 namespace I2n
48 {
49
50 /**
51  * loads config data from INI style configuration file.
52  * File is processed line by line.
53  * Empty lines are ignored; lines starting with '#' or ';' are ignored too (they are
54  * expected to be comments).
55  * Lines consisting of a bracketed expression like '[group]' start a new group.
56  * Other lines have to be in the form 'key = value' and define a key-value assignment in the current
57  * group.
58  * @param filename path to the config file which is read.
59  * @param[out] result the resulting config data.
60  * @param decoder (optional) decoder for the values.
61  * @return @a true if the file was succesfully parsed.
62  */
63 bool load_ini_config_file(
64     const std::string& filename,
65     ConfigData& result,
66     const ValueDecoder& decoder
67 )
68 {
69     std::ifstream f;
70     f.open(filename.c_str());
71     if (!f.good() )
72     {
73         return false;
74     }
75     return load_ini_config(f,result);
76 } // eo load_ini_config_file(const std::string&,ConfigData&)
77
78
79 /**
80  * loads config data from INI style configuration file.
81  * File is processed line by line.
82  * Empty lines are ignored; lines starting with '#' or ';' are ignored too (they are
83  * expected to be comments).
84  * Lines consisting of a bracketed expression like '[group]' start a new group.
85  * Other lines have to be in the form 'key = value' and define a key-value assignment in the current
86  * group.
87  * @param f istream to read the data from.
88  * @param[out] result the resulting config data.
89  * @param decoder (optional) decoder for the values.
90  * @return @a true if the file was successfully parsed.
91  */
92 bool load_ini_config(
93     std::istream& f,
94     ConfigData& result,
95     const ValueDecoder& decoder
96 )
97 {
98     result.clear();
99     if (!f.good())
100     {
101         return false;
102     }
103     std::string group("");
104     while (!f.eof() && f.good())
105     {
106         std::string line;
107         getline(f,line);
108         trim_mod(line);
109         if (line.empty() || line[0] == '#'|| line[0]==';') 
110         {
111             continue;
112         }
113         if (line[0] == '[' && line[line.size()-1] == ']')
114         {
115             // start new group:
116             group= line.substr(1, line.size()-2);
117             result[group];
118             continue;
119         }
120         std::string key, value;
121         if (pair_split(line,key,value,'='))
122         {
123             if (decoder)
124             {
125                 value= decoder(value);
126             }
127             result[group][key]+= value;
128         }
129         else
130         {
131             // bad line....
132             //TODO: spit more information?!
133             return false;
134         }
135     }
136     return true;
137 } // eo load_ini_config
138
139
140 } // eo namespace I2n