Dies ist eine alte Version des Dokuments!
Configuration File Parser
Overview
cfg_parser is a robust configuration file parser written in C++. It can read and write configuration files preserving any comments made inside the file. It supports escaped characters, quoted text and infinite line length (a whole line must fit into an STL string).
Features
- keyword = value
- [Sections]
- # comments
- [Sections] # with comments
- keyword = value # pairs with comments
- keyword = „values with spaces, newlines \n, embedded quotes \“ and all other escape characters supported by printf„
- sloppy parsing:
keyword= valueis accepted as well askeyword=valueorkeyword value(withouth '=') - displays parsing errors in a human friendly format (including filename, line number and line contents in question)
Download
Code Example
#include <iostream> #include <string> #include "cfg_parser.h" using namespace std; int main(int argc, char *argv[]) { // First we need to initialize a new configuration ConfigParser cfg("test.cfg"); // write everything into another config file cfg.writeFile("test2.cfg"); // get value as integer of keyword 'hier' from the global section int i = cfg.getInt("hier"); cout << "global hier=" << i << endl; // get value as integer of keyword 'hier' from section: mysection with default value = "" (which will be converted by atoi()) i = cfg.getInt("hier", "", "mysection"); cout << "section hier=" << i << endl; return 0; }
Documentation
Only relevant members are shown!
class ConfigParser { public: ConfigParser(const std::string& in_file = ""); ~ConfigParser(); // loads the given configuration file int readFile(const std::string& in_file); // writes the current configuration to the given file. I file not given, write back into the inputfile int writeFile(const std::string& out_file = ""); // returns filname of loaded file void getFilename(std::string& in_file); // empty lines in config file are written back to the out_file if value = 1. (default = 0) void preserveEmptyLines(int value); // return variable from configuration (if non-existent, value remains unchanged) void getVar(const std::string& keyword, std::string& value, const std::string& section = ""); // sets variable in configuration (section and/or variable are added if non-existent) void setVar(const std::string& keyword, const std::string& value, const std::string& section = "", const std::string& comment = ""); // deletes variable from configuration void delVar(const std::string& keyword, const std::string& section = ""); // returns integer if found, supplied default_value else int getInt(const std::string& keyword, const std::string& default_value = "", const std::string& section = ""); // returns long if found, supplied default_value else long getLong(const std::string& option, const std::string& default_value = "", const std::string& section = ""); // returns float if found, supplied default_value else float getFloat(const std::string& option, const std::string& default_value = "", const std::string& section = ""); // returns double if found, supplied default_value else double getDouble(const std::string& option, const std::string& default_value = "", const std::string& section = ""); // returns string if found, supplied default_value else std::string getString(const std::string& option, const std::string& default_value = "", const std::string& section = ""); // adds a section to the configuration void addSection(const std::string& section, const std::string& comment = ""); // deletes a section void delSection(const std::string& section); // displays current configuration content using cout void debug(); };
Example Config File
The following file is read-in correctly.
# Test file for cfg_parser # lets begin with a comment.. well we already did :-) # simple stuff keyword = value bla=123 test 234 text = "my loooong text including nested \" ! and line\nbreaks\n." # little bit more complicated bal = asdfasdfas text2="blaaaldflkadf"#evil comment text3 = "lets try a note that lasts for more than a line" # and including a comment :-) # another comment # yet another comment adf = adfasdf #bla a dffff [mysection] hier = 1 hier = 2 [ a ] adf adsf [ asdf d] a asdfdf