2 The software in this package is distributed under the GNU General
3 Public License version 2 (with a special exception described below).
5 A copy of GNU General Public License (GPL) is included in this distribution,
6 in the file COPYING.GPL.
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.
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.
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.
22 * @author Reinhard Pfau
24 * @copyright © Copyright 2007-2008 by Intra2net AG
29 #include "i2n_configfile.hpp"
35 #include <stringfunc.hxx>
40 #define DOUT(msg) std::cout << msg << std::endl
42 #define DOUT(msg) do {} while (0)
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
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.
63 bool load_ini_config_file(
64 const std::string& filename,
66 const ValueDecoder& decoder
70 f.open(filename.c_str());
75 return load_ini_config(f,result);
76 } // eo load_ini_config_file(const std::string&,ConfigData&)
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
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.
95 const ValueDecoder& decoder
103 std::string group("");
104 while (!f.eof() && f.good())
109 if (line.empty() || line[0] == '#'|| line[0]==';')
113 if (line[0] == '[' && line[line.size()-1] == ']')
116 group= line.substr(1, line.size()-2);
120 std::string key, value;
121 if (pair_split(line,key,value,'='))
125 value= decoder(value);
127 result[group][key]+= value;
132 //TODO: spit more information?!
137 } // eo load_ini_config
140 } // eo namespace I2n