[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 |
---|
| 12 | |
---|
[8330] | 13 | #include "filesys/file.h" |
---|
[5933] | 14 | #include <list> |
---|
[2064] | 15 | |
---|
[2141] | 16 | //! ini-file parser |
---|
| 17 | /** |
---|
[5014] | 18 | * This class can be used to load an initializer file and parse it's contents for variablename=value pairs. |
---|
| 19 | */ |
---|
[7661] | 20 | class IniParser : public File |
---|
[4597] | 21 | { |
---|
[5014] | 22 | private: |
---|
| 23 | //////////////////////////////////// |
---|
[5017] | 24 | //! a struct for Entries in the Parser's File's Sections |
---|
[5014] | 25 | struct IniEntry |
---|
| 26 | { |
---|
[7221] | 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 |
---|
[5014] | 30 | }; |
---|
[5945] | 31 | |
---|
[5017] | 32 | //! a struct for Sections in the Parser's file |
---|
[5014] | 33 | struct IniSection |
---|
| 34 | { |
---|
[7221] | 35 | std::string comment; //!< A Comment that is appendet to the Top of this Section. |
---|
| 36 | std::string name; //!< name of a given section |
---|
[5945] | 37 | std::list<IniEntry> entries; //!< a list of entries for this section |
---|
[5014] | 38 | }; |
---|
| 39 | //////////////////////////////////// |
---|
[4482] | 40 | |
---|
[5014] | 41 | public: |
---|
[7221] | 42 | IniParser (const std::string& filename = ""); |
---|
[6981] | 43 | virtual ~IniParser (); |
---|
[4482] | 44 | |
---|
[5945] | 45 | /** @returns true if the file is opened, false otherwise*/ |
---|
[7661] | 46 | bool isOpen() const { return true; } ///HACK //(this->fileName.empty()) ? false : true; }; |
---|
[5945] | 47 | /** @returns the fileName we have opened. */ |
---|
[7221] | 48 | const std::string& getFileName() const { return this->fileName; }; |
---|
[5945] | 49 | |
---|
[7221] | 50 | bool readFile(const std::string& fileName); |
---|
| 51 | bool writeFile(const std::string& fileName) const; |
---|
[4597] | 52 | |
---|
[7221] | 53 | void setFileComment(const std::string& fileComment); |
---|
| 54 | const std::string& getFileComment() const { return this->comment; }; |
---|
[5945] | 55 | |
---|
[7221] | 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; |
---|
[5015] | 60 | |
---|
[5945] | 61 | // iterate through sections with these Functions |
---|
[5936] | 62 | void firstSection(); |
---|
[7221] | 63 | const std::string& nextSection(); |
---|
[5014] | 64 | |
---|
| 65 | |
---|
[7221] | 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; |
---|
[7661] | 68 | bool IniParser::editVar(const std::string& entryName, const std::string& value, const std::string& sectionName = "", bool createMissing = true); |
---|
[7221] | 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; |
---|
[5015] | 71 | |
---|
[5945] | 72 | // iterate Through Variables with these Functions. |
---|
[5936] | 73 | void firstVar(); |
---|
[5014] | 74 | bool nextVar(); |
---|
| 75 | |
---|
[5945] | 76 | |
---|
| 77 | // retrieving functions when iterating. |
---|
[7221] | 78 | const std::string& getCurrentSection() const; |
---|
| 79 | const std::string& getCurrentName() const; |
---|
| 80 | const std::string& getCurrentValue() const; |
---|
[5014] | 81 | |
---|
[5945] | 82 | |
---|
| 83 | // maintenance. |
---|
[5014] | 84 | void debug() const; |
---|
| 85 | |
---|
[5934] | 86 | |
---|
[5014] | 87 | private: |
---|
| 88 | void deleteSections(); |
---|
[7221] | 89 | void setFileName(const std::string& fileName); |
---|
[5014] | 90 | |
---|
[5946] | 91 | void setFileComment(); |
---|
| 92 | void setSectionComment(); |
---|
| 93 | void setEntryComment(); |
---|
| 94 | |
---|
[7221] | 95 | std::list<IniSection>::const_iterator getSectionIT(const std::string& sectionName) const; |
---|
| 96 | std::list<IniSection>::iterator getSectionIT(const std::string& sectionName); |
---|
[5951] | 97 | |
---|
[7221] | 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 = ""); |
---|
[5945] | 100 | |
---|
[5014] | 101 | private: |
---|
[7221] | 102 | std::string fileName; //!< The name of the File that was parsed. |
---|
| 103 | std::string comment; //!< A Comment for the header of this File. |
---|
[5933] | 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) |
---|
[5945] | 107 | |
---|
[7221] | 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; |
---|
[2064] | 112 | }; |
---|
| 113 | |
---|
[3224] | 114 | #endif /* _INI_PARSER_H */ |
---|