/* The software in this package is distributed under the GNU General Public License version 2 (with a special exception described below). A copy of GNU General Public License (GPL) is included in this distribution, in the file COPYING.GPL. As a special exception, if other files instantiate templates or use macros or inline functions from this file, or you compile this file and link it with other works to produce a work based on this file, this file does not by itself cause the resulting work to be covered by the GNU General Public License. However the source code for this file must still be made available in accordance with section (3) of the GNU General Public License. This exception does not invalidate any other reasons why a work based on this file might be covered by the GNU General Public License. */ /** @file * * @author Reinhard Pfau * * @copyright © Copyright 2007-2008 by Intra2net AG */ //#define NOISEDEBUG #include "i2n_configfile.hpp" #include #include #include #include #ifdef NOISEDEBUG #include #define DOUT(msg) std::cout << msg << std::endl #else #define DOUT(msg) do {} while (0) #endif namespace I2n { /** * loads config data from INI style configuration file. * File is processed line by line. * Empty lines are ignored; lines starting with '#' or ';' are ignored too (they are * expected to be comments). * Lines consisting of a bracketed expression like '[group]' start a new group. * Other lines have to be in the form 'key = value' and define a key-value assignment in the current * group. * @param filename path to the config file which is read. * @param[out] result the resulting config data. * @param decoder (optional) decoder for the values. * @return @a true if the file was succesfully parsed. */ bool load_ini_config_file( const std::string& filename, ConfigData& result, const ValueDecoder& decoder ) { std::ifstream f; f.open(filename.c_str()); if (!f.good() ) { return false; } return load_ini_config(f,result); } // eo load_ini_config_file(const std::string&,ConfigData&) /** * loads config data from INI style configuration file. * File is processed line by line. * Empty lines are ignored; lines starting with '#' or ';' are ignored too (they are * expected to be comments). * Lines consisting of a bracketed expression like '[group]' start a new group. * Other lines have to be in the form 'key = value' and define a key-value assignment in the current * group. * @param f istream to read the data from. * @param[out] result the resulting config data. * @param decoder (optional) decoder for the values. * @return @a true if the file was successfully parsed. */ bool load_ini_config( std::istream& f, ConfigData& result, const ValueDecoder& decoder ) { result.clear(); if (!f.good()) { return false; } std::string group(""); while (!f.eof() && f.good()) { std::string line; getline(f,line); trim_mod(line); if (line.empty() || line[0] == '#'|| line[0]==';') { continue; } if (line[0] == '[' && line[line.size()-1] == ']') { // start new group: group= line.substr(1, line.size()-2); result[group]; continue; } std::string key, value; if (pair_split(line,key,value,'=')) { if (decoder) { value= decoder(value); } result[group][key]+= value; } else { // bad line.... //TODO: spit more information?! return false; } } return true; } // eo load_ini_config } // eo namespace I2n