-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathini.c
365 lines (334 loc) · 9.01 KB
/
ini.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "ini.h"
#define _CRLF "\r\n"
#define fputs_win(A,B) fputs(A,B); fputs(_CRLF,B);
#define test() puts("test");
int INI_ERROR = 0;
// [INI Library Functions]
//------------------------------------
char *ini_read(char *file, char *section, char *key, char *dvalue) {
FILE* pFile = fopen(file,"rb");
if (pFile == NULL) {
INI_ERROR = ini_error(INI_ERROR_FILEREAD,INI_ERROR_VERBOSE);
return dvalue;
} else {
//int slen = 2+strlen(section);
//char *s = malloc(slen+1); sprintf(s,"[%s]",strlower(section)); s[slen]='\0';
//char *k = strlower(key); //int klen = strlen(k);
char line[INI_MAXLINE];
//char keyname[INI_MAXLINE];
int in_section = 0, found_section = 0, k_end, no_section=0;
//int linenum=0;
if (strlen(section)==0 || section == NULL)
no_section=1;
while(fgets(line,sizeof(line),pFile) != NULL)
{
//linenum+=1;
int nr, len = strlen(line);
//if(line[len-1] == '\n') line[len-1] = '\0'; // strip trailing '\n' if it exists
for (nr=len;nr>1;nr--) {
if(isspace((unsigned char)line[nr-1])) //if(line[nr-1]=='\r' || line[nr-1]=='\n')
line[nr-1]='\0';
else
{
len = strlen(line);
break;
}
}
strcpy(line,ini_fix(line)); len = strlen(line);
if (line[0]=='[' && line[len-1]==']') //if section
{
strcpy(line,strlower(line));
/*
printf("DEBUG: ini_section(line) = \"%s\" : (%d) original: \"%s\"\n",
ini_section(line),
(strcmp(strlower(ini_section(line)),strlower(section))==0),
strlower(section));
*/
//if (((strstr(line,s)-line)+1)>=0) {
if (strcmp(strlower(ini_section(line)),strlower(section))==0) { //found section
in_section=1;
found_section=1;
if (strlen(key)==0 || key==NULL)
{
//free(s);
//free(k);
fclose(pFile);
return section;
}
} else {
in_section=0;
}
}
if (in_section || no_section)
{
k_end=((strchr(line,'=')-line)+1);
if (k_end>0)
{
//strcpy(keyname,strlower(line)); keyname[k_end-1]='\0';
//if (strcmp(keyname,k)==0)
if ((ini_key(line)!=NULL) && (strcmp(strlower(ini_key(line)),strlower(key))==0)) //found key
{
//free(s);
//free(k);
fclose(pFile);
return substr(line,k_end,(strlen(line)-k_end));
}
}
else if (k_end == 0)
{
INI_ERROR = ini_error(INI_ERROR_PARSEINVALID,INI_ERROR_VERBOSE);
}
}
else if (found_section && !in_section)
break; //return section;
}
//free(s);
//free(k);
fclose(pFile);
}
return dvalue;
}
int ini_write(char *file, char *section, char *key, char *value) {
/*
open original file
open output file
allocate a line buffer that is large enough
read a line from the original file
do
return an error if the buffer is too small
manipulate the line
write the manipulated line to the output file
read a line from the original file
loop until read returns nothing
then delete the original file with
and replace it with the output file
*/
char *tmp = malloc(strlen(file)+8);
sprintf(tmp,"%s.tmp",file);
FILE* pFile = fopen(file,"rb");
if (pFile != NULL) {
FILE* pTmp = fopen(tmp,"wb");
if (pTmp != NULL)
{
char line[INI_MAXLINE], o_line[INI_MAXLINE];
char* current_section = malloc(INI_MAXLINE);
//int linenum=0
int len, updated=0, nr;
while(fgets(line,sizeof(line),pFile) != NULL)
{
//linenum+=1;
len = strlen(line);
/*
if(line[len-1] == '\n') line[len-1] = '\0'; // strip trailing '\n' if it exists
len = strlen(line);
if(line[len-1] == '\r') line[len-1] = '\0'; // strip trailing '\r' if it exists
len = strlen(line);
if(line[len-1] == '\n') line[len-1] = '\0'; // strip trailing '\n' if it exists
len = strlen(line);
if(line[len-1] == '\r') line[len-1] = '\0'; // strip trailing '\r' if it exists
len = strlen(line);
*/
for (nr=len;nr>1;nr--) {
if(isspace((unsigned char)line[nr-1])) //if(line[nr-1]=='\r' || line[nr-1]=='\n')
line[nr-1]='\0';
else
{
len = strlen(line);
break;
}
}
strcpy(o_line,line);
strcpy(line,ini_fix(line)); len = strlen(line);
//strcpy(o_line,line); //uncomment this line, to trim comments from ini...
//----------------------
if (!updated)
{
if (ini_section(line)!=NULL) //Section Indicator line
{
strcpy(current_section,ini_section(line));
fputs_win(line,pTmp);
}
else //parse line: key=value
{
//if correct section...
//printf("%s =x= %s\n", current_section, section);
if (strcmp(strlower(current_section),strlower(section))==0)
{
//putchar('_');
//puts(ini_key(line));
//putchar('_');
if ((ini_key(line)!=NULL) && (strcmp(strlower(ini_key(line)),strlower(key))==0)) //found key
{
//int k_end = strlen(ini_key(line));
//value == substr(line,k_end,(strlen(line)-k_end));
fprintf(pTmp,"%s=%s%s",key,value,_CRLF);
updated=1;
}
else //not found key
{
fputs_win(o_line,pTmp);
}
}
else //continue copying...
{
fputs_win(o_line,pTmp);
}
}
}
else //continue copying...
{
fputs_win(o_line,pTmp);
}
}
if (!updated)
{
if (strcmp(strlower(current_section),strlower(section))!=0)
{
fputs(_CRLF,pTmp);
fprintf(pTmp,"[%s]%s",section,_CRLF);
}
fprintf(pTmp,"%s=%s%s",key,value,_CRLF);
updated=1;
}
fclose(pTmp);
fclose(pFile);
if (updated)
{
//delete old one, rename new one to old name...
if ((remove(file)!=0) && (rename(tmp,file)!=0))
{
free(tmp);
INI_ERROR = ini_error(INI_ERROR_FILE,INI_ERROR_VERBOSE);
return 0;
}
else
{
free(tmp);
return 1;
}
}
else
{
free(tmp);
INI_ERROR = ini_error(INI_ERROR_PARSEINVALID,INI_ERROR_VERBOSE);
return 0;
}
}
else
{
free(tmp);
fclose(pFile);
INI_ERROR = ini_error(INI_ERROR_FILEREAD,INI_ERROR_VERBOSE);
return 0;
}
fclose(pFile);
}
free(tmp);
INI_ERROR = ini_error(INI_ERROR_FILEREAD,INI_ERROR_VERBOSE);
return 0;
}
// [INI Internal Functions]
//------------------------------------
char *ini_fix(char *str) {
int i=(strchr(str,';')-str)+1; if (i>=0) str[i-1]='\0'; //Support for ;comment lines
return ini_strip(str);
}
char *ini_section(char *line) {
if (line==NULL)
return NULL;
int len = strlen(line);
if (line[0]=='[' && line[len-1]==']') //if section
#if INI_AUTOTRIM
return ini_strip(substr(line, 1, len-2));
#else
return substr(line, 1, len-2);
#endif
else
return NULL;
}
char *ini_key(char *line) {
if (line==NULL)
return NULL;
int k_end=(strchr(line,'=')-line)+1;
if (k_end>0)
{
char *keyname = malloc(INI_MAXLINE);
strcpy(keyname,strlower(line)); keyname[k_end-1]='\0';
#if INI_AUTOTRIM
return ini_strip(keyname);
#else
return keyname;
#endif
}
if (k_end==0)
{
INI_ERROR = ini_error(INI_ERROR_PARSEINVALID,INI_ERROR_VERBOSE);
}
return NULL;
}
int ini_error(int error, int v) {
if (v) {
if (error!=INI_ERROR_NONE){fprintf(stderr,"\nIni_Error: 0x%0.2X - ", error);}
switch(error) {
case INI_ERROR_NONE : return INI_ERROR_NONE;
case INI_ERROR_FILE : fprintf(stderr,"Generic File Error\n"); break;
case INI_ERROR_FILEREAD : fprintf(stderr,"File Read Error\n"); break;
case INI_ERROR_FILEWRITE : fprintf(stderr,"File Write Error\n"); break;
case INI_ERROR_MEMORY : fprintf(stderr,"Generic Memory Error\n"); break;
case INI_ERROR_MEMORYREAD : fprintf(stderr,"Memory Read Error\n"); break;
case INI_ERROR_MEMORYWRITE : fprintf(stderr,"Memory Write Error\n"); break;
case INI_ERROR_MEMORYALLOC : fprintf(stderr,"Memory Allocation Error\n"); break;
case INI_ERROR_PARSE : fprintf(stderr,"Parse Error\n"); break;
case INI_ERROR_PARSEINVALID : fprintf(stderr,"Parse Invalid Error\n"); break;
default : fprintf(stderr,"Unknown Error\n"); return INI_ERROR_UNKNOWN;
}
}
return error;
}
char *ini_strip(char *str) {
int i, j, len=strlen(str);
for (i=0;isspace((unsigned char)str[i]);i++)
{ /*nothing...*/ }
int m = strlen(str)-i;
char *out = malloc(m+1);
memcpy(out+0,str+i,m+1);
for (j=strlen(out);isspace((unsigned char)out[j-1]);j--)
{ /*nothing...*/ }
out[j]='\0';
char *new_p = realloc(out,j+1);
if (new_p == NULL) //...handle error
{
ini_error(INI_ERROR_MEMORYALLOC,INI_ERROR_VERBOSE);
return out;
}
return new_p;
}
// [INI Utility Functions]
//------------------------------------
char *strlower(char *str) {
#if !(INI_CASESENSITIVE)
int i, len=strlen(str);
char *out=malloc(len+1);
for (i=0;i<len;i++)
out[i]=tolower(str[i]);
out[len]='\0';
//free(out);
return out;
#else
return str;
#endif
}
char *substr(char *str, int start, int length) {
int i, len=strlen(str);
if ((length+start)<=len || start>0 || length>0){
char *out=calloc(len+1,sizeof(char));
for (i=start;i<(length+start);i++)
out[i-start]=str[i];
//out[len]='\0';
return out;
}
else
{
return NULL;
}
}