[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 | { |
---|
[5945] | 29 | char* comment; //!< A Comment that is appendet to the Top of this Entry. |
---|
| 30 | char* name; //!< name of a given Entry |
---|
| 31 | char* value; //!< value of a given Entry |
---|
[5014] | 32 | }; |
---|
[5945] | 33 | |
---|
[5017] | 34 | //! a struct for Sections in the Parser's file |
---|
[5014] | 35 | struct IniSection |
---|
| 36 | { |
---|
[5945] | 37 | char* comment; //!< A Comment that is appendet to the Top of this Section. |
---|
| 38 | char* name; //!< name of a given section |
---|
| 39 | std::list<IniEntry> entries; //!< a list of entries for this section |
---|
[5014] | 40 | }; |
---|
| 41 | //////////////////////////////////// |
---|
[4482] | 42 | |
---|
[5014] | 43 | public: |
---|
| 44 | IniParser (const char* filename = NULL); |
---|
| 45 | ~IniParser (); |
---|
[4482] | 46 | |
---|
[5945] | 47 | /** @returns true if the file is opened, false otherwise*/ |
---|
| 48 | bool isOpen() const { return (this->fileName != NULL)? true : false; }; |
---|
| 49 | /** @returns the fileName we have opened. */ |
---|
| 50 | const char* getFileName() const { return this->fileName; }; |
---|
| 51 | |
---|
[5020] | 52 | bool readFile(const char* fileName); |
---|
[5936] | 53 | bool writeFile(const char* fileName) const; |
---|
[4597] | 54 | |
---|
[5945] | 55 | void setFileComment(const char* fileComment); |
---|
| 56 | const char* getFileComment() const { return this->comment; }; |
---|
| 57 | |
---|
[5020] | 58 | bool addSection(const char* sectionName); |
---|
[5015] | 59 | bool getSection(const char* sectionName); |
---|
[5945] | 60 | void setSectionComment(const char* comment, const char* sectionName); |
---|
| 61 | const char* getSectionComment(const char* sectionNane) const; |
---|
[5015] | 62 | |
---|
[5945] | 63 | // iterate through sections with these Functions |
---|
[5936] | 64 | void firstSection(); |
---|
[5014] | 65 | const char* nextSection(); |
---|
| 66 | |
---|
| 67 | |
---|
[5020] | 68 | bool addVar(const char* entryName, const char* value, const char* sectionName = NULL); |
---|
[5014] | 69 | const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; |
---|
[5952] | 70 | void setEntryComment(const char* comment, const char* entryName, const char* sectionName); |
---|
[5945] | 71 | const char* getEntryComment(const char* entryName, const char* sectionName) const; |
---|
[5015] | 72 | |
---|
[5945] | 73 | // iterate Through Variables with these Functions. |
---|
[5936] | 74 | void firstVar(); |
---|
[5014] | 75 | bool nextVar(); |
---|
| 76 | |
---|
[5945] | 77 | |
---|
| 78 | // retrieving functions when iterating. |
---|
[5935] | 79 | const char* getCurrentSection() const; |
---|
| 80 | const char* getCurrentName() const; |
---|
| 81 | const char* getCurrentValue() const; |
---|
[5014] | 82 | |
---|
[5945] | 83 | |
---|
| 84 | // maintenance. |
---|
[5014] | 85 | void debug() const; |
---|
| 86 | |
---|
[5934] | 87 | |
---|
[5014] | 88 | private: |
---|
| 89 | void deleteSections(); |
---|
[5031] | 90 | void setFileName(const char* fileName); |
---|
[5014] | 91 | |
---|
[5946] | 92 | void setFileComment(); |
---|
| 93 | void setSectionComment(); |
---|
| 94 | void setEntryComment(); |
---|
| 95 | |
---|
[5945] | 96 | std::list<IniSection>::const_iterator getSectionIT(const char* sectionName) const; |
---|
[5951] | 97 | std::list<IniSection>::iterator getSectionIT(const char* sectionName); |
---|
| 98 | |
---|
[5945] | 99 | std::list<IniEntry>::const_iterator getEntryIT(const char* entryName, const char* sectionName = NULL) const; |
---|
[5951] | 100 | std::list<IniEntry>::iterator getEntryIT(const char* entryName, const char* sectionName = NULL); |
---|
[5945] | 101 | |
---|
[5014] | 102 | private: |
---|
[5933] | 103 | char* fileName; //!< The name of the File that was parsed. |
---|
[5945] | 104 | char* comment; //!< A Comment for the header of this File. |
---|
[5933] | 105 | std::list<IniSection> sections; //!< a list of all stored Sections of the Parser |
---|
| 106 | std::list<IniSection>::iterator currentSection; //!< the current selected Section |
---|
| 107 | std::list<IniEntry>::iterator currentEntry; //!< the current selected entry (in currentSection) |
---|
[5945] | 108 | |
---|
| 109 | std::list<char*> commentList; //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.) |
---|
[2064] | 110 | }; |
---|
| 111 | |
---|
[3224] | 112 | #endif /* _INI_PARSER_H */ |
---|