-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzt_conf.c
50 lines (46 loc) · 949 Bytes
/
zt_conf.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
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "zt_conf.h"
void freeOpts(options_t *opts) {
int i = 0;
if ( opts == NULL ) {
return;
}
while ( opts[i].name ) {
if ( opts[i].value) free(opts[i].value);
i++;
}
}
int readConf(const char *me, const char *file, options_t opts[]) {
FILE *fp = NULL;
char buf[1024];
int i;
if ( file == NULL ) {
return 1;
}
if ((fp = fopen(file,"r")) == NULL ) {
fprintf(stderr,"%s: Error opening %s %s\n",me, file,strerror(errno));
return 1;
}
options_t *opt = opts;
char key[1024];
char value[1024];
while ( fgets(buf, sizeof(buf), fp) ) {
*key = *value = '\0';;
sscanf(buf,"%s %s", key, value );
opt = &opts[0];
for ( i = 0;; i++ ) {
opt = &opts[i];
if ( opt == NULL || opt->name == NULL)
break;
if ( strcmp(opt->name,key) == 0) {
opt->value = strdup(value);
}
}
}
fclose(fp);
return 0;
}