| 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 <string> |
|---|
| 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 | std::string comment; //!< A Comment that is appendet to the Top of this Entry. |
|---|
| 31 | std::string name; //!< name of a given Entry |
|---|
| 32 | std::string value; //!< value of a given Entry |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | //! a struct for Sections in the Parser's file |
|---|
| 36 | struct IniSection |
|---|
| 37 | { |
|---|
| 38 | std::string comment; //!< A Comment that is appendet to the Top of this Section. |
|---|
| 39 | std::string 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 std::string& filename = ""); |
|---|
| 46 | virtual ~IniParser (); |
|---|
| 47 | |
|---|
| 48 | /** @returns true if the file is opened, false otherwise*/ |
|---|
| 49 | bool isOpen() const { return (!this->fileName.empty())? true : false; }; |
|---|
| 50 | /** @returns the fileName we have opened. */ |
|---|
| 51 | const std::string& getFileName() const { return this->fileName; }; |
|---|
| 52 | |
|---|
| 53 | bool readFile(const std::string& fileName); |
|---|
| 54 | bool writeFile(const std::string& fileName) const; |
|---|
| 55 | |
|---|
| 56 | void setFileComment(const std::string& fileComment); |
|---|
| 57 | const std::string& getFileComment() const { return this->comment; }; |
|---|
| 58 | |
|---|
| 59 | bool addSection(const std::string& sectionName); |
|---|
| 60 | bool getSection(const std::string& sectionName); |
|---|
| 61 | void setSectionComment(const std::string& comment, const std::string& sectionName); |
|---|
| 62 | const std::string& getSectionComment(const std::string& sectionNane) const; |
|---|
| 63 | |
|---|
| 64 | // iterate through sections with these Functions |
|---|
| 65 | void firstSection(); |
|---|
| 66 | const std::string& nextSection(); |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | bool addVar(const std::string& entryName, const std::string& value, const std::string& sectionName = "" ); |
|---|
| 70 | const std::string& getVar(const std::string& entryName, const std::string& sectionName, const std::string& defaultValue = "") const; |
|---|
| 71 | bool IniParser::editVar(const std::string& entryName, const std::string& value, const std::string& sectionName = ""); |
|---|
| 72 | void setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName); |
|---|
| 73 | const std::string& getEntryComment(const std::string& entryName, const std::string& 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 std::string& getCurrentSection() const; |
|---|
| 82 | const std::string& getCurrentName() const; |
|---|
| 83 | const std::string& getCurrentValue() const; |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | // maintenance. |
|---|
| 87 | void debug() const; |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | private: |
|---|
| 91 | void deleteSections(); |
|---|
| 92 | void setFileName(const std::string& fileName); |
|---|
| 93 | |
|---|
| 94 | void setFileComment(); |
|---|
| 95 | void setSectionComment(); |
|---|
| 96 | void setEntryComment(); |
|---|
| 97 | |
|---|
| 98 | std::list<IniSection>::const_iterator getSectionIT(const std::string& sectionName) const; |
|---|
| 99 | std::list<IniSection>::iterator getSectionIT(const std::string& sectionName); |
|---|
| 100 | |
|---|
| 101 | std::list<IniEntry>::const_iterator getEntryIT(const std::string& entryName, const std::string& sectionName = "") const; |
|---|
| 102 | std::list<IniEntry>::iterator getEntryIT(const std::string& entryName, const std::string& sectionName = ""); |
|---|
| 103 | |
|---|
| 104 | private: |
|---|
| 105 | std::string fileName; //!< The name of the File that was parsed. |
|---|
| 106 | std::string 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<std::string> commentList; //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.) |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | static const std::string emptyString; |
|---|
| 115 | }; |
|---|
| 116 | |
|---|
| 117 | #endif /* _INI_PARSER_H */ |
|---|