Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| configparser [04.01.2006 15:12 (vor 21 Jahren)] – cwacha | configparser [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | ===== Configuration File Parser ===== | ||
| - | === Overview === | ||
| - | // | ||
| - | |||
| - | === Features === | ||
| - | * keyword = value | ||
| - | * [Sections] | ||
| - | * # comments | ||
| - | * [Sections] # with comments | ||
| - | * keyword = value # pairs with comments | ||
| - | * keyword = " | ||
| - | * sloppy parsing: '' | ||
| - | * displays parsing errors in a human friendly format (including filename, linenumber and line contents in question) | ||
| - | |||
| - | === Download === | ||
| - | * {{projects: | ||
| - | |||
| - | === Code Example === | ||
| - | |||
| - | <code cpp> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #include " | ||
| - | |||
| - | using namespace std; | ||
| - | |||
| - | int main(int argc, char *argv[]) { | ||
| - | // First we need to initialize a new configuration | ||
| - | ConfigParser cfg(" | ||
| - | |||
| - | // write everything into another config file | ||
| - | cfg.writeFile(" | ||
| - | |||
| - | // get value as integer of keyword ' | ||
| - | int i = cfg.getInt(" | ||
| - | cout << " | ||
| - | |||
| - | // get value as integer of keyword ' | ||
| - | i = cfg.getInt(" | ||
| - | cout << " | ||
| - | |||
| - | | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | === Documentation === | ||
| - | |||
| - | Only relevant members are shown! | ||
| - | |||
| - | <code cpp> | ||
| - | class ConfigParser { | ||
| - | public: | ||
| - | ConfigParser(const std:: | ||
| - | ~ConfigParser(); | ||
| - | |||
| - | // loads the given configuration file | ||
| - | int readFile(const std:: | ||
| - | // writes the current configuration to the given file. I file not given, write back into the inputfile | ||
| - | int writeFile(const std:: | ||
| - | // returns filname of loaded file | ||
| - | void getFilename(std:: | ||
| - | // 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, | ||
| - | void getVar(const std:: | ||
| - | // sets variable in configuration (section and/or variable are added if non-existent) | ||
| - | void setVar(const std:: | ||
| - | // deletes variable from configuration | ||
| - | void delVar(const std:: | ||
| - | |||
| - | // returns integer if found, supplied default_value else | ||
| - | int getInt(const std:: | ||
| - | // returns long if found, supplied default_value else | ||
| - | long getLong(const std:: | ||
| - | // returns float if found, supplied default_value else | ||
| - | float getFloat(const std:: | ||
| - | // returns double if found, supplied default_value else | ||
| - | double getDouble(const std:: | ||
| - | // returns string if found, supplied default_value else | ||
| - | std:: | ||
| - | |||
| - | |||
| - | // adds a section to the configuration | ||
| - | void addSection(const std:: | ||
| - | // deletes a section | ||
| - | void delSection(const std:: | ||
| - | |||
| - | // displays current configuration content using cout | ||
| - | void debug(); | ||
| - | }; | ||
| - | </ | ||
| - | |||
| - | === Example Config File === | ||
| - | |||
| - | < | ||
| - | # 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 | ||
| - | | ||
| - | | ||
| - | text3 | ||
| - | that lasts | ||
| - | for | ||
| - | more | ||
| - | than | ||
| - | a line" | ||
| - | # another comment | ||
| - | |||
| - | |||
| - | # yet another comment | ||
| - | |||
| - | adf = adfasdf #bla a dffff | ||
| - | |||
| - | [mysection] | ||
| - | hier = 1 | ||
| - | hier = 2 | ||
| - | |||
| - | [ a ] | ||
| - | adf adsf | ||
| - | |||
| - | [ asdf d] | ||
| - | a asdfdf | ||
| - | |||
| - | </ | ||