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