-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathINIFile.cpp
345 lines (293 loc) · 9.03 KB
/
INIFile.cpp
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
// INIFile.cpp
// Configuration File Management Class
// Copyright (c) 1989-2019 InterSafe, Inc.
// Written by Michael A. Hotz
#include "common.h"
/*--------------------------------------------------------------------------*/
INIFile::INIFile(const char *aFileName)
{
INIGroup *group;
INIEntry *entry;
FILE *in;
struct stat finfo;
char *check,*after;
char *buffer;
char local[1024];
basegroup = NULL;
filename = NULL;
if (aFileName == NULL) return;
filename = new char[strlen(aFileName)+1];
strcpy(filename,aFileName);
in = fopen(filename,"r");
if (in == NULL) return;
memset(&finfo,0,sizeof(finfo));
fstat(fileno(in),&finfo);
buffer = new char[0x1000];
if (buffer != NULL) setvbuf(in,buffer,_IOFBF,0x1000);
group = MakeGroup("NULL");
for(;;)
{
check = fgets(local,sizeof(local),in);
if (check == NULL) break;
while ((check[0] != 0) && (check[0] != '#') && (check[0] != '\n') && (check[0] != '\r')) check++;
check[0] = 0;
check = strchr(local,'[');
if (check != NULL) after = strchr(check,']');
else after = NULL;
if (after != NULL)
{
group = MakeGroup(local);
continue;
}
check = strchr(local,'=');
if (check == NULL) continue;
*check++=0;
// skip over any leading space
while ((check[0] != 0) && (isspace(check[0]))) *check++=0;
// for quoted strings look for closing quote
if (check[0] == '"')
{
*check++=0;
after = strchr(check,'"');
if (check == NULL) continue;
after[0] = 0;
}
// otherwise trim any trailing space
else
{
after = check;
while ((after[0] != 0) && (!isspace(after[0]))) after++;
after[0] = 0;
}
if (group == NULL) continue;
entry = group->MakeEntry(local);
if (entry != NULL) entry->SetEntryText(check);
}
fclose(in);
if (buffer != NULL) delete[](buffer);
}
/*--------------------------------------------------------------------------*/
INIFile::~INIFile(void)
{
INIGroup *current,*chain;
if (filename != NULL) delete[](filename);
current = basegroup;
while (current != NULL)
{
chain = current->next;
delete(current);
current = chain;
}
basegroup = NULL;
}
/*--------------------------------------------------------------------------*/
INIGroup *INIFile::FindGroup(const char *aGroupName)
{
INIGroup *current;
for(current = basegroup;current != NULL;current = current->next)
{
if (strcasecmp(current->GroupName,aGroupName) == 0) return(current);
}
return(NULL);
}
/*--------------------------------------------------------------------------*/
INIGroup *INIFile::MakeGroup(const char *aGroupName)
{
INIGroup *current;
if (basegroup == NULL) return(basegroup = new INIGroup(NULL,aGroupName));
for(current = basegroup;current->next != NULL;current = current->next);
current->next = new INIGroup(current,aGroupName);
return(current->next);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,signed char& dest,signed char init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = (char)strtol(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,signed short& dest,signed short init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = (short)strtol(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,signed int& dest,signed int init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = (int)strtol(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,signed long& dest,signed long init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = strtol(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,unsigned char& dest,unsigned char init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = (unsigned char)strtoul(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,unsigned short& dest,unsigned short init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = (unsigned short)strtoul(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,unsigned int& dest,unsigned int init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = (unsigned int)strtoul(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,unsigned long& dest,unsigned long init)
{
char *find;
dest = init;
find = FindString(group,field);
if (find != NULL) dest = strtoul(find,NULL,0);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,char *dest,const char *init)
{
char *find;
if (init == NULL) strcpy(dest,"");
else strcpy(dest,init);
find = FindString(group,field);
if (find != NULL) strcpy(dest,find);
return(find ? 1 : 0);
}
/*--------------------------------------------------------------------------*/
int INIFile::GetItem(const char *group,const char *field,void *dest,int size,void *init)
{
unsigned char *target;
unsigned char byte;
unsigned char niba,nibb;
char *find;
int x,y;
if (init == NULL) memset(dest,0,size);
else memcpy(dest,init,size);
find = FindString(group,field);
if (find == NULL) return(0);
target = (unsigned char *)dest;
for(x = 0;x < size;x++)
{
y = (x * 2);
niba = (unsigned char)((find[y]) - 65);
nibb = (unsigned char)((find[y+1]) - 65);
byte = (unsigned char)(niba | (nibb << 4));
target[x] = byte;
}
return(1);
}
/*--------------------------------------------------------------------------*/
char *INIFile::FindString(const char *aGroupName,const char *aEntryName)
{
INIGroup *group;
INIEntry *entry;
group = FindGroup(aGroupName);
if (group == NULL) return(NULL);
entry = group->FindEntry(aEntryName);
if (entry == NULL) return(NULL);
return(entry->EntryText);
}
/*--------------------------------------------------------------------------*/
/****************************************************************************/
/*--------------------------------------------------------------------------*/
INIGroup::INIGroup(INIGroup *aLast,const char *aGroupName) : last(aLast)
{
char *check,*strip;
char worktext[32];
baseentry = NULL;
next = NULL;
strcpy(worktext,aGroupName);
check = strchr(worktext,'[');
if (check == NULL) check = worktext;
else *check++ = 0;
strip = strchr(check,']');
if (strip != NULL) *strip = 0;
GroupName = new char[strlen(check)+1];
if (GroupName != NULL) strcpy(GroupName,check);
}
/*--------------------------------------------------------------------------*/
INIGroup::~INIGroup(void)
{
INIEntry *current,*chain;
current = baseentry;
while (current != NULL)
{
chain = current->next;
delete(current);
current = chain;
}
delete[](GroupName);
}
/*--------------------------------------------------------------------------*/
INIEntry *INIGroup::MakeEntry(const char *aEntryName)
{
INIEntry *current;
if (baseentry == NULL) return(baseentry = new INIEntry(NULL,aEntryName));
for(current = baseentry;current->next != NULL;current = current->next);
current->next = new INIEntry(current,aEntryName);
return(current->next);
}
/*--------------------------------------------------------------------------*/
INIEntry *INIGroup::FindEntry(const char *aEntryName)
{
INIEntry *current;
for(current = baseentry;current != NULL;current = current->next)
{
if (strcasecmp(current->EntryName,aEntryName) == 0) return(current);
}
return(NULL);
}
/*--------------------------------------------------------------------------*/
/****************************************************************************/
/*--------------------------------------------------------------------------*/
INIEntry::INIEntry(INIEntry *aLast,const char *aEntryName) : last(aLast)
{
EntryName = new char[strlen(aEntryName)+1];
if (EntryName != NULL) strcpy(EntryName,aEntryName);
EntryText = NULL;
next = NULL;
}
/*--------------------------------------------------------------------------*/
INIEntry::~INIEntry(void)
{
if (EntryName != NULL) delete[](EntryName);
if (EntryText != NULL) delete[](EntryText);
}
/*--------------------------------------------------------------------------*/
int INIEntry::SetEntryText(const char *aEntryText)
{
if (EntryText != NULL) delete[](EntryText);
EntryText = new char[strlen(aEntryText)+1];
if (EntryText == NULL) return(1);
strcpy(EntryText,aEntryText);
return(0);
}
/*--------------------------------------------------------------------------*/