-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcanfigger.c
473 lines (382 loc) · 9.58 KB
/
canfigger.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
/*
This file is part of canfigger<https://github.com/andy5995/canfigger>
MIT License
Copyright (c) 2024 Andy Alt([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <ctype.h> // isspace()
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h> // free(), malloc()
#include <string.h>
// This is only required for version info and can be removed
// if you're copying the canfigger source files to use as
// an embedded library with your own project (i.e., not building
// canfigger with the build system it's shipped with).
#include "config.h"
#include "canfigger.h"
static char *grab_str_segment(char *a, char **dest, const int c);
/** \cond */
struct line
{
size_t len;
char *start;
char *end;
};
/** \endcond */
static char *
strclone(const char *src, size_t n)
{
char *dest = NULL;
if (n == 0)
{
dest = malloc(strlen(src) + 1);
if (dest)
strcpy(dest, src);
}
else
{
dest = malloc(n + 1);
if (dest)
{
memcpy(dest, src, n);
dest[n] = '\0';
}
}
if (!dest)
perror("malloc (canfigger)");
return dest;
}
void
canfigger_free_current_attr_str_advance(struct attributes *attributes,
char **attr)
{
if (!attributes)
{
*attr = NULL;
return;
}
if (attributes->current && attributes->iter_ptr)
free(attributes->current);
if (!attributes->iter_ptr)
{
free(attributes->current);
attributes->current = NULL;
*attr = NULL;
return;
}
attributes->iter_ptr = grab_str_segment(attributes->iter_ptr,
&attributes->current, '\n');
if (*attributes->current)
{
*attr = attributes->current;
return;
}
// If we're here, that means strdup() failed to allocate memory in grab_str_segment()
// If an expected attribute isn't returned, the caller may want to terminate
// the remainder of the loop that's iterating through the entire linked list
// and exit the program.
*attr = NULL;
return;
}
void
canfigger_free_current_key_node_advance(struct Canfigger **node)
{
if (*node)
{
if ((*node)->attributes)
{
if ((*node)->attributes->current)
{
free((*node)->attributes->current);
(*node)->attributes->current = NULL;
}
if ((*node)->attributes->str)
{
free((*node)->attributes->str);
(*node)->attributes->str = NULL;
}
free((*node)->attributes);
(*node)->attributes = NULL;
}
if ((*node)->value)
{
free((*node)->value);
(*node)->value = NULL;
}
free((*node)->key);
(*node)->key = NULL;
struct Canfigger *temp_node = (*node)->next;
free(*node);
*node = temp_node;
}
return;
}
void
canfigger_free_list(struct Canfigger **node)
{
if (*node)
{
while (*node)
canfigger_free_current_key_node_advance(node);
}
return;
}
/*
* returns a pointer to the first character after lc
* If lc appears more than once, the pointer
* will move past that as well.
*
* Ex1: "__Hello World": the pointer will be set to the 'H'.
* Ex2: "_H_ello World": Again, the pointer will be set to the 'H'.
*/
static char *
erase_lead_char(const int lc, char *haystack)
{
char *ptr = haystack;
if (*ptr != lc)
return ptr;
while (*ptr == lc)
ptr++;
return ptr;
}
static void
truncate_whitespace(char *str)
{
if (!str)
return;
char *pos_0 = str;
/* Advance pointer until NULL terminator is found
* Don't try to use strchr() because you'll get a different
* result if the pointer is already at '\0'. */
while (*str != '\0')
str++;
/* set pointer to segment preceding NULL terminator */
if (str != pos_0)
str--;
else
return;
while (isspace(*str))
{
*str = '\0';
if (str != pos_0)
str--;
else
break;
}
return;
}
static char *
grab_str_segment(char *a, char **dest, const int c)
{
a = erase_lead_char(' ', a);
char *b = strchr(a, c);
if (!b)
{
*dest = strclone(a, 0);
return b;
}
size_t len = b - a;
*dest = strclone(a, len);
if (!*dest)
return NULL;
truncate_whitespace(*dest);
return b + 1;
}
static void *
malloc_wrap(size_t size)
{
void *retval = malloc(size);
if (retval)
return retval;
perror("Failed to allocate memory: ");
return NULL;
}
static void
add_key_node(struct Canfigger **root, struct Canfigger **cur_node)
{
struct Canfigger *tmp_node = malloc_wrap(sizeof(struct Canfigger));
if (!tmp_node)
return;
if (*root)
(*cur_node)->next = tmp_node;
else
*root = tmp_node;
*cur_node = tmp_node;
return;
}
static char *
read_entire_file(const char *filename)
{
FILE *fp = fopen(filename, "rb");
if (!fp)
{
fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
return NULL;
}
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
if (file_size < 0)
{
fprintf(stderr, "Error getting the size of %s: %s\n", filename,
strerror(errno));
fclose(fp);
return NULL;
}
fseek(fp, 0, SEEK_SET);
char *buffer = malloc_wrap(file_size + 1);
if (!buffer)
{
fclose(fp);
return NULL;
}
size_t n_bytes = fread(buffer, 1, file_size, fp);
if (ferror(fp))
{
fprintf(stderr, "Error reading %s: %s\n", filename, strerror(errno));
free(buffer);
fclose(fp);
return NULL;
}
// Note that if the return value of ftell() is -1 this cast would be bad.
// However, above, the return value of ftell() is checked, and the function
// returns if the value is < 0
if (n_bytes == (size_t) file_size)
{
buffer[file_size] = '\0';
fclose(fp);
return buffer;
}
free(buffer);
fprintf(stderr, "Partial read of %s: expected %ld bytes, got %zu bytes\n",
filename, file_size, n_bytes);
fclose(fp);
return NULL;
}
static void
free_incomplete_node(struct Canfigger **node)
{
if (*node)
{
if ((*node)->key)
free((*node)->key);
if ((*node)->value)
free((*node)->value);
if ((*node)->attributes)
if ((*node)->attributes->str)
free((*node)->attributes->str);
}
free(*node);
return;
}
struct Canfigger *
canfigger_parse_file(const char *file, const int delimiter)
{
struct Canfigger *root = NULL, *cur_node = NULL;
char *buffer = read_entire_file(file);
if (buffer == NULL)
return NULL;
char file_contents[strlen(buffer) + 1];
memcpy(file_contents, buffer, sizeof file_contents);
free(buffer);
struct line line;
line.start = file_contents;
line.end = strchr(line.start, '\n');
bool node_complete;
while (line.end)
{
line.len = line.end - line.start;
char tmp_line[line.len + 1];
memcpy(tmp_line, line.start, line.len);
tmp_line[line.len] = '\0';
// Used in the next loop
if (line.end)
{
line.start = line.end + 1;
line.end = strchr(line.start, '\n');
}
char *line_ptr = tmp_line;
truncate_whitespace(line_ptr);
while (isspace(*line_ptr))
line_ptr = erase_lead_char(*line_ptr, line_ptr);
if (*line_ptr == '\0' || *line_ptr == '#' || *line_ptr == '[')
continue;
node_complete = false;
add_key_node(&root, &cur_node);
if (!cur_node)
break;
// Get key
cur_node->key = NULL;
line_ptr = grab_str_segment(line_ptr, &cur_node->key, '=');
if (!cur_node->key)
{
free_incomplete_node(&cur_node);
break;
}
// Get value
cur_node->value = NULL;
if (line_ptr)
{
line_ptr = grab_str_segment(line_ptr, &cur_node->value, delimiter);
if (!cur_node->value)
{
free_incomplete_node(&cur_node);
break;
}
}
// Handle attributes
if (line_ptr)
{
cur_node->attributes = malloc_wrap(sizeof(struct attributes));
if (!cur_node->attributes)
{
free_incomplete_node(&cur_node);
break;
}
struct attributes *attr_ptr = cur_node->attributes;
attr_ptr->current = NULL;
attr_ptr->str = strclone(line_ptr, 0);
if (!attr_ptr->str)
{
free_incomplete_node(&cur_node);
break;
}
attr_ptr->iter_ptr = attr_ptr->str;
// Change the delimiter, which will be used later
// in canfigger_free_current_attr_str_advance()
char *delimiter_ptr = strchr(attr_ptr->iter_ptr, delimiter);
while (delimiter_ptr)
{
*delimiter_ptr = '\n';
delimiter_ptr = strchr(delimiter_ptr, delimiter);
}
}
else
cur_node->attributes = NULL;
cur_node->next = NULL;
node_complete = true;
}
if (!root)
return NULL;
if (!node_complete)
{
canfigger_free_list(&root);
return NULL;
}
return root;
}