Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
| configparser [05.01.2006 23:48 (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, line number and line contents in question) | ||
| - | |||
| - | === Download === | ||
| - | * {{projects: | ||
| - | |||
| - | === Code Example === | ||
| - | |||
| - | <code cpp> | ||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | #include " | ||
| - | |||
| - | using namespace std; | ||
| - | |||
| - | int main(int argc, char *argv[]) { | ||
| - | ConfigParser cfg(" | ||
| - | |||
| - | cfg.debug(); | ||
| - | cfg.writeFile(" | ||
| - | |||
| - | int i = cfg.get(" | ||
| - | cout << " | ||
| - | i = cfg.get(" | ||
| - | cout << " | ||
| - | |||
| - | string value = cfg.get< | ||
| - | cout << " | ||
| - | value.clear(); | ||
| - | |||
| - | // alternative way | ||
| - | string value2; | ||
| - | cfg.getVar(" | ||
| - | cout << " | ||
| - | |||
| - | // C strings need an extra function to return a const pointer to the saved element | ||
| - | // REMARK: the const char* is valid only as long as the keyword exists!! | ||
| - | test = cfg.getCString(" | ||
| - | test2 = cfg.getCString(" | ||
| - | test3 = cfg.getCString(" | ||
| - | printf(" | ||
| - | |||
| - | // | ||
| - | cfg.getCString(" | ||
| - | cfg.getCString(" | ||
| - | cfg.getCString(" | ||
| - | printf(" | ||
| - | |||
| - | |||
| - | // Creating a completely new config file | ||
| - | ConfigParser t; | ||
| - | t.set< | ||
| - | t.set(" | ||
| - | t.set(" | ||
| - | t.set(" | ||
| - | char* text = " | ||
| - | t.set(" | ||
| - | t.writeFile(" | ||
| - | |||
| - | ConfigParser b(" | ||
| - | b.debug(); | ||
| - | |||
| - | return 0; | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | === 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 enable = true. | ||
| - | // this function has to be called before readFile() since it defaults to false | ||
| - | void preserveEmptyLines(bool enable = true); | ||
| - | |||
| - | |||
| - | // return variable from configuration (if non-existent, | ||
| - | void getVar(const std:: | ||
| - | // sets variable in configuration (section and/or variable are added if non-existent) | ||
| - | // if keyword is " | ||
| - | void setVar(const std:: | ||
| - | // deletes variable from configuration | ||
| - | void delVar(const std:: | ||
| - | |||
| - | // returns value converted to type T if found, default_value else | ||
| - | // Remark: does NOT work with char*. Use std::string or getCString() instead | ||
| - | template < | ||
| - | |||
| - | // set value of keyword (internally converted to string). (works with char* too) | ||
| - | template < | ||
| - | |||
| - | // returns const char* if found c_str() of supplied default_value else. Remark that char* is only valid as long as keyword exists! | ||
| - | const char* getCString(const std:: | ||
| - | |||
| - | // adds a section to the configuration (if it does not yet exist) | ||
| - | void addSection(const std:: | ||
| - | // deletes a section (if it exists) | ||
| - | void delSection(const std:: | ||
| - | |||
| - | // 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 | ||
| - | | ||
| - | | ||
| - | 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 | ||
| - | |||
| - | </ | ||