-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibconfc.c
212 lines (194 loc) · 4.8 KB
/
libconfc.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "libconfc.h"
conf_t *conf_init()
{
conf_t *cfg = malloc(sizeof *cfg);
cfg->key = NULL;
cfg->value = NULL;
cfg->next = NULL;
return cfg;
}
// initialize first element, update or add config element to the end of chain
int conf_set_val(conf_t *cfg, const char *key, const char *value)
{
// case 1: set values right after conf_init()
if(cfg->key == NULL)
{
if((cfg->key = malloc(strlen(key) + 1)) == NULL) return 0;
strcpy(cfg->key, key);
if((cfg->value = malloc(strlen(value) + 1)) == NULL) {
free(cfg->key);
cfg->key = NULL;
return 0;
}
strcpy(cfg->value, value);
return 1;
}
conf_t *c;
for(c = cfg ; ; c = c->next)
{
// case 2: key matched, update value
if(!strcasecmp(key, c->key))
{
free(c->value);
if((c->value = malloc(strlen(value) + 1)) == NULL) return 0;
strcpy(c->value, value);
return 1;
}
// case 3: key not matched and this is last struct, add new struct to the end of the chain
if(c->next == NULL)
{
if((c->next = malloc(sizeof(conf_t))) == NULL) return 0;
c = c->next;
if((c->key = malloc(strlen(key) + 1)) == NULL) {
free(c->next);
c->next = NULL;
return 0;
}
strcpy(c->key, key);
if((c->value = malloc(strlen(value) + 1)) == NULL) {
free(c->key);
c->key = NULL;
free(c);
c = NULL;
return 0;
}
strcpy(c->value, value);
c->next = NULL;
return 1;
}
}
}
// get value by key
char *conf_get_val(conf_t *cfg, const char *key)
{
conf_t *c;
for(c = cfg; c != NULL; c = c->next)
{
if(!strcasecmp(key, c->key))
{
return c->value;
}
}
return NULL;
}
// remove config element from chain
void conf_remove(conf_t **cfg, const char *key)
{
conf_t *c, *cp = *cfg;
for(c = *cfg; c != NULL; c = c->next)
{
if(!strcasecmp(key, c->key))
{
if(c == *cfg) *cfg = c->next;
cp->next = c->next;
free(c->key); c->key = NULL;
free(c->value); c->value = NULL;
free(c); c = NULL;
return;
}
cp = c;
}
}
// load config from file
int conf_load(conf_t *cfg, const char *file_name)
{
char line_buf[128] = { 0 };
char key[64] = { 0 };
char value[64] = { 0 };
FILE *f = fopen(file_name, "r");
if(!f) return 0;
int read_ok = 0;
while(fgets(line_buf, sizeof line_buf, f) != NULL)
{
if(line_buf[0] == '#' || line_buf[0] == '\n') continue;
if((sscanf(line_buf, "%[^:\t =\n]%*[:\t =\n]%s", key, value) != 2)) continue;
conf_set_val(cfg, key, value);
memset(key, 0x0, sizeof(key));
memset(value, 0x0, sizeof(value));
read_ok = 1;
}
if(!read_ok) return 0;
fclose(f);
return 1;
}
// save config to file
int conf_save(conf_t *cfg, const char *file_name)
{
FILE* f;
f = fopen(file_name, "r");
if(!f)
{
f = fopen(file_name, "w");
if(!f) return 0;
conf_t *c;
for(c = cfg; c != NULL; c = c->next)
{
if(fprintf(f, "%s = %s\n", c->key, c->value) < 0)
{
fclose(f);
return 0;
}
}
}
fclose(f);
return 1;
}
// debug: print all elements of config
void conf_print(conf_t *cfg)
{
conf_t * ct = cfg;
do {
printf("%s = %s\n", ct->key, ct->value);
ct = ct->next;
} while(ct != NULL);
}
// check if value matches or key exists
int conf_check_val(conf_t *cfg, const char *key, const char *value)
{
char *val = conf_get_val(cfg, key);
// if there is no such key
if(val == NULL) return 0;
// if value matches
if(!strcmp(val, value)) return 1;
// otherwise
return 0;
}
// adds config element to the beginning of chain
int conf_add_1st(conf_t **cfg, const char *key, const char *value)
{
conf_t *c;
if((c = malloc(sizeof(conf_t))) == NULL) return 0;
if((c->key = malloc(strlen(key) + 1)) == NULL) {
free(c);
c = NULL;
return 0;
}
strcpy(c->key, key);
if((c->value = malloc(strlen(value) + 1)) == NULL) {
free(c->key);
c->key = NULL;
free(c);
c = NULL;
return 0;
}
strcpy(c->value, value);
c->next = *cfg;
*cfg = c;
return 1;
}
// deletes whole config from memory
void conf_free(conf_t *cfg)
{
conf_t *c = cfg, *ct;
do {
ct = c;
c = c->next;
free(ct->key); ct->key = NULL;
free(ct->value); ct->value = NULL;
free(ct); ct = NULL;
} while (c != NULL);
}