[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 | |
---|
[4597] | 13 | #include "base_object.h" |
---|
[2064] | 14 | |
---|
[5014] | 15 | // FORWARD DEFINITION // |
---|
| 16 | template<class T> class tList; |
---|
[2064] | 17 | |
---|
[2141] | 18 | //! ini-file parser |
---|
| 19 | /** |
---|
[5014] | 20 | * This class can be used to load an initializer file and parse it's contents for variablename=value pairs. |
---|
| 21 | */ |
---|
[4597] | 22 | class IniParser : public BaseObject |
---|
| 23 | { |
---|
[5014] | 24 | private: |
---|
| 25 | //////////////////////////////////// |
---|
| 26 | struct IniEntry |
---|
| 27 | { |
---|
| 28 | char* name; |
---|
| 29 | char* value; |
---|
| 30 | }; |
---|
[4597] | 31 | |
---|
[5014] | 32 | struct IniSection |
---|
| 33 | { |
---|
| 34 | char* name; |
---|
| 35 | tList<IniEntry>* entries; |
---|
| 36 | }; |
---|
| 37 | //////////////////////////////////// |
---|
[4482] | 38 | |
---|
[5014] | 39 | public: |
---|
| 40 | IniParser (const char* filename = NULL); |
---|
| 41 | ~IniParser (); |
---|
[4482] | 42 | |
---|
[5014] | 43 | bool openFile(const char* name); |
---|
[4597] | 44 | |
---|
[5015] | 45 | bool getSection(const char* sectionName); |
---|
| 46 | |
---|
| 47 | void getFirstSection(); |
---|
[5014] | 48 | const char* nextSection(); |
---|
| 49 | |
---|
[5015] | 50 | /** @returns true if the file is opened, false otherwise*/ |
---|
[5014] | 51 | bool isOpen() const { return (sections != NULL)?true:false; }; |
---|
| 52 | |
---|
| 53 | const char* getVar(const char* entryName, const char* sectionName, const char* defaultValue = "") const; |
---|
[5015] | 54 | |
---|
| 55 | void getFirstVar(); |
---|
[5014] | 56 | bool nextVar(); |
---|
| 57 | |
---|
[5015] | 58 | /** @returns the name of the Current selected Section */ |
---|
| 59 | const char* getCurrentSection() const { return (this->currentSection!=NULL)?this->currentSection->name:NULL; }; |
---|
| 60 | /** @returns the current entries Name, or NULL if we havn't selected a Entry */ |
---|
| 61 | const char* getCurrentName() const { return (this->currentEntry!=NULL)?this->currentEntry->name:NULL; }; |
---|
| 62 | /** @returns the current entries Value, or NULL if we havn't selected a Entry */ |
---|
| 63 | const char* getCurrentValue() const { return (this->currentEntry!=NULL)?this->currentEntry->value:NULL; }; |
---|
[5014] | 64 | |
---|
| 65 | void debug() const; |
---|
| 66 | |
---|
| 67 | private: |
---|
| 68 | void deleteSections(); |
---|
| 69 | |
---|
| 70 | private: |
---|
| 71 | tList<IniSection>* sections; |
---|
| 72 | IniSection* currentSection; |
---|
| 73 | IniEntry* currentEntry; |
---|
[2064] | 74 | }; |
---|
| 75 | |
---|
[3224] | 76 | #endif /* _INI_PARSER_H */ |
---|