blob: 00b45a2e1acaecd71e1a27b0a4e5df01a3ed5804
1 | #ifndef INI_FILE_H_ |
2 | #define INI_FILE_H_ |
3 | /* |
4 | *vertion 0.01, just linux 8bit byte string, write by showbo |
5 | */ |
6 | #include <CTvLog.h> |
7 | |
8 | static const int MAX_INI_FILE_LINE_LEN = 512; |
9 | |
10 | typedef enum _LINE_TYPE { |
11 | LINE_TYPE_SECTION = 0, |
12 | LINE_TYPE_KEY, |
13 | LINE_TYPE_COMMENT, |
14 | } LINE_TYPE; |
15 | |
16 | //基于链表结构 |
17 | |
18 | //行链表,去掉多余的空格和回车,换行后. |
19 | typedef struct _LINE { |
20 | LINE_TYPE type;//类型 |
21 | char Text[MAX_INI_FILE_LINE_LEN];//内容 |
22 | int LineLen; |
23 | char *pKeyStart; |
24 | char *pKeyEnd; |
25 | char *pValueStart; |
26 | char *pValueEnd; |
27 | struct _LINE *pNext; |
28 | } LINE; |
29 | //段链表 |
30 | typedef struct _SECTION { |
31 | LINE *pLine; |
32 | struct _SECTION *pNext; |
33 | } SECTION; |
34 | |
35 | |
36 | class CIniFile { |
37 | public: |
38 | //const int MAX_SECTION_NAME_LEN = 16; |
39 | //const int MAX_KEY_NAME_LEN = 64; |
40 | //const int MAX_VALUE_LEN = 512; |
41 | |
42 | CIniFile(); |
43 | ~CIniFile(); |
44 | int LoadFromFile(const char *filename); |
45 | int LoadFromString(const char *str); |
46 | |
47 | int SaveToFile(const char *filename = NULL); |
48 | |
49 | int SetString(const char *section, const char *key, const char *value); |
50 | int SetInt(const char *section, const char *key, int value); |
51 | |
52 | const char *GetString(const char *section, const char *key, const char *def_value); |
53 | int GetInt(const char *section, const char *key, int def_value); |
54 | |
55 | void printAll(); |
56 | private: |
57 | LINE_TYPE getLineType(char *Str); |
58 | //去掉串里面的,空格,回车,换行,s指向转换处理后的串的开头 |
59 | void allTrim(char *Str); |
60 | |
61 | SECTION *getSection(const char *section); |
62 | LINE *getKeyLineAtSec(SECTION *pSec, const char *key); |
63 | |
64 | void FreeAllMem(); |
65 | int InsertSection(SECTION *pSec); |
66 | int InsertKeyLine(SECTION *pSec, LINE *line); |
67 | char mpFileName[256]; |
68 | FILE *m_pIniFile; |
69 | LINE *mpFirstLine; |
70 | SECTION *mpFirstSection; |
71 | }; |
72 | #endif //end of INI_FILE_H_ |
73 |