-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfread.c
720 lines (542 loc) · 15.7 KB
/
confread.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
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/* ----------------------------------------------------------------------------
SOURCE FILE
Name: confread.c
Program: Configuration Reader
Developer: Andrew Burian
Created On: 2015-03-07
Functions:
struct confread_section* add_section(struct confread_file* file, char* name)
struct confread_pair* add_pair(struct confread_section* section, char* key,
char* value)
struct confread_file* confread_open(char* path)
struct confread_section* confread_find_section(struct confread_file* confFile,
char* name)
struct confread_pair* confread_find_pair(struct confread_section* confSec,
char* key)
char* confread_find_value(struct confread_section* confSec, char* name)
void confread_close(struct confread_file** confFile)
Description:
A config file reading library
This tool is for easy loading and parsing of config files, and includes
methods to easily search for sections and key-value pairs within the config
file.
All values are handled as strings, as it's up to the program to determine what
to do with the given data
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
#include "confread.h"
/* ----------------------------------------------------------------------------
FUNCTION
Name: Add Section
Prototype: struct confread_section* add_section(struct confread_file* file,
char* name)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
struct confread_file* file
The confread file to add a section to
char* name
The name of the section to be added
Return Values:
struct confread_section*
a confread section which is either new, or a pointer to an existing
one of the same name
Description:
*** For internal use
Given a section name, will either create and return a new empty section added
to the file, or will return the existing section if one was found
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
struct confread_section *add_section(struct confread_file *file, char *name)
{
// temp section pointer
struct confread_section *thisSection = 0;
struct confread_section *prevSection = 0;
// ensure this section does not already exist
if ((thisSection = confread_find_section(file, name))) {
return thisSection;
}
// add the new section
thisSection = malloc(sizeof(struct confread_section));
// name the section
thisSection->name = malloc(strlen(name) + 1);
memcpy(thisSection->name, name, strlen(name) + 1);
// set the initial values
thisSection->next = 0;
thisSection->pairs = 0;
// get the first section in the list
prevSection = file->sections;
// special handling for first item
if (!prevSection) {
file->sections = thisSection;
return thisSection;
}
// find the end of the linked list of sections
while (prevSection && prevSection->next) {
prevSection = prevSection->next;
}
// append to list
prevSection->next = thisSection;
return thisSection;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Add Pair
Prototype: struct confread_pair* add_pair(struct confread_section* section,
char* key, char* value)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
struct confread_section* section
the section to add the key-value pair to
char* key
the key portion of the pair
char* value
the value portion of the pair
Return Values:
struct confread_pair*
pointer to the newly added pair
Description:
*** For internal use
Given a pair key, will either create and return a new pair added to the
section with the given value, or an existing pair of the same key with the
value overwritten.
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
struct confread_pair *add_pair(struct confread_section *section, char *key,
char *value)
{
// temp pair pointer
struct confread_pair *thisPair = 0;
struct confread_pair *prevPair = 0;
// ensure pair does not exist
if ((thisPair = confread_find_pair(section, key))) {
// overwrite the value
thisPair->value = realloc(thisPair->value, strlen(value) + 1);
memcpy(thisPair->value, value, strlen(value) + 1);
return thisPair;
}
// add the new pair
thisPair = malloc(sizeof(struct confread_pair));
// name the pair
thisPair->key = malloc(strlen(key) + 1);
memcpy(thisPair->key, key, strlen(key) + 1);
// set the initial value
thisPair->value = malloc(strlen(value) + 1);
memcpy(thisPair->value, value, strlen(value) + 1);
// set as end of list
thisPair->next = 0;
// get the first pair in the section
prevPair = section->pairs;
// special handling for first
if (!prevPair) {
section->pairs = thisPair;
return thisPair;
}
// find the end of the linked list of pairs
while (prevPair && prevPair->next) {
prevPair = prevPair->next;
}
// add to list
prevPair->next = thisPair;
return thisPair;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Open
Prototype: struct confread_file* confread_open(char* path)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
char* path
file path to the conf file to read
Return Values:
struct confread_file*
the newly creaded confread file
null on failure
Description:
Parses the given config file and loads it into the data structure for
processing
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
struct confread_file *confread_open(char *path)
{
// the actual configuration file
FILE *confDataFile = 0;
// temp pointers
struct confread_file *confFile = 0;
struct confread_section *thisSection = 0;
// the line from the conf file
char *line = 0;
size_t lineLen = 0;
// other char pointers for manipulation of the line
char *lineStart = 0;
char *lineEnd = 0;
char *keyStart = 0;
char *keyEnd = 0;
// open the data file
if (!(confDataFile = fopen(path, "r"))) {
return 0;
}
// create the conf file
confFile = malloc(sizeof(struct confread_file));
confFile->name = malloc(strlen(path) + 1);
confFile->sections = 0;
memcpy(confFile->name, path, strlen(path) + 1);
// add the 'root section'
thisSection = add_section(confFile, "root");
// Read the file line by line until EOF
while (getline(&line, &lineLen, confDataFile) != -1) {
// seek forward to the first non-space character
for (lineStart = line; *lineStart != 0; ++lineStart) {
if (!isspace(*lineStart)) {
break;
}
}
// omit if empty
if (*lineStart == 0) {
continue;
}
// omit if comment '#'
if (*lineStart == '#') {
continue;
}
// see if this is a section header
if (*lineStart == '[') {
// move lineStart off the header start
++lineStart;
// seek forward to the end of the header ']'
for (lineEnd = lineStart; *lineEnd != 0; ++lineEnd) {
if (*lineEnd == ']') {
break;
}
}
// if it's anything other than the end of header, invalid line
if (*lineEnd != ']') {
continue;
}
// seek linestart forward to trim whitespace
for (lineStart = lineStart; lineStart != lineEnd;
++lineStart) {
if (!isspace(*lineStart)) {
break;
}
}
// omit zero size headers
if (lineStart == lineEnd) {
continue;
}
// seek line end back to trim whitespace
for (lineEnd = lineEnd - 1; lineEnd != lineStart;
--lineEnd) {
if (!isspace(*lineEnd)) {
break;
}
}
// set next char as null
lineEnd[1] = 0;
// create the new section
thisSection = add_section(confFile, lineStart);
continue;
}
// otherwise, key-value
else {
// seek line end forward to the separator '='
for (lineEnd = lineStart; *lineEnd != 0; ++lineEnd) {
if (*lineEnd == '=') {
break;
}
}
// discard if invalid
if (*lineEnd != '=') {
continue;
}
// if key is zero length, discard
if (lineEnd == lineStart) {
continue;
}
// set the key start
keyStart = lineEnd + 1;
// seek the line end backwards to trim whitespace
for (lineEnd = lineEnd - 1; lineEnd != lineStart;
--lineEnd) {
if (!isspace(*lineEnd)) {
break;
}
}
// set the following char to null
lineEnd[1] = 0;
// seek the key end forward to the end of the line
for (keyEnd = keyStart; *keyEnd != 0; ++keyEnd) {
if (*keyEnd == '\n') {
break;
}
}
// if the key is zero length, discard
if (keyEnd == keyStart) {
continue;
}
// seek key end backwards to trim trailing whitespace
for (keyEnd = keyEnd; keyEnd != keyStart; --keyEnd) {
if (!isspace(*keyEnd)) {
break;
}
}
// seek the key start forward to trim leading whitespace
for (keyStart = keyStart; keyStart != keyEnd;
++keyStart) {
if (!isspace(*keyStart)) {
break;
}
}
// set the new line char to null
keyEnd[1] = 0;
// add the key-value pair
add_pair(thisSection, lineStart, keyStart);
}
}
free(line);
fclose(confDataFile);
return confFile;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Find Section
Prototype: struct confread_section* confread_find_section(
struct confread_file* confFile, char* name)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
struct confread_file* confFile
the file to search for the section in
char* name
the name of the section to search for
Return Values:
struct confread_section*
the section matching the provided name
null on not found
Description:
Searches for a section within a file and returns it if found
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
struct confread_section *confread_find_section(struct confread_file *confFile,
char *name)
{
// temp pointer to section
struct confread_section *thisSection = 0;
// null check
if (!confFile || !name) {
return 0;
}
// set as first section
thisSection = confFile->sections;
// loop through all sections
while (thisSection) {
// check for match
if (!strcmp(thisSection->name, name)) {
break;
}
// next section
thisSection = thisSection->next;
}
// return found section or null
return thisSection;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Find Pair
Prototype: struct confread_pair* confread_find_pair(
struct confread_section* confSec, char* key)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
struct confread_section* confSec
the section to search in
char* key
the key to search for
Return Values:
struct confread_pair*
the pair matching the provided value
null on not found
Description:
Finds a key pair in the section and returns it if found
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
struct confread_pair *confread_find_pair(struct confread_section *confSec,
char *key)
{
// temp pair pointer
struct confread_pair *thisPair = 0;
// null check
if (!confSec || !key) {
return 0;
}
// set to start of pairs
thisPair = confSec->pairs;
// iterate through all pairs in section
while (thisPair) {
// check if the key matches target key
if (!strcmp(thisPair->key, key)) {
break;
}
// get next pair
thisPair = thisPair->next;
}
// return found pair or null
return thisPair;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Find Value
Prototype: struct confread_pair* confread_find_pair(
struct confread_section* confSec, char* key)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
struct confread_section* confSec
the section to search in
char* key
the key to search for
Return Values:
char*
the value matching the key in the provided section
null on not found
Description:
Finds a key pair in the section and returns only it's value if found
Revisions:
(none)
---------------------------------------------------------------------------- */
char *confread_find_value(struct confread_section *confSec, char *key)
{
struct confread_pair *thisPair = 0;
// get the pair
if (!(thisPair = confread_find_pair(confSec, key))) {
return 0;
}
return thisPair->value;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Close
Prototype: void confread_close(struct confread_file** confFile)
Developer: Andrew Burian
Created On: 2015-03-07
Parameters:
struct confread_file** confFile
a pointer to the address of the confread file to be freed
Description:
Properly frees and closes all resources used by the confread file, and sets
the pointer to null
Revisions:
Andrew Burian
2015-08-21
Revised sections and pair to be a linked list rather than
reallocating continuous memory sections.
---------------------------------------------------------------------------- */
void confread_close(struct confread_file **confFile)
{
// temp section pointer
struct confread_section *thisSection = 0;
struct confread_section *nextSection = 0;
// temp pair pointer
struct confread_pair *thisPair = 0;
struct confread_pair *nextPair = 0;
// null check
if (!confFile || !(*confFile)) {
return;
}
// get the first section
thisSection = (*confFile)->sections;
// loop through all sections in the file
while (thisSection) {
// get the first pair in this section
thisPair = thisSection->pairs;
// loop through each key pair in the section
while (thisPair) {
// get the next pointer before freeing anything
nextPair = thisPair->next;
// free the two data elements in the pair
free(thisPair->key);
free(thisPair->value);
// free the pair itself
free(thisPair);
// move to the next one
thisPair = nextPair;
}
// get the next pointer before freeing anything
nextSection = thisSection->next;
// all pairs have been freed, now free the section data
free(thisSection->name);
// free the section itself
free(thisSection);
// move to the next one
thisSection = nextSection;
}
// free the file section data
free((*confFile)->name);
// free the conf file itself
free(*confFile);
*confFile = 0;
}
/* ----------------------------------------------------------------------------
FUNCTION
Name: Check Value
Prototype: int confread_check_pair(struct confread_section *section,
char* key, char *value);
Developer: Andrew Burian
Created On: 2015-08-21
Parameters:
struct confread_section *secion
the section to search for the key-value pair in
char *key
the key of the pair to search for
char *value
the value to check if the key is found
Return Values:
int
boolean result
1 if the key is present and the value matches
0 otherwise
Description:
Searches for the given key in the given section, and if it exists
checks to see if the provided value matches the one in the pair.
Revisions:
(none)
---------------------------------------------------------------------------- */
int confread_check_pair(struct confread_section *section, char *key, char *value){
char *thisValue = 0;
// sanity checks
if(!section || !key || !value){
return 0;
}
// attempt to find the value
thisValue = confread_find_value(section, key);
if(!thisValue){
return 0;
}
return (!strcmp(value, thisValue));
}