-
Notifications
You must be signed in to change notification settings - Fork 302
/
readconfig.c
363 lines (304 loc) · 8.17 KB
/
readconfig.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 <ctype.h>
#include <stdio.h>
#include <string.h>
#include "readconfig.h"
#include "utils.h"
#include "readline.h"
void ConfigInitInfo(ConfigFileInfo *Info)
{
Info -> fp = NULL;
Info -> Options = NULL;
Info -> NumOfOptions = 0;
}
int ConfigOpenFile(ConfigFileInfo *Info, const char *File)
{
Info -> fp = fopen(File, "r");
if( Info -> fp == NULL )
return GET_LAST_ERROR();
else
return 0;
}
int ConfigCloseFile(ConfigFileInfo *Info)
{
return fclose(Info -> fp);
}
int ConfigAddOption(ConfigFileInfo *Info, char *KeyName, MultilineStrategy Strategy, OptionType Type, VType Initial, char *Caption)
{
int loop;
if( strlen(KeyName) > sizeof(Info -> Options -> KeyName) - 1 )
{
return -1;
}
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if(Info -> Options[loop].Status == STATUS_UNUSED)
break;
}
if(loop == Info -> NumOfOptions)
{
int loop2;
if( SafeRealloc((void *)&(Info -> Options), (Info -> NumOfOptions + 10) * sizeof(ConfigOption)) != 0)
{
return 1;
}
(Info -> NumOfOptions) += 10;
for(loop2 = loop; loop2 != Info -> NumOfOptions; ++loop2)
Info -> Options[loop2].Status = STATUS_UNUSED;
}
strcpy(Info -> Options[loop].KeyName, KeyName);
Info -> Options[loop].Type = Type;
Info -> Options[loop].Status = STATUS_DEFAULT_VALUE;
if( Caption != NULL )
{
strncpy(Info -> Options[loop].Caption, Caption, CAPTION_MAX_SIZE);
Info -> Options[loop].Caption[CAPTION_MAX_SIZE] = '\0';
} else {
*(Info -> Options[loop].Caption) = '\0';
}
switch( Type )
{
case TYPE_INT32:
case TYPE_BOOLEAN:
Info -> Options[loop].Holder = Initial;
break;
case TYPE_STRING:
if(Initial.str != NULL)
{
Info -> Options[loop].Holder.str = SafeMalloc(strlen(Initial.str) + 1);
strcpy(Info -> Options[loop].Holder.str, Initial.str);
} else {
Info -> Options[loop].Holder.str = NULL;
}
Info -> Options[loop].Strategy = Strategy;
break;
default:
break;
}
return 0;
}
static ConfigOption *GetOptionOfAInfo(const ConfigFileInfo *Info, const char *KeyName)
{
int loop;
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if(strcmp(KeyName, Info -> Options[loop].KeyName) == 0)
return Info -> Options + loop;
}
return NULL;
}
static char *GetKeyNameFromLine(const char *Line, char *Buffer)
{
const char *itr = Line;
const char *SpacePosition;
for(; isspace(*itr); ++itr);
SpacePosition = strchr(itr, ' ');
if(SpacePosition == NULL)
return NULL;
strncpy(Buffer, itr, SpacePosition - Line);
Buffer[SpacePosition - Line] = '\0';
return Buffer;
}
static const char *GetValuePosition(const char *Line)
{
const char *itr = Line;
for(; isspace(*itr); ++itr);
itr = strchr(itr, ' ');
if(itr == NULL)
return NULL;
for(; isspace(*itr) && *itr != '\0'; ++itr);
if( *itr == '\0' )
return NULL;
else
return itr;
}
int ConfigRead(ConfigFileInfo *Info)
{
int NumOfRead = 0;
char Buffer[3072];
char *ValuePos;
ReadLineStatus ReadStatus;
char KeyName[KEY_NAME_MAX_SIZE + 1];
ConfigOption *Option;
while(TRUE){
ReadStatus = ReadLine(Info -> fp, Buffer, sizeof(Buffer));
if( ReadStatus == READ_FAILED_OR_END )
return NumOfRead;
if( GetKeyNameFromLine(Buffer, KeyName) == NULL )
continue;
Option = GetOptionOfAInfo(Info, KeyName);
if( Option == NULL )
continue;
ValuePos = (char *)GetValuePosition(Buffer);
if( ValuePos == NULL )
continue;
switch( Option -> Type )
{
case TYPE_INT32:
sscanf(ValuePos, "%d", &(Option -> Holder.INT32));
break;
case TYPE_BOOLEAN:
if( isdigit(*ValuePos) )
{
if( *ValuePos == '0' )
Option -> Holder.boolean = FALSE;
else
Option -> Holder.boolean = TRUE;
} else {
StrToLower(ValuePos);
if( strstr(ValuePos, "false") != NULL )
Option -> Holder.boolean = FALSE;
else if( strstr(ValuePos, "true") != NULL )
Option -> Holder.boolean = TRUE;
if( strstr(ValuePos, "no") != NULL )
Option -> Holder.boolean = FALSE;
else if( strstr(ValuePos, "yes") != NULL )
Option -> Holder.boolean = TRUE;
}
break;
case TYPE_STRING:
{
char *result;
switch (Option -> Strategy)
{
case STRATEGY_UNKNOWN:
continue;
break;
case STRATEGY_APPEND_DISCARD_DEFAULT:
if( Option -> Status == STATUS_DEFAULT_VALUE )
{
Option -> Strategy = STRATEGY_APPEND;
}
/* No break */
case STRATEGY_REPLACE:
if( Option -> Holder.str != NULL )
{
SafeFree(Option -> Holder.str);
}
result = SafeMalloc(strlen(ValuePos) + 1);
if( result == NULL )
{
continue;
}
strcpy(result, ValuePos);
Option -> Status = STATUS_SPECIAL_VALUE;
break;
case STRATEGY_APPEND:
if( Option -> Holder.str != NULL )
{
result = SafeMalloc(strlen(Option -> Holder.str) + strlen(ValuePos) + 2);
if( result == NULL )
{
continue;
}
strcpy(result, Option -> Holder.str);
strcat(result, ",");
strcat(result, ValuePos);
SafeFree(Option -> Holder.str);
} else {
result = SafeMalloc(strlen(ValuePos) + 1);
if( result == NULL )
{
continue;
}
strcpy(result, ValuePos);
}
Option -> Status = STATUS_SPECIAL_VALUE;
break;
default:
continue;
break;
}
while( ReadStatus != READ_DONE ){
ReadStatus = ReadLine(Info -> fp, Buffer, sizeof(Buffer));
if( ReadStatus == READ_FAILED_OR_END )
break;
if( SafeRealloc((void *)&result, strlen(result) + strlen(Buffer) + 1) != 0 )
break;
strcat(result, Buffer);
}
if( strlen(result) != 0 )
{
int loop = strlen(result) - 1;
while( result[loop] == '\n' || result[loop] == '\r' )
{
result[loop] = '\0';
--loop;
}
}
Option -> Holder.str = result;
}
break;
default:
break;
}
++NumOfRead;
}
return NumOfRead;
}
const char *ConfigGetString(ConfigFileInfo *Info, char *KeyName)
{
int loop;
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if( Info -> Options[loop].Type == TYPE_STRING && strncmp(Info -> Options[loop].KeyName, KeyName, KEY_NAME_MAX_SIZE) == 0 )
return Info -> Options[loop].Holder.str;
}
return 0;
}
int ConfigGetInt32(ConfigFileInfo *Info, char *KeyName)
{
int loop;
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if( Info -> Options[loop].Type == TYPE_INT32 && strncmp(Info -> Options[loop].KeyName, KeyName, KEY_NAME_MAX_SIZE) == 0 )
return Info -> Options[loop].Holder.INT32;
}
return 0;
}
BOOL ConfigGetBoolean(ConfigFileInfo *Info, char *KeyName)
{
int loop;
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if( Info -> Options[loop].Type == TYPE_BOOLEAN && strncmp(Info -> Options[loop].KeyName, KeyName, KEY_NAME_MAX_SIZE) == 0 )
return Info -> Options[loop].Holder.boolean;
}
return 0;
}
void ConfigSetValue(ConfigFileInfo *Info, VType Value, char *KeyName)
{
int loop;
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if( strncmp(Info -> Options[loop].KeyName, KeyName, KEY_NAME_MAX_SIZE) == 0 )
{
Info -> Options[loop].Holder = Value;
break;
}
}
}
void ConfigDisplay(ConfigFileInfo *Info)
{
int loop;
for(loop = 0; loop != Info -> NumOfOptions; ++loop)
{
if( *(Info -> Options[loop].Caption) != '\0' )
{
switch( Info -> Options[loop].Type )
{
case TYPE_INT32:
printf("%s:%d\n", Info -> Options[loop].Caption, Info -> Options[loop].Holder.INT32);
break;
case TYPE_BOOLEAN:
printf("%s:%s\n", Info -> Options[loop].Caption, BoolToYesNo(Info -> Options[loop].Holder.boolean));
break;
case TYPE_STRING:
if( Info -> Options[loop].Holder.str != NULL )
printf("%s:%s\n", Info -> Options[loop].Caption, Info -> Options[loop].Holder.str);
break;
default:
break;
}
}
}
}