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
|
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cfg.h"
int
cfgsetup(struct cfgfile *cfg, char *filename)
{
/*
* sets up the cfgfile struct pointed to by cfg, using the cfg
* file location pointed to by filename
*/
cfg->count = 0;
cfg->file = strdup(filename);
if (!cfg->file)
return 0;
FILE *fp;
fp = fopen(cfg->file, "r");
if (!fp)
return 0;
if (!readfile(cfg, fp))
{
fclose(fp);
return 0;
}
fclose(fp);
//printlist(cfg->headnode);
return 1;
}
void
cfgfree(struct cfgfile *cfg)
{
/*
* frees all memory allocated by cfg
*/
freenodes(cfg->headnode);
free(cfg->file);
free(cfg);
}
int
cfggetvalue(struct cfgfile *cfg, char *key, char *buffer, size_t buffersize)
{
/*
* stores the value from the option pointed to by key in buffer with
* a maximum size of buffersize
*/
if (!cfg->headnode)
return 0;
struct cfgnode *tmp = cfg->headnode;
while (tmp)
{
if (strcmp(key, tmp->key) == 0)
{
strncpy(buffer, tmp->value, buffersize);
return 1;
}
tmp = tmp->next;
}
return 0;
}
void
cfgappendchar(char *dest, char ch)
{
/*
* appends char ch to dest
* assumes there is enough allocated memory
*/
int len = strlen(dest);
dest[len] = ch;
dest[len+1] = '\0';
}
int
readfile(struct cfgfile *cfg, FILE *fp)
{
/*
* read file pointer to by fp parsing lines
* storing them in the cfgfile structure cfg
*/
cfg->headnode = createnode();
if (!cfg->headnode)
return 0;
char ch;
size_t bufsize = 30;
size_t linesize = 0;
char *line = NULL;
char *buffer = malloc(bufsize);
if (!buffer)
return 0;
while ((ch = getc(fp)) != EOF)
{
if (!(ch == '\n') && !(ch == '\r'))
{
if (linesize >= bufsize-1)
{
bufsize += 10;
buffer = realloc(buffer, bufsize);
if (!buffer)
return 0;
}
cfgappendchar(buffer, ch);
linesize += sizeof(char);
}
else
{
free(line);
line = strdup(buffer);
if (!line)
{
free(buffer);
return 0;
}
if (!parseline(line, cfg))
{
free(line);
free(buffer);
return 0;
}
buffer[0] = '\0';
/* I could free buffer here, though I don't
* see a point, we already have the memory
* allocated, may as well keep it */
linesize = 0;
}
}
free(line);
free(buffer);
return 1;
}
int
parseline(char *line, struct cfgfile *cfg)
{
/* parse line pointed to by line string
* results in a cfgnode node appended to head */
/* determine if the line is a comment (#)
* also strip whitespaces from the beginning */
char ch = line[0];
while (ch == ' ' || ch == '\t')
{
memmove(line, line+1, strlen(line));
ch = line[0];
}
if (ch == '#')
return 1;
else
{
// TODO: strip whitespaces from the key and value
// while key[strlen(key)-1 == ' ']
struct cfgnode *tmp;
if (cfg->count == 0)
tmp = cfg->headnode;
else
{
tmp = createnode();
if (!tmp)
return 0;
if (!addnode(cfg->headnode, tmp))
return 0;
}
// malloc memory for key and value
int c = 0;
int doingkey = 1;
size_t keybufsize = 1;
size_t keysize = 255;
size_t valbufsize = 255;
size_t valsize = 0;
tmp->key = malloc(keybufsize);
tmp->value = malloc(valbufsize);
if (!tmp->key)
return 0;
while ((ch = line[c]) != '\0')
{
if (doingkey)
{
if (ch == '=')
doingkey = 0;
else
{
if (keysize >= keybufsize-1)
{
keybufsize += 10;
tmp->key = realloc(tmp->key, keybufsize);
if (!tmp->key)
return 0;
}
cfgappendchar(tmp->key, ch);
valsize += sizeof(char);
}
}
else
{
if (valsize >= valbufsize-1)
{
valbufsize += 10;
tmp->value = realloc(tmp->value, valbufsize);
if (!tmp->value)
return 0;
}
cfgappendchar(tmp->value, ch);
valsize += sizeof(char);
}
c++;
}
cfg->count++;
}
return 1;
}
struct cfgnode
*createnode(void)
{
/*
* create a cfgnode and return a pointer to it
*/
struct cfgnode *tmp = malloc(sizeof(struct cfgnode));
if (!tmp)
return NULL;
tmp->next = NULL;
tmp->key = NULL;
tmp->value = NULL;
return tmp;
}
int
addnode(struct cfgnode *head, struct cfgnode *node)
{
/*
* add a cfgnode to the end head
*/
if (!head || !node)
return 0;
struct cfgnode *tmp = head;
while (tmp->next)
tmp = tmp->next;
tmp->next = node;
return 1;
}
void
freenodes(struct cfgnode *head)
{
/*
* free all cfgnode in the linked list
*/
if (!head)
return;
struct cfgnode *tmp;
while (head)
{
tmp = head;
head = head->next;
free(tmp->key);
free(tmp->value);
free(tmp);
}
}
void
printlist(struct cfgnode *head)
{
struct cfgnode *tmp = head;
while (tmp)
{
printf("key: %s\nvalue: %s\n", tmp->key, tmp->value);
tmp = tmp->next;
}
}
|