fixed "may be uninitialized" on linux systems
[NeonServV5.git] / src / lib / ini.c
1 /*
2  * modified port of Cini project
3  *  original by:
4  *   liukui,liukee2009@foxmail.com
5 */
6 #ifdef  __cplusplus
7 extern "C" {
8 #endif
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "ini.h"
15
16 #define CHECK_FILE(f) if((f)==NULL) return FILE_NO_FILE
17
18 typedef struct FIELD {
19     struct FIELD *fNext;
20     char FieldName[FIELDNAME_LEN];
21     char FieldValue[FIELDVALUE_LEN];
22 } FIELD;
23 typedef struct tagSECTION {
24     struct tagSECTION *sNext;
25     char SectionName[SECTION_LEN];
26     FIELD *fNext; 
27 } SECTION;
28
29 static SECTION *g_INIlist = NULL;
30
31 static void removeSpace(char *str) {
32     int i;
33     int start=0;
34     int end=strlen(str);
35     while(0x20 == str[start])start++;
36     for(i=start;i<end;i++) {
37         str[i-start]=str[i];
38     }
39     for(i=end-1;i>=0;i--) {
40         if (str[i] == 0x20) {
41             str[i]='\0';
42         } else
43             break;
44     }
45 }
46
47 void WriteString(char *SectionName, char *FieldName, char *FieldValue) {
48     SECTION *sect = NULL, *preSect = NULL;
49     FIELD *Field = NULL, *preField = NULL;
50
51     if (!g_INIlist)
52     {
53         sect = malloc(sizeof(*sect));
54         memset(sect,0,sizeof(*sect));
55         strcpy(sect->SectionName, SectionName);
56         g_INIlist = sect;
57     } else {
58         sect = g_INIlist;
59         while(sect) {
60             if(!strcmp(sect->SectionName, SectionName))
61                 break;
62             preSect = sect;
63             sect = sect->sNext;
64         }
65         if (!sect) {
66             sect = malloc(sizeof(*sect));
67             memset(sect,0,sizeof(*sect));
68             strcpy(sect->SectionName, SectionName);
69             preSect->sNext = sect;
70         }
71     }
72     if (!sect->fNext) {
73         Field = malloc(sizeof(*Field));
74         memset(Field,0,sizeof(*Field));
75         strcpy(Field->FieldName, FieldName);
76         sect->fNext = Field;
77     } else {
78         Field = sect->fNext;
79         while(Field) {
80             if(!strcmp(Field->FieldName, FieldName))
81                 break;
82             preField=Field;
83             Field=Field->fNext;
84         }
85
86         if (!Field) {
87             Field = malloc(sizeof(*Field));
88             memset(Field,0,sizeof(*Field));
89             strcpy(Field->FieldName, FieldName);
90             preField->fNext = Field;
91         }
92     }
93     strcpy(Field->FieldValue, FieldValue);
94     return;
95 }
96
97 /* 0:ok;1:no section;2:no filename; */
98 void ReadString(char *SectionName,char *FieldName,char *FieldValue) {
99     SECTION *sect = NULL;
100     FIELD *Field = NULL;
101     if (NULL == g_INIlist)
102         return;
103     sect=g_INIlist;
104     while(sect) {
105         if(!strcmp(sect->SectionName, SectionName))
106             break;
107         sect = sect->sNext;
108     }
109     if (!sect)
110         return;
111     if (!sect->fNext)
112         return;
113     Field = sect->fNext;
114     while(Field) {
115         if(!strcmp(Field->FieldName, FieldName))
116                 break;
117         Field = Field->fNext;
118     }
119     if (!Field)
120         return;
121     strcpy(FieldValue, Field->FieldValue);
122     return;
123 }
124
125 int loadINI(char *filename) {
126     FILE *f;
127     int i, fpointer = 0;
128     char SectionName[SECTION_LEN], FieldName[FIELDNAME_LEN], FieldValue[FIELDVALUE_LEN], ch;
129
130     f=fopen(filename,"rb");
131     CHECK_FILE(f);
132
133     while(!feof(f)) {
134         while(!feof(f)) {
135             fread(&ch,sizeof(char),1,f);
136             if (ch == '[')
137                 break;
138         }
139         i=0;
140         while(!feof(f))
141         {
142             fread(&ch,sizeof(char),1,f);
143             if (ch != ']')
144                 SectionName[i++] = ch;
145             else {
146                 SectionName[i]='\0';
147                 break;
148             }
149         }
150         removeSpace(SectionName);
151         while(!feof(f)) {
152             fread(&ch,sizeof(char),1,f);
153             if (ch == '\r' || ch == '\n')
154                 break;
155         }
156         if (SectionName[0] == '\0')
157             continue; 
158         while(!feof(f)) {
159             while(!feof(f)) {
160                 fpointer = ftell(f);
161                 fread(&ch,sizeof(char),1,f);                
162                 if (isalnum(ch) || ch=='[')
163                     break;
164             }
165             if(feof(f))
166                 break;
167             if (ch=='[') {
168                 fseek(f,fpointer,SEEK_SET);
169                 break;
170             }
171             i=0;
172             FieldName[i++] = ch;            
173             while(!feof(f))
174             {
175                 fread(&ch,sizeof(char),1,f);
176                 if (ch != '=')
177                     FieldName[i++] = ch;
178                 else {
179                     FieldName[i] = '\0';
180                     break;
181                 }
182             }
183             removeSpace(FieldName);
184             if (FieldName[0]=='\0')
185                 continue;
186             i=0;
187             while(!feof(f)) {
188                 fread(&ch,sizeof(char),1,f);
189                 if (ch != '\r' && ch != '\n') 
190                     FieldValue[i++] = ch;
191                 else {
192                     FieldValue[i] = '\0';
193                     break;
194                 }
195             }
196             removeSpace(FieldValue);
197             WriteString(SectionName,FieldName,FieldValue);
198         }
199     }
200     fclose(f);
201     return FILE_SUCCESS;
202 }
203
204 int saveINI(char *filename) {
205     SECTION *sect = NULL, *preSect;
206     FIELD *Field = NULL, *preField;
207     char *pSBracket = "[", *pEBracket="]", *peq="=", *pend="\r\n";
208     
209     if (NULL == g_INIlist)
210         return FILE_SUCCESS;
211     FILE *f = fopen(filename,"wb");
212     CHECK_FILE(f);
213     sect = g_INIlist;
214     while(sect) {
215         preSect = sect;
216         fwrite(pSBracket,1,strlen(pSBracket),f);
217         fwrite(&(preSect->SectionName),1,strlen((char *)&(preSect->SectionName)),f);
218         fwrite(pEBracket,1,strlen(pEBracket),f);
219         fwrite(pend,1,strlen(pend),f);
220         if (NULL != sect->fNext) {
221             Field = sect->fNext;
222             while(Field) {
223                 preField = Field;
224                 fwrite(&(preField->FieldName),1,strlen(preField->FieldName),f);
225                 fwrite(peq,1,strlen(peq),f);
226                 fwrite(&(preField->FieldValue),1,strlen(preField->FieldValue),f);
227                 fwrite(pend,1,strlen(pend),f);
228                 Field = Field->fNext;
229                 free(preField);
230             }
231         }
232         sect=sect->sNext;
233         free(preSect);
234     }
235     fflush(f);
236     fclose(f);
237     g_INIlist = NULL;
238     return FILE_SUCCESS;
239 }
240
241 #ifdef  __cplusplus
242 }
243 #endif