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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
|
/*
* %CopyrightBegin%
*
* Copyright Ericsson AB 2003-2009. All Rights Reserved.
*
* The contents of this file are subject to the Erlang Public License,
* Version 1.1, (the "License"); you may not use this file except in
* compliance with the License. You should have received a copy of the
* Erlang Public License along with this software. If not, it can be
* retrieved online at http://www.erlang.org/.
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* %CopyrightEnd%
*/
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include "init_file.h"
#define ALLOC malloc
#define REALLOC realloc
#define FREE free
#define CONTEXT_BUFFER_SIZE 1024
typedef struct {
HANDLE fd;
int eof;
int num;
int pos;
char buff[CONTEXT_BUFFER_SIZE];
} FileContext;
static char *read_one_line(FileContext *fc)
{
char *buff = ALLOC(10);
int size = 10;
int num = 0;
int skipping;
int escaped;
#define PUSH(Char) \
do { \
if (num == size) { \
size += 10; \
buff = REALLOC(buff,size); \
} \
buff[num++] = (Char); \
} while(0)
#define POP() (buff[--num])
#define TOP() (buff[(num-1)])
skipping = 0;
escaped = 0;
for(;;) {
char current;
if (fc->eof) {
break;
}
if (fc->pos == fc->num) {
if (!ReadFile(fc->fd, fc->buff, CONTEXT_BUFFER_SIZE,
&(fc->num), NULL) || !(fc->num)) {
fc->eof = 1;
break;
}
fc->pos = 0;
}
current = fc->buff[fc->pos];
++(fc->pos);
switch (current) {
case ' ':
if (!skipping && num) {
PUSH(current);
}
escaped = 0;
break;
case ';':
if (!skipping) {
if (!escaped) {
skipping = 1;
} else {
PUSH(current);
}
}
escaped = 0;
break;
case '\\':
if (!skipping) {
if (!escaped) {
escaped = 1;
} else {
PUSH(current);
escaped = 0;
}
}
break;
case '\r':
break;
case '\n':
if (!escaped) {
while (num && TOP() == ' ') {
POP();
}
if (num) {
goto done;
}
}
skipping = 0;
escaped = 0;
break;
default:
if (!skipping) {
PUSH(current);
}
escaped = 0;
break;
}
}
/* EOF comes here */
while (num && TOP() == ' ') {
POP();
}
if (!num) {
FREE(buff);
return NULL;
}
done:
PUSH('\0');
return buff;
#undef PUSH
#undef POP
#undef TOP
}
static int is_section_header(char *line)
{
int x = strlen(line);
return (x > 2 && *line == '[' && line[x-1] == ']');
}
static int is_key_value(char *line)
{
char *p = strchr(line,'=');
return (p != NULL && p > line);
}
static char *digout_section_name(char *line)
/* Moving it because it shall later be freed. */
{
int x = strlen(line);
memmove(line,line+1,x-1);
line[x-2] = '\0';
return line;
}
static void digout_key_value(char *line, char **key, char **value)
{
char *e = strchr(line,'=');
*key = line;
*value = (e+1);
*e = '\0';
while (*(--e) == ' ') {
*e = '\0';
}
while (**value == ' ') {
++(*value);
}
}
InitFile *load_init_file(char *filename)
{
HANDLE infile;
InitFile *inif;
InitSection *inis;
InitEntry *inie;
FileContext fc;
char *line;
char **lines;
int size_lines;
int num_lines;
int i;
if ( (infile = CreateFile(filename,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE) {
return NULL;
}
size_lines = 10;
num_lines = 0;
lines = ALLOC(size_lines * sizeof(char *));
fc.fd = infile;
fc.eof = 0;
fc.num = 0;
fc.pos = 0;
while ((line = read_one_line(&fc)) != NULL) {
if (num_lines == size_lines) {
size_lines += 10;
lines = REALLOC(lines,size_lines * sizeof(char *));
}
lines[num_lines] = line;
++num_lines;
}
CloseHandle(infile);
/* Now check the lines before doing anything else, so that
we don't need any error handling while creating the data
structures */
/*
The file should contain:
[section]
Key=Value
...
[section]
...
*/
i = 0;
while (i < num_lines && is_section_header(lines[i])) {
++i;
while (i < num_lines && is_key_value(lines[i])) {
++i;
}
}
if (i < num_lines) {
for (i = 0; i < num_lines; ++i) {
FREE(lines[i]);
}
FREE(lines);
return NULL;
}
/* So, now we know it's consistent... */
i = 0;
inif = ALLOC(sizeof(InitFile));
inif->num_sections = 0;
inif->size_sections = 10;
inif->sections = ALLOC(sizeof(InitSection *) * 10);
while (i < num_lines) {
inis = ALLOC(sizeof(InitSection));
inis->num_entries = 0;
inis->size_entries = 10;
inis->section_name = digout_section_name(lines[i]);
inis->entries = ALLOC(sizeof(InitEntry *) * 10);
++i;
while (i < num_lines && is_key_value(lines[i])) {
inie = ALLOC(sizeof(InitEntry));
digout_key_value(lines[i], &(inie->key), &(inie->value));
if (inis->num_entries == inis->size_entries) {
inis->size_entries += 10;
inis->entries =
REALLOC(inis->entries,
sizeof(InitEntry *) * inis->size_entries);
}
inis->entries[inis->num_entries] = inie;
++(inis->num_entries);
++i;
}
if (inif->num_sections == inif->size_sections) {
inif->size_sections += 10;
inif->sections =
REALLOC(inif->sections,
sizeof(InitSection *) * inif->size_sections);
}
inif->sections[inif->num_sections] = inis;
++(inif->num_sections);
}
FREE(lines); /* Only the array of strings, not the actual strings, they
are kept in the data structures. */
return inif;
}
int store_init_file(InitFile *inif, char *filename)
{
char *buff;
int size = 10;
int num = 0;
int i,j;
HANDLE outfile;
#define PUSH(Char) \
do { \
if (num == size) { \
size += 10; \
buff = REALLOC(buff,size); \
} \
buff[num++] = (Char); \
} while(0)
if ( (outfile = CreateFile(filename,
GENERIC_WRITE,
FILE_SHARE_WRITE,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL)) == INVALID_HANDLE_VALUE) {
return INIT_FILE_OPEN_ERROR;
}
buff = ALLOC(size);
for(i = 0; i < inif->num_sections; ++i) {
int len;
int written;
InitSection *inis = inif->sections[i];
if (!WriteFile(outfile,"[",1,&written,NULL) || written != 1) {
goto write_error;
}
len = strlen(inis->section_name);
if (!WriteFile(outfile,inis->section_name,len,&written,NULL) ||
written != len) {
goto write_error;
}
if (!WriteFile(outfile,"]\n",2,&written,NULL) || written != 2) {
goto write_error;
}
for (j = 0; j < inis->num_entries; ++j) {
InitEntry *inie = inis->entries[j];
char *p = inie->key;
num = 0;
for (;*p != '\0';++p) {
switch (*p) {
case '\\':
case ';':
PUSH('\\');
default:
PUSH(*p);
break;
}
}
PUSH('=');
p = inie->value;
for (;*p != '\0';++p) {
switch (*p) {
case '\\':
case ';':
PUSH('\\');
default:
PUSH(*p);
break;
}
}
PUSH('\n');
if (!WriteFile(outfile,buff,num,&written,NULL) || written != num) {
goto write_error;
}
}
}
FREE(buff);
CloseHandle(outfile);
return INIT_FILE_NO_ERROR;
write_error:
FREE(buff);
CloseHandle(outfile);
return INIT_FILE_WRITE_ERROR;
#undef PUSH
}
InitFile *create_init_file(void)
{
InitFile *inif = ALLOC(sizeof(InitFile));
inif->num_sections = 0;
inif->size_sections = 10;
inif->sections = ALLOC(sizeof(InitSection *) * 10);
return inif;
}
InitSection *create_init_section(char *section_name)
{
InitSection *inis = ALLOC(sizeof(InitSection));
inis->num_entries = 0;
inis->section_name = ALLOC(sizeof(char) * (strlen(section_name) + 1));
strcpy(inis->section_name, section_name);
inis->size_entries = 10;
inis->entries = ALLOC(sizeof(InitEntry *) * 10);
return inis;
}
static void free_init_entry(InitEntry *inie)
{
FREE(inie->key);
/* Value is part of the same buffer */
FREE(inie);
}
void free_init_section(InitSection *inis)
{
int i;
for (i = 0;i < inis->num_entries; ++i) {
free_init_entry(inis->entries[i]);
}
FREE(inis->entries);
FREE(inis->section_name);
FREE(inis);
}
void free_init_file(InitFile *inif)
{
int i;
for (i = 0; i < inif->num_sections; ++i) {
free_init_section(inif->sections[i]);
}
FREE(inif->sections);
FREE(inif);
}
static int find_init_section(InitFile *inif, char *section_name)
{
int i;
for (i = 0; i < inif->num_sections; ++i) {
if (!strcmp(inif->sections[i]->section_name, section_name)) {
return i;
}
}
return -1;
}
int delete_init_section(InitFile *inif, char *section_name)
{
int i;
if ((i = find_init_section(inif, section_name)) < 0) {
return INIT_FILE_NOT_PRESENT;
}
free_init_section(inif->sections[i]);
--(inif->num_sections);
inif->sections[i] = inif->sections[inif->num_sections];
return INIT_FILE_PRESENT;
}
int add_init_section(InitFile *inif, InitSection *inis)
{
int i;
InitSection *oinis;
if ((i = find_init_section(inif, inis->section_name)) >= 0) {
oinis = inif->sections[i];
inif->sections[i] = inis;
free_init_section(oinis);
return INIT_FILE_PRESENT;
}
if (inif->num_sections == inif->size_sections) {
inif->size_sections += 10;
inif->sections = REALLOC(inif->sections,
sizeof(InitSection *) * inif->size_sections);
}
inif->sections[inif->num_sections] = inis;
++(inif->num_sections);
return INIT_FILE_NOT_PRESENT;
}
InitSection *lookup_init_section(InitFile *inif, char *section_name)
{
int i;
if ((i = find_init_section(inif,section_name)) < 0) {
return NULL;
}
return inif->sections[i];
}
char *nth_init_section_name(InitFile *inif, int n)
{
if (n >= inif->num_sections) {
return NULL;
}
return inif->sections[n]->section_name;
}
/* Inefficient... */
InitSection *copy_init_section(InitSection *inis, char *new_name)
{
int i;
char *key;
InitSection *ninis = create_init_section(new_name);
i = 0;
while ((key = nth_init_entry_key(inis,i)) != NULL) {
add_init_entry(ninis, key, lookup_init_entry(inis, key));
++i;
}
return ninis;
}
static int find_init_entry(InitSection *inis, char *key)
{
int i;
for (i = 0; i < inis->num_entries; ++i) {
if (!strcmp(inis->entries[i]->key,key)) {
return i;
}
}
return -1;
}
int add_init_entry(InitSection *inis, char *key, char *value)
{
int keylen = strlen(key);
char *buff = ALLOC(sizeof(char) * (keylen + strlen(value) + 2));
InitEntry *inie;
char *obuff;
int i;
strcpy(buff,key);
strcpy(buff+keylen+1,value);
if ((i = find_init_entry(inis,key)) >= 0) {
inie = inis->entries[i];
FREE(inie->key);
inie->key = buff;
inie->value = buff+keylen+1;
return INIT_FILE_PRESENT;
}
inie = ALLOC(sizeof(InitEntry));
inie->key = buff;
inie->value = buff+keylen+1;
if (inis->num_entries == inis->size_entries) {
inis->size_entries += 10;
inis->entries = REALLOC(inis->entries,
sizeof(InitEntry *) * inis->size_entries);
}
inis->entries[inis->num_entries] = inie;
++(inis->num_entries);
return INIT_FILE_NOT_PRESENT;
}
char *lookup_init_entry(InitSection *inis, char *key)
{
int i;
if ((i = find_init_entry(inis,key)) < 0) {
return NULL;
}
return inis->entries[i]->value;
}
char *nth_init_entry_key(InitSection *inis, int n)
{
if (n >= inis->num_entries) {
return NULL;
}
return inis->entries[n]->key;
}
int delete_init_entry(InitSection *inis, char *key)
{
int i;
InitEntry *inie;
if ((i = find_init_entry(inis, key)) < 0) {
return INIT_FILE_NOT_PRESENT;
}
free_init_entry(inis->entries[i]);
--(inis->num_entries);
inis->entries[i] = inis->entries[inis->num_entries];
return INIT_FILE_PRESENT;
}
|