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 | void setEntryComment(const std::string& comment, const std::string& entryName, const std::string& sectionName); |
---|
72 | const std::string& getEntryComment(const std::string& entryName, const std::string& sectionName) const; |
---|
73 | |
---|
74 | // iterate Through Variables with these Functions. |
---|
75 | void firstVar(); |
---|
76 | bool nextVar(); |
---|
77 | |
---|
78 | |
---|
79 | // retrieving functions when iterating. |
---|
80 | const std::string& getCurrentSection() const; |
---|
81 | const std::string& getCurrentName() const; |
---|
82 | const std::string& getCurrentValue() const; |
---|
83 | |
---|
84 | |
---|
85 | // maintenance. |
---|
86 | void debug() const; |
---|
87 | |
---|
88 | |
---|
89 | private: |
---|
90 | void deleteSections(); |
---|
91 | void setFileName(const std::string& fileName); |
---|
92 | |
---|
93 | void setFileComment(); |
---|
94 | void setSectionComment(); |
---|
95 | void setEntryComment(); |
---|
96 | |
---|
97 | std::list<IniSection>::const_iterator getSectionIT(const std::string& sectionName) const; |
---|
98 | std::list<IniSection>::iterator getSectionIT(const std::string& sectionName); |
---|
99 | |
---|
100 | std::list<IniEntry>::const_iterator getEntryIT(const std::string& entryName, const std::string& sectionName = "") const; |
---|
101 | std::list<IniEntry>::iterator getEntryIT(const std::string& entryName, const std::string& sectionName = ""); |
---|
102 | |
---|
103 | private: |
---|
104 | std::string fileName; //!< The name of the File that was parsed. |
---|
105 | std::string comment; //!< A Comment for the header of this File. |
---|
106 | std::list<IniSection> sections; //!< a list of all stored Sections of the Parser |
---|
107 | std::list<IniSection>::iterator currentSection; //!< the current selected Section |
---|
108 | std::list<IniEntry>::iterator currentEntry; //!< the current selected entry (in currentSection) |
---|
109 | |
---|
110 | std::list<std::string> commentList; //!< A list of Comments. (this is for temporary saving of Comments, that are inserted in front of Sections/Entries.) |
---|
111 | |
---|
112 | |
---|
113 | static const std::string emptyString; |
---|
114 | }; |
---|
115 | |
---|
116 | #endif /* _INI_PARSER_H */ |
---|