| 1 | /*! |
|---|
| 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 | */ |
|---|
| 7 | |
|---|
| 8 | #ifndef _INI_PARSER_H |
|---|
| 9 | #define _INI_PARSER_H |
|---|
| 10 | |
|---|
| 11 | #define PARSELINELENGHT 512 //!< how many chars to read at once |
|---|
| 12 | #ifndef NULL |
|---|
| 13 | #define NULL 0x0 //!< NULL |
|---|
| 14 | #endif |
|---|
| 15 | |
|---|
| 16 | #include <list> |
|---|
| 17 | #include <assert.h> |
|---|
| 18 | |
|---|
| 19 | //! ini-file parser |
|---|
| 20 | /** |
|---|
| 21 | * This class can be used to load an initializer file and parse it's contents for variablename=value pairs. |
|---|
| 22 | */ |
|---|
| 23 | class IniParser |
|---|
| 24 | { |
|---|
| 25 | private: |
|---|
| 26 | //////////////////////////////////// |
|---|
| 27 | //! a struct for Entries in the Parser's File's Sections |
|---|
| 28 | struct IniEntry |
|---|
| 29 | { |
|---|
| 30 | char* comment; //!< A Comment that is appendet to the Top of this Entry. |
|---|
| 31 | char* name; //!< name of a given Entry |
|---|
| 32 | char* value; //!< value of a given Entry |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | //! a struct for Sections in the Parser's file |
|---|
| 36 | struct IniSection |
|---|
| 37 | { |
|---|
| 38 | char* comment; //!< A Comment that is appendet to the Top of this Section. |
|---|
| 39 | char* name; //!< name of a given section |
|---|
| 40 | std::list<IniEntry> entries; //!< a list of entries for this section |
|---|
| 41 | }; |
|---|
| 42 | //////////////////////////////////// |
|---|
| 43 | |
|---|
| 44 | public: |
|---|
| 45 | IniParser (const char* filename = NULL); |
|---|
| 46 | ~IniParser (); |
|---|
| 47 | |
|---|
| 48 | /** @returns true if the file is opened, false otherwise*/ |
|---|
| 49 | bool isOpen() const { return (this->fileName != NULL)? true : false; }; |
|---|
| 50 | /** @returns the fileName we have opened. */ |
|---|
| 51 | const char* getFileName() const { return this->fileName; }; |
|---|
| 52 | |
|---|
| 53 | bool readFile(const char* fileName); |
|---|
| 54 | bool writeFile(const char* fileName) const; |
|---|
| 55 | |
|---|
| 56 | void setFileComment(const char* fileComment); |
|---|
| 57 | const char* getFileComment() const { return this->comment; }; |
|---|
| 58 | |
|---|
| 59 | bool addSection(const char* sectionName); |
|---|
| 60 | bool getSection(const char* sectionName); |
|---|
| 61 | void setSectionComment(const char* comment, const char* sectionName); |
|---|
| 62 | const char* getSectionComment(const char* sectionNane) const; |
|---|
| 63 | |
|---|
| 64 | // iterate through sections with these Functions |
|---|
| 65 | void firstSection(); |
|---|
| 66 | const char* nextSection(); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | bool addVar(const char* entryName, const char* value, const char* sectionName = NULL); |
|---|
| 70 | bool editVar(const char* entryName, const char* value, const char* sectionName = NULL); |
|---|
| 71 | const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; |
|---|
| 72 | void setEntryComment(const char* comment, const char* entryName, const char* sectionName); |
|---|
| 73 | const char* getEntryComment(const char* entryName, const char* sectionName) const; |
|---|
| 74 | |
|---|
| 75 | // iterate Through Variables with these Functions. |
|---|
| 76 | void firstVar(); |
|---|
| 77 | bool nextVar(); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | // retrieving functions when iterating. |
|---|
| 81 | const char* getCurrentSection() const; |
|---|
| 82 | const char* getCurrentName() const; |
|---|
| 83 | const char* getCurrentValue() const; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | // maintenance. |
|---|
| 87 | void debug() const; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | private: |
|---|
| 91 | void deleteSections(); |
|---|
| 92 | void setFileName(const char* fileName); |
|---|
| 93 | |
|---|
| 94 | void setFileComment(); |
|---|
| 95 | void setSectionComment(); |
|---|
| 96 | void setEntryComment(); |
|---|
| 97 | |
|---|
| 98 | std::list<IniSection>::const_iterator getSectionIT(const char* sectionName) const; |
|---|
| 99 | std::list<IniSection>::iterator getSectionIT(const char* sectionName); |
|---|
| 100 | |
|---|
| 101 | std::list<IniEntry>::const_iterator getEntryIT(const char* entryName, const char* sectionName = NULL) const; |
|---|
| 102 | std::list<IniEntry>::iterator getEntryIT(const char* entryName, const char* sectionName = NULL); |
|---|
| 103 | |
|---|
| 104 | private: |
|---|
| 105 | char* fileName; //!< The name of the File that was parsed. |
|---|
| 106 | char* comment; //!< A Comment for the header of this File. |
|---|
| 107 | std::list<IniSection> sections; //!< a list of all stored Sections of the Parser |
|---|
| 108 | std::list<IniSection>::iterator currentSection; //!< the current selected Section |
|---|
| 109 | std::list<IniEntry>::iterator currentEntry; //!< the current selected entry (in currentSection) |
|---|
| 110 | |
|---|
| 111 | std::list<char*> commentList; //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.) |
|---|
| 112 | }; |
|---|
| 113 | |
|---|
| 114 | #endif /* _INI_PARSER_H */ |
|---|