-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigz.c
335 lines (317 loc) · 5.5 KB
/
configz.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "configz.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define BUFFER 1024
///去掉两边的空格
void trim(char * buf)
{
if(buf == NULL)
{
return;
}
int i = strlen(buf)-1;
while(isspace(buf[i]) && i >= 0)
{
i--;
}
buf[i+1] = '\0';
char * t = buf;
while(isspace(*t))
{
t++;
}
if(*t == '\0')
{
*buf = '\0';
}
else
{
strcpy(buf,t);
}
}
///判断是否为注释行,会对line进行修改(不会改变有效内容)
int isCommentLine(char * line)
{
trim(line);
if(line == NULL || strlen(line) == 0 || line[0] == '#')
{
return 1;
}
else
{
return 0;
}
}
///判断是否为Label行,会对line进行修改(不会改变有效内容)
int isLabel(char * line)
{
if(strlen(line) < 2)
{
return 0;
}
char * content = strtok(line,"#");
strcpy(line,content);
if(line == NULL)
{
return 0;
}
trim(line);
int len =strlen(line);
if(len < 2)
{
return 0;
}
if(line[0] == '[' && line[len-1] == ']')
{
return 1;
}
else
{
return 0;
}
}
///从打开文件中的当前位置开始读取key的对应value,找到或者文件结束或者读到下一个Label行则返回
int readKV(FILE * file, const char * key, char * value)
{
char buffer[BUFFER];
memset(buffer,0,BUFFER);
int key_len = strlen(key);
while(fgets(buffer,BUFFER,file) != NULL)
{
if(isCommentLine(buffer))
{
continue;
}
if(isLabel(buffer))
{
break;
}
char * t_key = strtok(buffer,"=");
if(t_key == NULL)
{
continue;
}
trim(t_key);
if(strlen(t_key) != key_len || strcmp(t_key,key) !=0)
{
continue;
}
char* t_value = strtok(NULL,"#");
if(t_value == NULL)
{
fclose(file);
return -1;
}
trim(t_value);
if(strlen(t_value) == 0)
{
fclose(file);
return -1;
}
strcpy(value,t_value);
fclose(file);
return 0;
}
fclose(file);
return -1;
}
int configz_readKV(const char * configfile, const char * key, char * value)
{
FILE * file =fopen(configfile,"r");
if(file == NULL)
{
return -1;
}
return readKV(file, key, value);
}
int configz_countLabel(const char * configfile,const char * label)
{
FILE * file =fopen(configfile,"r");
if(file == NULL)
{
return -1;
}
char buffer[BUFFER];
memset(buffer,0,BUFFER);
int label_len = strlen(label);
int label_num = 0;
while(fgets(buffer,BUFFER,file) != NULL)
{
if(isCommentLine(buffer))
{
continue;
}
if(isLabel(buffer) && strlen(buffer) == label_len+2 && strncmp(buffer+1,label,label_len) ==0)
{
label_num++;
}
}
fclose(file);
return label_num;
}
int configz_readLKV(const char * configfile, const char * label, const char * key, char * value, int num)
{
if(num < 1)
{
return -1;
}
FILE * file =fopen(configfile,"r");
if(file == NULL)
{
return -1;
}
char buffer[BUFFER];
memset(buffer,0,BUFFER);
int label_len = strlen(label);
int label_num = 0;
while(fgets(buffer,BUFFER,file) != NULL)
{
if(isCommentLine(buffer))
{
continue;
}
if(isLabel(buffer) && strlen(buffer) == label_len+2 && strncmp(buffer+1,label,label_len) ==0)
{
label_num++;
if(label_num == num)
{
break;
}
}
}
if(label_num != num)
{
fclose(file);
return -1;
}
return readKV(file, key, value);
}
int configz_readSLKV(const char * configfile, const char * label, const char * key, char * value)
{
return configz_readLKV(configfile, label, key, value, 1);
}
int configz_readString(const char * configfile, const char * label, const char * key, char * value, int num)
{
if(configfile == NULL || key == NULL)
{
return -1;
}
if(label == NULL)
{
return configz_readKV(configfile, key, value);
}
else if(num == 0)
{
return configz_readSLKV(configfile, label, key, value);
}
else
{
return configz_readLKV(configfile, label, key, value, num);
}
}
int isValidIP(char * buf)
{
if(buf == NULL)
{
return 0;
}
int len = strlen(buf);
if(len >15 || len < 7)
{
return 0;
}
char * p;
p = buf;
int numCount = 0;
int pointCount = 0;
char ipnum[4];
memset(ipnum,0,4);
while(*p != '\0')
{
if(*p >='0' && *p <='9')
{
if(numCount > 2)
{
return 0;
}
ipnum[numCount] = *p;
numCount++;
}
else if(*p =='.')
{
pointCount++;
if(pointCount > 3)
{
return 0;
}
if(numCount == 0)
{
return 0;
}
else
{
ipnum[numCount] = '\0';
int num = atoi(ipnum);
if(num < 0 || num > 255)
{
return 0;
}
numCount = 0;
}
}
else
{
return 0;
}
p++;
}
return 1;
}
int configz_readIP(const char * configfile, const char * label, const char * key, char * value, int num)
{
if(configz_readString(configfile, label, key, value, num) != 0)
{
return -1;
}
if(isValidIP(value))
{
return 0;
}
return -1;
}
/*int main()
{
char value[100];
memset(value,0 ,100);
char * conffile = "label.ini";
printf("%d\n",configz_countLabel(conffile,"base"));
printf("%d\n",configz_countLabel(conffile,"tcp"));
printf("%d\n",configz_countLabel(conffile,"udp"));
if(configz_readString(conffile,"base","test",value,0) ==0)
{
printf("base test = %s\n",value);
}
if(configz_readString(conffile,"base","test",value,2) ==0)
{
printf("base test2 = %s\n",value);
}
if(configz_readString(conffile,"base","test",value,3) ==0)
{
printf("base test3 = %s\n",value);
}
if(configz_readString(conffile,"base","test",value,4) ==0)
{
printf("base test4 = %s\n",value);
}
if(configz_readIP(conffile,"base","ip",value,4) ==0)
{
printf("base ip = %s\n",value);
}
if(configz_readIP(conffile,"base","addr",value,4) ==0)
{
printf("base addr = %s\n",value);
}
return 0;
}*/