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 | |
---|
13 | #include "base_object.h" |
---|
14 | |
---|
15 | // FORWARD DEFINITION // |
---|
16 | template<class T> class tList; |
---|
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 : public BaseObject |
---|
23 | { |
---|
24 | private: |
---|
25 | //////////////////////////////////// |
---|
26 | //! a struct for Entries in the Parser's File's Sections |
---|
27 | struct IniEntry |
---|
28 | { |
---|
29 | char* name; //!< name of a given Entry |
---|
30 | char* value; //!< value of a given Entry |
---|
31 | }; |
---|
32 | //! a struct for Sections in the Parser's file |
---|
33 | struct IniSection |
---|
34 | { |
---|
35 | char* name; //!< name of a given section |
---|
36 | tList<IniEntry>* entries; //!< a list of entries for this section |
---|
37 | }; |
---|
38 | //////////////////////////////////// |
---|
39 | |
---|
40 | public: |
---|
41 | IniParser (const char* filename = NULL); |
---|
42 | ~IniParser (); |
---|
43 | |
---|
44 | bool readFile(const char* fileName); |
---|
45 | bool writeFile(const char* fileName); |
---|
46 | |
---|
47 | bool addSection(const char* sectionName); |
---|
48 | bool getSection(const char* sectionName); |
---|
49 | |
---|
50 | void getFirstSection(); |
---|
51 | const char* nextSection(); |
---|
52 | |
---|
53 | /** @returns true if the file is opened, false otherwise*/ |
---|
54 | bool isOpen() const { return (sections != NULL)?true:false; }; |
---|
55 | |
---|
56 | bool addVar(const char* entryName, const char* value, const char* sectionName = NULL); |
---|
57 | const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; |
---|
58 | |
---|
59 | void getFirstVar(); |
---|
60 | bool nextVar(); |
---|
61 | |
---|
62 | /** @returns the name of the Current selected Section */ |
---|
63 | const char* getCurrentSection() const { return (this->currentSection!=NULL)?this->currentSection->name:NULL; }; |
---|
64 | /** @returns the current entries Name, or NULL if we havn't selected a Entry */ |
---|
65 | const char* getCurrentName() const { return (this->currentEntry!=NULL)?this->currentEntry->name:NULL; }; |
---|
66 | /** @returns the current entries Value, or NULL if we havn't selected a Entry */ |
---|
67 | const char* getCurrentValue() const { return (this->currentEntry!=NULL)?this->currentEntry->value:NULL; }; |
---|
68 | |
---|
69 | void debug() const; |
---|
70 | |
---|
71 | private: |
---|
72 | void deleteSections(); |
---|
73 | |
---|
74 | private: |
---|
75 | tList<IniSection>* sections; //!< a list of all stored Sections of the Parser |
---|
76 | IniSection* currentSection; //!< the current selected Section |
---|
77 | IniEntry* currentEntry; //!< the current selected entry (in currentSection) |
---|
78 | }; |
---|
79 | |
---|
80 | #endif /* _INI_PARSER_H */ |
---|