-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathoption_list.cpp
More file actions
184 lines (173 loc) · 3.92 KB
/
option_list.cpp
File metadata and controls
184 lines (173 loc) · 3.92 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
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
//========================================================================
// Option list
//========================================================================
// @brief: read and compare parameters
#include "option_list.h"
// read cfg data
list *read_data_cfg(char *filename)
{
FILE *file = fopen(filename, "r");
if (file == 0)
{
file_error(filename);
}
char *line;
list *options = make_list();
int nu = 0;
// get each line
while((line=fgetl(file)) != 0)
{
nu++;
//printf("nu:%d; line: %s;\n",nu,line);
strip(line);
switch(line[0])
{
case '\0':
case '#':
case ';':
{
free(line);
break;
}
default:
{
if (!read_option(line, options))
{
fprintf(stderr,"Config file error line %d, could parse: %s\n",nu,line);
free(line);
}
break;
}
}
}
fclose(file);
return options;
}
// change "=" to "\n", and insert it into a list option
// val stores the address of string of value
int read_option(char *s, list *options)
{
size_t i;
size_t len = strlen(s);
char *val=0;
// split the string s within "="
for (i = 0; i < len; i++)
{
if (s[i] == '=')
{
s[i] = '\0';
val = s+i+1;
break;
}
}
//
if(i == len-1)
{ // no value for this key, insert failed: return 0
return 0;
}
char *key = s;
option_insert(options, key, val);
// successfully insert key&value into option: return 1
return 1;
}
// insert value(*val) into list option
void option_insert(list *l, char *key, char *val)
{
kvp *p = (kvp *)malloc(sizeof(kvp));
p->key = key;
p->val = val;
p->used = 0;
list_insert(l, p);
}
// check specific strings (keys)
char *option_find_str(list *l, char *key, char *def)
{
char *v = option_find(l, key);
if (v)
{ //
return v;
}
if (def)
{ // use default cfg
fprintf(stderr, "%s: Using default '%s' \n", key, def);
}
return def;
}
//traverse the list l
char *option_find (list *l, char *key)
{
node *n = l->front;
// traverse the list from the first node l->front
while(n)
{
kvp *p = (kvp *)n->val;
if (strcmp(p->key, key) == 0)
{
p->used = 1;
return p->val;
}
n = n->next;
}
// no match key found, return 0
return 0;
}
// ???
void option_unused (list *l)
{
node *n = l->front;
// traverse the list from the first node l->front
while(n)
{
kvp *p = (kvp *)n->val;
if(!p->used)
{
fprintf(stderr, "Unused field: '%s' = '%s'\n", p->key, p->val);
}
n = n->next;
}
}
// find specific ints
int option_find_int(list *l, char *key, int def)
{
char *v = option_find(l, key);
if (v)
{
return atoi(v);
}
if (def)
{
fprintf(stderr, "%s: Using default '%d'\n", key, def);
}
return def;
}
// find specific ints
int option_find_int_quiet(list *l, char *key, int def)
{
char *v = option_find(l, key);
if (v)
{
return atoi(v);
}
return def;
}
// find specific floats
float option_find_float(list *l, char *key, float def)
{
char *v = option_find(l, key);
if (v)
{
return atof(v);
}
fprintf(stderr, "%s: Using default: '%lf'\n", key, def);
return def;
}
// find specific floats
float option_find_float_quiet(list *l, char *key, float def)
{
char *v = option_find(l, key);
if (v)
{
return atof(v);
}
return def;
}