Simple library of INI file parser.

Dependents:   TrainInfoSample WatchSensorMail

Committer:
rinosh2
Date:
Thu Nov 18 17:39:02 2010 +0000
Revision:
2:4ae80a05f6ea
Parent:
1:3601c7feb547
Add debug messages

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rinosh2 0:995403221573 1 ///////////////////////////////////////////////////////////////////////////////
rinosh2 0:995403221573 2 // IniFile: .ini file parser by rinos 2010
rinosh2 0:995403221573 3 ///////////////////////////////////////////////////////////////////////////////
rinosh2 0:995403221573 4
rinosh2 0:995403221573 5 // Ini file value (int/bool/string)
rinosh2 0:995403221573 6 // Key1 = 123 -> 123 (int)
rinosh2 0:995403221573 7 // Key2 = 0x123 -> 291 (int)
rinosh2 0:995403221573 8 // Key3 = FALSE -> false (bool)
rinosh2 0:995403221573 9 // Key4 = TRUE -> true (bool)
rinosh2 0:995403221573 10 // Key5 = 123 -> true (bool)
rinosh2 0:995403221573 11 // key6 = abc "def -> 'abc "def' (string)
rinosh2 0:995403221573 12 // key7 = " ghi "jkl " -> ' ghi "jkl '(string)
rinosh2 0:995403221573 13 // #comment line
rinosh2 0:995403221573 14
rinosh2 0:995403221573 15 #ifndef __INI_FILE_H__
rinosh2 0:995403221573 16 #define __INI_FILE_H__
rinosh2 0:995403221573 17
rinosh2 0:995403221573 18 #include "mbed.h"
rinosh2 0:995403221573 19
rinosh2 0:995403221573 20 class IniFile{
rinosh2 0:995403221573 21 // defines /////////////////////////////////////////////////////////////////
rinosh2 0:995403221573 22 public:
rinosh2 0:995403221573 23 // data type
rinosh2 0:995403221573 24 typedef enum {
rinosh2 0:995403221573 25 DTYPE_INT = -1,
rinosh2 0:995403221573 26 DTYPE_BOOL = -2,
rinosh2 0:995403221573 27 // other string
rinosh2 0:995403221573 28 } DataType;
rinosh2 0:995403221573 29
rinosh2 0:995403221573 30 // For the multiple read
rinosh2 0:995403221573 31 struct IniList{
rinosh2 0:995403221573 32 const char* key; // key name (set NULL for list end)
rinosh2 0:995403221573 33 int typelen; // >0: buffer length, <0: DataType
rinosh2 0:995403221573 34 void* buf; // return buffer
rinosh2 0:995403221573 35 };
rinosh2 0:995403221573 36
rinosh2 0:995403221573 37 // error code
rinosh2 0:995403221573 38 typedef enum {
rinosh2 0:995403221573 39 S_SUCCESS,
rinosh2 0:995403221573 40 S_OPEN_ERROR,
rinosh2 0:995403221573 41 S_NOT_OPENED,
rinosh2 0:995403221573 42 S_NO_KEY,
rinosh2 0:995403221573 43 S_BUFFER_TOO_SHORT,
rinosh2 0:995403221573 44 S_FORMAT_ERROR,
rinosh2 0:995403221573 45 } Status;
rinosh2 0:995403221573 46
rinosh2 0:995403221573 47 // internal member/method //////////////////////////////////////////////////
rinosh2 0:995403221573 48 private:
rinosh2 0:995403221573 49 FILE* m_fp;
rinosh2 0:995403221573 50
rinosh2 0:995403221573 51 // Invalid method
rinosh2 0:995403221573 52 protected:
rinosh2 1:3601c7feb547 53 IniFile(const IniFile& v);
rinosh2 1:3601c7feb547 54 const IniFile& operator =(const IniFile& v);
rinosh2 0:995403221573 55
rinosh2 0:995403221573 56 public:
rinosh2 0:995403221573 57 IniFile(const char* file = 0);
rinosh2 0:995403221573 58 ~IniFile();
rinosh2 0:995403221573 59
rinosh2 0:995403221573 60 // Access methods
rinosh2 0:995403221573 61 Status open(const char* file);
rinosh2 0:995403221573 62 Status close();
rinosh2 0:995403221573 63
rinosh2 0:995403221573 64 Status get(const char* key, char* ret, int ret_size);
rinosh2 0:995403221573 65 Status get(const char* key, int& ret);
rinosh2 0:995403221573 66 Status get(const char* key, bool& ret);
rinosh2 1:3601c7feb547 67 Status get(const IniList* inilist);
rinosh2 0:995403221573 68
rinosh2 0:995403221573 69 // For easy acccess
rinosh2 0:995403221573 70 static Status getval(const char* inifile, const char* key, char* ret, int ret_size){
rinosh2 0:995403221573 71 return IniFile(inifile).get(key, ret, ret_size);
rinosh2 0:995403221573 72 }
rinosh2 0:995403221573 73 static Status getval(const char* inifile, const char* key, int& ret){
rinosh2 0:995403221573 74 return IniFile(inifile).get(key, ret);
rinosh2 0:995403221573 75 }
rinosh2 0:995403221573 76 static Status getval(const char* inifile, const char* key, bool& ret){
rinosh2 0:995403221573 77 return IniFile(inifile).get(key, ret);
rinosh2 0:995403221573 78 }
rinosh2 1:3601c7feb547 79 static Status getval(const char* inifile, const IniList* inilist){
rinosh2 0:995403221573 80 return IniFile(inifile).get(inilist);
rinosh2 0:995403221573 81 }
rinosh2 1:3601c7feb547 82
rinosh2 1:3601c7feb547 83 // for string triming
rinosh2 1:3601c7feb547 84 static Status strtrim(char* dst, const char* src, int dst_size); // move to public
rinosh2 0:995403221573 85 };
rinosh2 0:995403221573 86
rinosh2 1:3601c7feb547 87 // for the table
rinosh2 1:3601c7feb547 88 #define INIFILE_INT(key, val) {key, IniFile::DTYPE_INT, &val}
rinosh2 1:3601c7feb547 89 #define INIFILE_BOOL(key, val) {key, IniFile::DTYPE_BOOL, &val}
rinosh2 1:3601c7feb547 90 #define INIFILE_STR(key, buf, size) {key, size, buf}
rinosh2 1:3601c7feb547 91 #define INIFILE_END 0
rinosh2 1:3601c7feb547 92
rinosh2 0:995403221573 93 #endif