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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
#include <linux/string.h>
#include <asm/unistd.h>
#include <linux/slab.h>
#include "ini.h"
char CFG_SSL = '['; /* Ïî±êÖ¾·ûSection Symbol --¿É¸ù¾ÝÌØÊâÐèÒª½øÐж¨Òå¸ü¸Ä£¬Èç { }µÈ*/
char CFG_SSR = ']'; /* Ïî±êÖ¾·ûSection Symbol --¿É¸ù¾ÝÌØÊâÐèÒª½øÐж¨Òå¸ü¸Ä£¬Èç { }µÈ*/
char CFG_NIS = ':'; /* name Óë index Ö®¼äµÄ·Ö¸ô·û */
char CFG_NTS = '#'; /* ×¢ÊÍ·û*/
static char * ini_str_trim_r(char * buf);
static char * ini_str_trim_l(char * buf);
static int ini_file_get_line(char *filedata, char *buffer, int maxlen);
static int ini_split_key_value(char *buf, char **key, char **val);
static long atol(char *nptr);
/*************************************************************
Function: »ñµÃkeyµÄÖµ
Input: char * filedata¡¡Îļþ£»char * section¡¡ÏîÖµ£»char * key¡¡¼üÖµ
Output: char * value¡¡keyµÄÖµ
Return: 0 SUCCESS
-1 δÕÒµ½section
-2 δÕÒµ½key
-10 Îļþ´ò¿ªÊ§°Ü
-12 ¶ÁÈ¡Îļþʧ°Ü
-14 Îļþ¸ñʽ´íÎó
-22 ³¬³ö»º³åÇø´óС
Note:
*************************************************************/
int ini_get_key(char *filedata, char * section, char * key, char * value)
{
//char buf1[MAX_CFG_BUF + 1], buf2[MAX_CFG_BUF + 1];
char *buf1, *buf2;
char *key_ptr, *val_ptr;
int n, ret;
int dataoff = 0;
*value='\0';
buf1 = kzalloc(MAX_CFG_BUF + 1, GFP_KERNEL);
if(!buf1){
printk("buf1: mem alloc failed.\n");
return -ENOMEM;
}
buf2 = kzalloc(MAX_CFG_BUF + 1, GFP_KERNEL);
if(!buf2){
printk("buf2: mem alloc failed.\n");
kfree(buf1);
return -ENOMEM;
}
while(1) { /* ËÑÕÒÏîsection */
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf1, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto r_cfg_end;
ret = CFG_SECTION_NOT_FOUND;
if(n < 0)
goto r_cfg_end; /* Îļþβ£¬Î´·¢ÏÖ */
n = strlen(ini_str_trim_l(ini_str_trim_r(buf1)));
if(n == 0 || buf1[0] == CFG_NTS)
continue; /* ¿ÕÐÐ »ò ×¢ÊÍÐÐ */
ret = CFG_ERR_FILE_FORMAT;
if(n > 2 && ((buf1[0] == CFG_SSL && buf1[n-1] != CFG_SSR)))
goto r_cfg_end;
if(buf1[0] == CFG_SSL) {
buf1[n-1] = 0x00;
if(strcmp(buf1+1, section) == 0)
break; /* ÕÒµ½Ïîsection */
}
}
while(1){ /* ËÑÕÒkey */
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf1, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto r_cfg_end;
ret = CFG_KEY_NOT_FOUND;
if(n < 0)
goto r_cfg_end;/* Îļþβ£¬Î´·¢ÏÖkey */
n = strlen(ini_str_trim_l(ini_str_trim_r(buf1)));
if(n == 0 || buf1[0] == CFG_NTS)
continue; /* ¿ÕÐÐ »ò ×¢ÊÍÐÐ */
ret = CFG_KEY_NOT_FOUND;
if(buf1[0] == CFG_SSL)
goto r_cfg_end;
if(buf1[n-1] == '+') { /* Óö+ºÅ±íʾÏÂÒ»ÐмÌÐø */
buf1[n-1] = 0x00;
while(1) {
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf2, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto r_cfg_end;
if(n < 0)
break;/* Îļþ½áÊø */
n = strlen(ini_str_trim_r(buf2));
ret = CFG_ERR_EXCEED_BUF_SIZE;
if(n > 0 && buf2[n-1] == '+'){/* Óö+ºÅ±íʾÏÂÒ»ÐмÌÐø */
buf2[n-1] = 0x00;
if( (strlen(buf1) + strlen(buf2)) > MAX_CFG_BUF)
goto r_cfg_end;
strcat(buf1, buf2);
continue;
}
if(strlen(buf1) + strlen(buf2) > MAX_CFG_BUF)
goto r_cfg_end;
strcat(buf1, buf2);
break;
}
}
ret = CFG_ERR_FILE_FORMAT;
if(ini_split_key_value(buf1, &key_ptr, &val_ptr) != 1)
goto r_cfg_end;
ini_str_trim_l(ini_str_trim_r(key_ptr));
if(strcmp(key_ptr, key) != 0)
continue; /* ºÍkeyÖµ²»Æ¥Åä */
strcpy(value, val_ptr);
break;
}
ret = CFG_OK;
r_cfg_end:
//if(fp != NULL) fclose(fp);
kfree(buf1);
kfree(buf2);
return ret;
}
/*************************************************************
Function: »ñµÃËùÓÐsection
Input: char *filename¡¡Îļþ,int max ×î´ó¿É·µ»ØµÄsectionµÄ¸öÊý
Output: char *sections[]¡¡´æ·ÅsectionÃû×Ö
Return: ·µ»Øsection¸öÊý¡£Èô³ö´í£¬·µ»Ø¸ºÊý¡£
-10 Îļþ´ò¿ª³ö´í
-12 Îļþ¶ÁÈ¡´íÎó
-14 Îļþ¸ñʽ´íÎó
Note:
*************************************************************/
int ini_get_sections(char *filedata, unsigned char * sections[], int max)
{
//FILE *fp;
char buf1[MAX_CFG_BUF + 1];
int n, n_sections = 0, ret;
int dataoff = 0;
// if((fp = fopen(filename, "rb")) == NULL)
// return CFG_ERR_OPEN_FILE;
while(1) {/*ËÑÕÒÏîsection */
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf1, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto cfg_scts_end;
if(n < 0)
break;/* Îļþβ */
n = strlen(ini_str_trim_l(ini_str_trim_r(buf1)));
if(n == 0 || buf1[0] == CFG_NTS)
continue; /* ¿ÕÐÐ »ò ×¢ÊÍÐÐ */
ret = CFG_ERR_FILE_FORMAT;
if(n > 2 && ((buf1[0] == CFG_SSL && buf1[n-1] != CFG_SSR)))
goto cfg_scts_end;
if(buf1[0] == CFG_SSL) {
if (max!=0){
buf1[n-1] = 0x00;
strcpy((char *)sections[n_sections], buf1+1);
if (n_sections>=max)
break; /* ³¬¹ý¿É·µ»Ø×î´ó¸öÊý */
}
n_sections++;
}
}
ret = n_sections;
cfg_scts_end:
// if(fp != NULL)
// fclose(fp);
return ret;
}
/*************************************************************
Function: È¥³ý×Ö·û´®ÓұߵĿÕ×Ö·û
Input: char * buf ×Ö·û´®Ö¸Õë
Output:
Return: ×Ö·û´®Ö¸Õë
Note:
*************************************************************/
static char * ini_str_trim_r(char * buf)
{
int len,i;
char tmp[128];
memset(tmp, 0, sizeof(tmp));
len = strlen(buf);
// tmp = (char *)malloc(len);
memset(tmp,0x00,len);
for(i = 0;i < len;i++) {
if (buf[i] !=' ')
break;
}
if (i < len) {
strncpy(tmp,(buf+i),(len-i));
}
strncpy(buf,tmp,len);
// free(tmp);
return buf;
}
/*************************************************************
Function: È¥³ý×Ö·û´®×ó±ßµÄ¿Õ×Ö·û
Input: char * buf ×Ö·û´®Ö¸Õë
Output:
Return: ×Ö·û´®Ö¸Õë
Note:
*************************************************************/
static char * ini_str_trim_l(char * buf)
{
int len,i;
char tmp[128];
memset(tmp, 0, sizeof(tmp));
len = strlen(buf);
//tmp = (char *)malloc(len);
memset(tmp,0x00,len);
for(i = 0;i < len;i++) {
if (buf[len-i-1] !=' ')
break;
}
if (i < len) {
strncpy(tmp,buf,len-i);
}
strncpy(buf,tmp,len);
//free(tmp);
return buf;
}
/*************************************************************
Function: ´ÓÎļþÖжÁÈ¡Ò»ÐÐ
Input: FILE *fp Îļþ¾ä±ú£»int maxlen »º³åÇø×î´ó³¤¶È
Output: char *buffer Ò»ÐÐ×Ö·û´®
Return: >0 ʵ¼Ê¶ÁµÄ³¤¶È
-1 Îļþ½áÊø
-2 ¶ÁÎļþ³ö´í
Note:
*************************************************************/
static int ini_file_get_line(char *filedata, char *buffer, int maxlen)
{
int i, j;
char ch1;
for(i=0, j=0; i<maxlen; j++) {
ch1 = filedata[j];
if(ch1 == '\n' || ch1 == 0x00)
break; /* »»ÐÐ */
if(ch1 == '\f' || ch1 == 0x1A) { /* '\f':»»Ò³·ûÒ²ËãÓÐЧ×Ö·û */
buffer[i++] = ch1;
break;
}
if(ch1 != '\r') buffer[i++] = ch1; /* ºöÂԻسµ·û */
}
buffer[i] = '\0';
return i+2;
}
/*************************************************************
Function: ·ÖÀëkeyºÍvalue
key=val
jack = liaoyuewang
| | |
k1 k2 i
Input: char *buf
Output: char **key, char **val
Return: 1 --- ok
0 --- blank line
-1 --- no key, "= val"
-2 --- only key, no '='
Note:
*************************************************************/
static int ini_split_key_value(char *buf, char **key, char **val)
{
int i, k1, k2, n;
if((n = strlen((char *)buf)) < 1)
return 0;
for(i = 0; i < n; i++)
if(buf[i] != ' ' && buf[i] != '\t')
break;
if(i >= n)
return 0;
if(buf[i] == '=')
return -1;
k1 = i;
for(i++; i < n; i++)
if(buf[i] == '=')
break;
if(i >= n)
return -2;
k2 = i;
for(i++; i < n; i++)
if(buf[i] != ' ' && buf[i] != '\t')
break;
buf[k2] = '\0';
*key = buf + k1;
*val = buf + i;
return 1;
}
int my_atoi(const char *str)
{
int result = 0;
int signal = 1; /* ĬÈÏΪÕýÊý */
if((*str>='0'&&*str<='9')||*str=='-'||*str=='+') {
if(*str=='-'||*str=='+') {
if(*str=='-')
signal = -1; /*ÊäÈ븺Êý*/
str++;
}
}
else
return 0;
/*¿ªÊ¼×ª»»*/
while(*str>='0' && *str<='9')
result = result*10 + (*str++ - '0' );
return signal*result;
}
int isspace(int x)
{
if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')
return 1;
else
return 0;
}
int isdigit(int x)
{
if(x<='9' && x>='0')
return 1;
else
return 0;
}
static long atol(char *nptr)
{
int c; /* current char */
long total; /* current total */
int sign; /* if ''-'', then negative, otherwise positive */
/* skip whitespace */
while ( isspace((int)(unsigned char)*nptr) )
++nptr;
c = (int)(unsigned char)*nptr++;
sign = c; /* save sign indication */
if (c == '-' || c == '+')
c = (int)(unsigned char)*nptr++; /* skip sign */
total = 0;
while (isdigit(c)) {
total = 10 * total + (c - '0'); /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}
if (sign == '-')
return -total;
else
return total; /* return result, negated if necessary */
}
/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected. Because of this, we can just use
* atol().
*
*Entry:
* nptr = ptr to string to convert
*
*Exit:
* return int value of the string
*
*Exceptions:
* None - overflow is not detected.
*
*******************************************************************************/
int atoi(char *nptr)
{
return (int)atol(nptr);
}
|