[2064] | 1 | /*! |
---|
[5014] | 2 | * @file ini_parser.h |
---|
| 3 | * A small ini file parser |
---|
| 4 | * |
---|
| 5 | * Can be used to find a defined [Section] in an ini file and get the VarName = Value entries |
---|
| 6 | */ |
---|
[4597] | 7 | |
---|
[3224] | 8 | #ifndef _INI_PARSER_H |
---|
| 9 | #define _INI_PARSER_H |
---|
[2064] | 10 | |
---|
[5014] | 11 | #define PARSELINELENGHT 512 //!< how many chars to read at once |
---|
[5031] | 12 | #ifndef NULL |
---|
[5933] | 13 | #define NULL 0x0 //!< NULL |
---|
[5031] | 14 | #endif |
---|
[5014] | 15 | |
---|
[5933] | 16 | #include <list> |
---|
[2064] | 17 | |
---|
[2141] | 18 | //! ini-file parser |
---|
| 19 | /** |
---|
[5014] | 20 | * This class can be used to load an initializer file and parse it's contents for variablename=value pairs. |
---|
| 21 | */ |
---|
[5031] | 22 | class IniParser |
---|
[4597] | 23 | { |
---|
[5014] | 24 | private: |
---|
| 25 | //////////////////////////////////// |
---|
[5017] | 26 | //! a struct for Entries in the Parser's File's Sections |
---|
[5014] | 27 | struct IniEntry |
---|
| 28 | { |
---|
[5017] | 29 | char* name; //!< name of a given Entry |
---|
| 30 | char* value; //!< value of a given Entry |
---|
[5014] | 31 | }; |
---|
[5017] | 32 | //! a struct for Sections in the Parser's file |
---|
[5014] | 33 | struct IniSection |
---|
| 34 | { |
---|
[5017] | 35 | char* name; //!< name of a given section |
---|
[5933] | 36 | std::list<IniEntry> entries; //!< a list of entries for this section |
---|
[5014] | 37 | }; |
---|
| 38 | //////////////////////////////////// |
---|
[4482] | 39 | |
---|
[5014] | 40 | public: |
---|
| 41 | IniParser (const char* filename = NULL); |
---|
| 42 | ~IniParser (); |
---|
[4482] | 43 | |
---|
[5020] | 44 | bool readFile(const char* fileName); |
---|
| 45 | bool writeFile(const char* fileName); |
---|
[4597] | 46 | |
---|
[5020] | 47 | bool addSection(const char* sectionName); |
---|
[5015] | 48 | bool getSection(const char* sectionName); |
---|
| 49 | |
---|
| 50 | void getFirstSection(); |
---|
[5014] | 51 | const char* nextSection(); |
---|
| 52 | |
---|
[5015] | 53 | /** @returns true if the file is opened, false otherwise*/ |
---|
[5933] | 54 | bool isOpen() const { return (this->fileName != NULL)? true : false; }; |
---|
[5031] | 55 | /** @returns the fileName we have opened. */ |
---|
| 56 | const char* getFileName() const { return this->fileName; }; |
---|
[5014] | 57 | |
---|
[5020] | 58 | bool addVar(const char* entryName, const char* value, const char* sectionName = NULL); |
---|
[5014] | 59 | const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; |
---|
[5015] | 60 | |
---|
| 61 | void getFirstVar(); |
---|
[5014] | 62 | bool nextVar(); |
---|
| 63 | |
---|
[5015] | 64 | /** @returns the name of the Current selected Section */ |
---|
| 65 | const char* getCurrentSection() const { return (this->currentSection!=NULL)?this->currentSection->name:NULL; }; |
---|
| 66 | /** @returns the current entries Name, or NULL if we havn't selected a Entry */ |
---|
| 67 | const char* getCurrentName() const { return (this->currentEntry!=NULL)?this->currentEntry->name:NULL; }; |
---|
| 68 | /** @returns the current entries Value, or NULL if we havn't selected a Entry */ |
---|
| 69 | const char* getCurrentValue() const { return (this->currentEntry!=NULL)?this->currentEntry->value:NULL; }; |
---|
[5014] | 70 | |
---|
| 71 | void debug() const; |
---|
| 72 | |
---|
| 73 | private: |
---|
| 74 | void deleteSections(); |
---|
[5031] | 75 | void setFileName(const char* fileName); |
---|
[5014] | 76 | |
---|
| 77 | private: |
---|
[5933] | 78 | char* fileName; //!< The name of the File that was parsed. |
---|
| 79 | std::list<IniSection> sections; //!< a list of all stored Sections of the Parser |
---|
| 80 | std::list<IniSection>::iterator currentSection; //!< the current selected Section |
---|
| 81 | std::list<IniEntry>::iterator currentEntry; //!< the current selected entry (in currentSection) |
---|
[2064] | 82 | }; |
---|
| 83 | |
---|
[3224] | 84 | #endif /* _INI_PARSER_H */ |
---|