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