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