-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
116 lines (89 loc) · 2.38 KB
/
config.c
File metadata and controls
116 lines (89 loc) · 2.38 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "string.h"
#include "config.h"
static int _add_entry(olio_config * c, char * name, char * value)
{
c->entry_count++;
c->entry_names = (char**) realloc(c->entry_names,
sizeof(char*) * c->entry_count);
c->entry_values = (char**) realloc(c->entry_values,
sizeof(char*) * c->entry_count);
if (c->entry_names == NULL || c->entry_values == NULL)
return -1;
c->entry_names[c->entry_count-1] = name;
c->entry_values[c->entry_count-1] = value;
return 0;
}
static int _parse_config_line(olio_config * c, olio_string * str)
{
uint16_t lpos;
uint16_t tpos;
char * name;
char * value;
const char * data = olio_string_contents(str);
uint16_t len = olio_string_length(str);
if (len == 0) return 0;
name = (char *) malloc(sizeof(char) * len);
value = (char *) malloc(sizeof(char) * len);
if (name == NULL || value == NULL)
return -1;
lpos = 0;
while (lpos < len && isspace(data[lpos])) lpos++;
tpos = 0;
while (lpos < len &&
!isspace(data[lpos]) && data[lpos] != ':')
name[tpos++] = data[lpos++];
name[tpos] = 0;
while (lpos < len &&
(isspace(data[lpos]) || data[lpos] == ':'))
lpos++;
tpos = 0;
while (lpos < len)
value[tpos++] = data[lpos++];
while (tpos > 0 && isspace(value[tpos-1])) tpos--;
value[tpos] = 0;
return _add_entry(c, name, value);
}
void olio_config_init(olio_config * c)
{
c->entry_count = 0;
c->entry_names = NULL;
c->entry_values = NULL;
}
void olio_config_free(olio_config * c)
{
uint16_t i;
for (i = 0; i < c->entry_count; i++) {
if (c->entry_names[i] != NULL) free(c->entry_names[i]);
if (c->entry_values[i] != NULL) free(c->entry_values[i]);
}
if (c->entry_names != NULL) free(c->entry_names);
if (c->entry_values != NULL) free(c->entry_values);
}
int olio_config_read(olio_config * c, const char * filename)
{
OLIO_STRING_STACK(str, 128);
int to_eol;
FILE * fp;
fp = fopen(filename, "r");
if (fp == NULL) return -1;
to_eol = 0;
while (!feof(fp)) {
int ci = getc(fp);
if (ci == '#') to_eol = 1;
else if (ci == '\n') {
_parse_config_line(c, str);
olio_string_clear(str);
to_eol = 0;
}
else if (!to_eol) {
if (olio_string_append_char(str, ci))
return -1;
}
}
olio_string_free(str);
fclose(fp);
return 0;
}