2 * modified port of Cini project
4 * liukui,liukee2009@foxmail.com
16 #define CHECK_FILE(f) if((f)==NULL) return FILE_NO_FILE
18 typedef struct FIELD {
20 char FieldName[FIELDNAME_LEN];
21 char FieldValue[FIELDVALUE_LEN];
23 typedef struct tagSECTION {
24 struct tagSECTION *sNext;
25 char SectionName[SECTION_LEN];
29 static SECTION *g_INIlist = NULL;
31 static void removeSpace(char *str) {
35 while(0x20 == str[start])start++;
36 for(i=start;i<end;i++) {
39 for(i=end-1;i>=0;i--) {
47 void WriteString(char *SectionName, char *FieldName, char *FieldValue) {
48 SECTION *sect = NULL, *preSect = NULL;
49 FIELD *Field = NULL, *preField = NULL;
53 sect = malloc(sizeof(*sect));
54 memset(sect,0,sizeof(*sect));
55 strcpy(sect->SectionName, SectionName);
60 if(!strcmp(sect->SectionName, SectionName))
66 sect = malloc(sizeof(*sect));
67 memset(sect,0,sizeof(*sect));
68 strcpy(sect->SectionName, SectionName);
69 preSect->sNext = sect;
73 Field = malloc(sizeof(*Field));
74 memset(Field,0,sizeof(*Field));
75 strcpy(Field->FieldName, FieldName);
80 if(!strcmp(Field->FieldName, FieldName))
87 Field = malloc(sizeof(*Field));
88 memset(Field,0,sizeof(*Field));
89 strcpy(Field->FieldName, FieldName);
90 preField->fNext = Field;
93 strcpy(Field->FieldValue, FieldValue);
97 /* 0:ok;1:no section;2:no filename; */
98 void ReadString(char *SectionName,char *FieldName,char *FieldValue) {
101 if (NULL == g_INIlist)
105 if(!strcmp(sect->SectionName, SectionName))
115 if(!strcmp(Field->FieldName, FieldName))
117 Field = Field->fNext;
121 strcpy(FieldValue, Field->FieldValue);
125 int loadINI(char *filename) {
128 char SectionName[SECTION_LEN], FieldName[FIELDNAME_LEN], FieldValue[FIELDVALUE_LEN], ch;
130 f=fopen(filename,"rb");
135 fread(&ch,sizeof(char),1,f);
142 fread(&ch,sizeof(char),1,f);
144 SectionName[i++] = ch;
150 removeSpace(SectionName);
152 fread(&ch,sizeof(char),1,f);
153 if (ch == '\r' || ch == '\n')
156 if (SectionName[0] == '\0')
161 fread(&ch,sizeof(char),1,f);
162 if (isalnum(ch) || ch=='[')
168 fseek(f,fpointer,SEEK_SET);
175 fread(&ch,sizeof(char),1,f);
183 removeSpace(FieldName);
184 if (FieldName[0]=='\0')
188 fread(&ch,sizeof(char),1,f);
189 if (ch != '\r' && ch != '\n')
190 FieldValue[i++] = ch;
192 FieldValue[i] = '\0';
196 removeSpace(FieldValue);
197 WriteString(SectionName,FieldName,FieldValue);
204 int saveINI(char *filename) {
205 SECTION *sect = NULL, *preSect;
206 FIELD *Field = NULL, *preField;
207 char *pSBracket = "[", *pEBracket="]", *peq="=", *pend="\r\n";
209 if (NULL == g_INIlist)
211 FILE *f = fopen(filename,"wb");
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) {
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;