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