[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 | |
---|
[9880] | 13 | #include <string> |
---|
[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 | */ |
---|
[9880] | 20 | class IniParser |
---|
[4597] | 21 | { |
---|
[9880] | 22 | public: |
---|
| 23 | //////////////////////////////////// |
---|
| 24 | /// A Node for a Ini-Node. The base of all INI-elements. |
---|
| 25 | class Node |
---|
| 26 | { |
---|
| 27 | public: |
---|
| 28 | Node(const std::string& name, const std::string& comment = ""); |
---|
| 29 | virtual ~Node() {}; |
---|
| 30 | const std::string& name() const { return _name; }; |
---|
| 31 | const std::string& comment() const { return _comment; }; |
---|
| 32 | void setName(const std::string& name) { this->_name = name; }; |
---|
| 33 | void setComment(const std::string& comment) { this->_comment = comment; }; |
---|
| 34 | |
---|
| 35 | bool operator==(const std::string& name) const { return _name == name; }; |
---|
| 36 | bool operator==(const Node& node) const { return this->_name == node._name; }; |
---|
| 37 | |
---|
| 38 | virtual void debug() const = 0; |
---|
| 39 | |
---|
[5014] | 40 | private: |
---|
[9880] | 41 | std::string _comment; //!< A Comment that is appendet to the Top of this Node |
---|
| 42 | std::string _name; //!< name of a given Node |
---|
| 43 | }; |
---|
[5945] | 44 | |
---|
[9880] | 45 | //! a class for Entries in the Parser's File's Sections |
---|
| 46 | class Entry : public Node |
---|
| 47 | { |
---|
| 48 | public: |
---|
| 49 | Entry(const std::string& name, const std::string& value = "", const std::string& comment = ""); |
---|
| 50 | const std::string& value() const { return _value; }; |
---|
| 51 | void setValue (const std::string& value) { _value = value; }; |
---|
[4482] | 52 | |
---|
[9880] | 53 | virtual void debug() const; |
---|
| 54 | |
---|
[5014] | 55 | public: |
---|
[9880] | 56 | typedef std::list<Entry> list; |
---|
| 57 | typedef list::iterator iterator; |
---|
| 58 | typedef list::const_iterator const_iterator; |
---|
[4482] | 59 | |
---|
[9880] | 60 | private: |
---|
| 61 | std::string _value; //!< value of a given Entry |
---|
| 62 | }; |
---|
[5945] | 63 | |
---|
[9880] | 64 | //! a clas for Sections in the Parser's file |
---|
| 65 | class Section : public Node |
---|
| 66 | { |
---|
| 67 | public: |
---|
| 68 | Section(const std::string& sectionName, const std::string& comment = ""); |
---|
[4597] | 69 | |
---|
[9880] | 70 | Entry& addEntry(const std::string& entryName, const std::string& value = "", const std::string& comment = ""); |
---|
| 71 | bool editEntry(const std::string& entryName, const std::string& value, bool createMissing = true); |
---|
| 72 | const std::string& getValue(const std::string& entryName, const std::string& defaultValue = "") const; |
---|
[5945] | 73 | |
---|
[9880] | 74 | bool setEntryComment(const std::string& entryName, const std::string& comment); |
---|
| 75 | const std::string& getEntryComment(const std::string& entryName) const; |
---|
[5015] | 76 | |
---|
[9880] | 77 | const Entry::list& entries() const { return _entries; } |
---|
| 78 | Entry* getEntry(const std::string& entryName); |
---|
[5014] | 79 | |
---|
[9880] | 80 | Entry::const_iterator getEntryIt(const std::string& entryName) const; |
---|
| 81 | Entry::iterator begin() { return _entries.begin(); }; |
---|
| 82 | Entry::const_iterator begin() const { return _entries.begin(); }; |
---|
| 83 | Entry::iterator end() { return _entries.end(); }; |
---|
| 84 | Entry::const_iterator end() const { return _entries.end(); }; |
---|
[5014] | 85 | |
---|
[9880] | 86 | void clear(); |
---|
[5015] | 87 | |
---|
[9880] | 88 | virtual void debug() const; |
---|
[5014] | 89 | |
---|
[9880] | 90 | public: |
---|
| 91 | typedef std::list<Section> list; |
---|
| 92 | typedef list::iterator iterator; |
---|
| 93 | typedef list::const_iterator const_iterator; |
---|
[5945] | 94 | |
---|
[9880] | 95 | private: |
---|
| 96 | Entry::list _entries; //!< a list of entries for this section |
---|
| 97 | }; |
---|
[5014] | 98 | |
---|
[9880] | 99 | //! A class for a INI-file. |
---|
| 100 | class Document : public Node |
---|
| 101 | { |
---|
| 102 | public: |
---|
| 103 | Document(const std::string& fileName, const std::string& comment = ""); |
---|
[5945] | 104 | |
---|
[9880] | 105 | Section& addSection(const std::string& sectionName, const std::string& comment = ""); |
---|
| 106 | bool removeSection(const std::string& sectionName); |
---|
| 107 | bool setSectionComment(const std::string& sectionName, const std::string& comment); |
---|
[5014] | 108 | |
---|
[9880] | 109 | const Section::list& sections() const { return _sections; } |
---|
| 110 | Section* getSection(const std::string& sectionName); |
---|
[5934] | 111 | |
---|
[9880] | 112 | Section::const_iterator getSectionIt(const std::string& sectionName) const; |
---|
| 113 | Section::iterator begin() { return _sections.begin(); }; |
---|
| 114 | Section::const_iterator begin() const { return _sections.begin(); }; |
---|
| 115 | Section::iterator end() { return _sections.end(); }; |
---|
| 116 | Section::const_iterator end() const { return _sections.end(); }; |
---|
[5014] | 117 | |
---|
[9880] | 118 | bool addEntry(const std::string& sectionName, const std::string& entryName, const std::string& value = "", const std::string& comment = ""); |
---|
| 119 | bool editEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, bool createMissing = true); |
---|
| 120 | const std::string& getValue(const std::string& sectionName, const std::string& entryName, const std::string& defaultValue = "") const; |
---|
[5946] | 121 | |
---|
[9880] | 122 | bool setEntryComment(const std::string& sectionName, const std::string& entryName, const std::string& comment); |
---|
| 123 | const std::string& getEntryComment(const std::string& sectionName, const std::string& entryName) const; |
---|
[5951] | 124 | |
---|
[9880] | 125 | void clear(); |
---|
[5945] | 126 | |
---|
[9880] | 127 | virtual void debug() const; |
---|
| 128 | |
---|
[5014] | 129 | private: |
---|
[9880] | 130 | Section::list _sections; //!< a list of all stored Sections of the Parser. |
---|
| 131 | }; |
---|
[5945] | 132 | |
---|
[9880] | 133 | public: |
---|
| 134 | IniParser (const std::string& filename = ""); |
---|
| 135 | virtual ~IniParser (); |
---|
[7221] | 136 | |
---|
[9880] | 137 | /** @returns true if the file is opened, false otherwise */ |
---|
| 138 | bool isOpen() const { return !this->_document.sections().empty(); }; |
---|
| 139 | /** @returns the fileName we have opened. */ |
---|
| 140 | const std::string& getFileName() const { return this->_document.name(); }; |
---|
[7221] | 141 | |
---|
[9880] | 142 | /// Read and Write the File |
---|
| 143 | bool readFile(const std::string& fileName, bool keepSettings = false); |
---|
| 144 | bool writeFile(const std::string& fileName) const; |
---|
| 145 | |
---|
| 146 | void setFileComment(const std::string& fileComment); |
---|
| 147 | const std::string& getFileComment() const { return this->_document.comment(); }; |
---|
| 148 | |
---|
| 149 | /// Woring with Sections. |
---|
| 150 | Section& addSection(const std::string& sectionName); |
---|
| 151 | // iterate through sections with these Functions |
---|
| 152 | Section* getSection(const std::string& sectionName) { return this->_document.getSection(sectionName); }; |
---|
| 153 | Section::const_iterator getSectionIt(const std::string& sectionName) const; |
---|
| 154 | |
---|
| 155 | Section::iterator begin() { return this->_document.begin(); }; |
---|
| 156 | Section::const_iterator begin() const { return this->_document.begin(); }; |
---|
| 157 | Section::iterator end() { return this->_document.end(); }; |
---|
| 158 | Section::const_iterator end() const { return this->_document.end(); }; |
---|
| 159 | |
---|
| 160 | void setSectionComment(const std::string& sectionName, const std::string& comment); |
---|
| 161 | const std::string& getSectionsComment(const std::string& sectionNane) const; |
---|
| 162 | |
---|
| 163 | /// Working on Entries. (globally) |
---|
| 164 | bool addEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, const std::string& comment); |
---|
| 165 | const std::string& getValue(const std::string& sectionNmae, const std::string& entryName, const std::string& defaultValue = "") const; |
---|
| 166 | bool editEntry(const std::string& sectionName, const std::string& entryName, const std::string& value, bool createMissing = true); |
---|
| 167 | |
---|
| 168 | void setEntryComment(const std::string& sectionName, const std::string& entryName, const std::string& comment); |
---|
| 169 | const std::string& getEntryComment(const std::string& sectionName, const std::string& entryName) const; |
---|
| 170 | |
---|
| 171 | // maintenance. |
---|
| 172 | void debug() const; |
---|
| 173 | |
---|
| 174 | private: |
---|
| 175 | void setNodeComment(Node* node, std::list<std::string>* comments); |
---|
| 176 | private: |
---|
| 177 | std::string _fileName; |
---|
| 178 | Document _document; |
---|
| 179 | |
---|
| 180 | static const std::string _emptyString; //!< Just an Empty String that will be returned if nothing else is found. |
---|
[2064] | 181 | }; |
---|
| 182 | |
---|
[3224] | 183 | #endif /* _INI_PARSER_H */ |
---|