blob: fdeb31c1e52cffaaf7089906ba090520c75a1379
1 | #include <stdio.h> |
2 | #include <stdlib.h> |
3 | #include <unistd.h> |
4 | #include <errno.h> |
5 | #include <string.h> |
6 | #include <sys/ioctl.h> |
7 | #include "common.h" |
8 | |
9 | /* |
10 | remove head and tail blank in string |
11 | */ |
12 | static void StrTrim(char **s) |
13 | { |
14 | int i; |
15 | int len = strlen(*s); |
16 | |
17 | for (i = len - 1; i >= 0; i--) { |
18 | if ((*s)[i] <= ' ') |
19 | (*s)[i] = 0; |
20 | else |
21 | break; |
22 | } |
23 | |
24 | while (**s) { |
25 | if (**s <= ' ') |
26 | (*s)++; |
27 | else |
28 | break; |
29 | } |
30 | } |
31 | |
32 | static void StrLineTrim(char *line, int length) |
33 | { |
34 | char *tmp_ptr = line; |
35 | |
36 | if (line == NULL) |
37 | return; |
38 | /*trim comment string*/ |
39 | while (*tmp_ptr && ((tmp_ptr - line) <= length)) { |
40 | if (*tmp_ptr == '#' || *tmp_ptr == ';') { |
41 | *tmp_ptr = '\0'; |
42 | break; |
43 | } |
44 | tmp_ptr++; |
45 | } |
46 | } |
47 | |
48 | static int StrEmpty(char *str) |
49 | { |
50 | int len = strlen(str); |
51 | return (len == 0); |
52 | } |
53 | |
54 | /* |
55 | Mapcode in file: |
56 | key_begin |
57 | 0x47 11 |
58 | 0x13 2 |
59 | ...... |
60 | key_end |
61 | */ |
62 | static int GetMapCode(char *line, int *code) |
63 | { |
64 | char *p2; |
65 | char *p1 = line; |
66 | int ircode; |
67 | |
68 | /*remove head and tail blank in string*/ |
69 | StrTrim(&line); |
70 | |
71 | /*get second data point in line*/ |
72 | p2 = strchr(line, ' '); |
73 | if (p2) { |
74 | *p2++ = 0; |
75 | StrTrim(&p2); |
76 | } |
77 | if (!p2 || !*p2) |
78 | return -1; |
79 | StrTrim(&p1); |
80 | if (!*p1) |
81 | return -1; |
82 | ircode = strtoul(p1, NULL, 0); |
83 | if (ircode > 0xff) { |
84 | fprintf(stderr, "invalid ircode: 0x%x\n", ircode); |
85 | return -1; |
86 | } |
87 | |
88 | *code = MAPCODE(ircode, strtoul(p2, NULL, 0)); |
89 | return 0; |
90 | } |
91 | |
92 | int GetKeyValue(char *line, char **key, char **value) |
93 | { |
94 | char *p1, *p2; |
95 | |
96 | p1 = line; |
97 | p2 = strchr(line, '='); |
98 | if (p2) { |
99 | *p2++ = 0; |
100 | StrTrim(&p2); |
101 | } |
102 | if (!p2 || !*p2) |
103 | return -1; |
104 | StrTrim(&p1); |
105 | if (!p1 || !*p1) |
106 | return -1; |
107 | *value = p2; |
108 | *key = p1; |
109 | return 0; |
110 | } |
111 | |
112 | static int ReadFile(FILE *fp, pfileHandle handler, void *data) |
113 | { |
114 | char *key; |
115 | char *value; |
116 | unsigned int mapcode; |
117 | unsigned char parse_flag = CONFIG_LEVEL; |
118 | char *line; |
119 | char *savep; |
120 | int ret; |
121 | |
122 | line = (char*)malloc(MAX_LINE_LEN + 1); |
123 | if (!line) { |
124 | fprintf(stderr, "malloc line failed\n"); |
125 | return -1; |
126 | } |
127 | savep = line; |
128 | while (fgets(savep, MAX_LINE_LEN, fp)) { |
129 | line = savep; |
130 | StrLineTrim(line, MAX_LINE_LEN); /*jump comment*/ |
131 | if (StrEmpty(line)) |
132 | continue; |
133 | StrTrim(&line); |
134 | switch (parse_flag) { |
135 | case CONFIG_LEVEL: |
136 | if ((strcasecmp(line, "key_begin")) == 0) { |
137 | parse_flag = KEYMAP_LEVEL; |
138 | continue; |
139 | } |
140 | if (!!GetKeyValue(line, &key, &value)) |
141 | continue; |
142 | if ((*handler)(key, value, data) < 0) { |
143 | fprintf(stderr, "invalid line:%s=%s\n", |
144 | line, value); |
145 | continue; |
146 | } |
147 | break; |
148 | case KEYMAP_LEVEL: |
149 | if ((strcasecmp(line, "key_end")) == 0) { |
150 | parse_flag = CONFIG_LEVEL; |
151 | continue; |
152 | } |
153 | if (!!GetMapCode(line, &mapcode)) |
154 | continue; |
155 | snprintf(line, 128, "%u", mapcode); |
156 | if ((*handler)("mapcode", line, data) < 0) { |
157 | fprintf(stderr, "invalid line:%s=%s\n", |
158 | "mapcode", line); |
159 | continue; |
160 | } |
161 | break; |
162 | } |
163 | |
164 | } |
165 | if (savep) |
166 | free(savep); |
167 | return 0; |
168 | } |
169 | |
170 | |
171 | int ParseFile(const char *file, pfileHandle handler, void *data) |
172 | { |
173 | FILE *cfgFp; |
174 | |
175 | if (NULL == (cfgFp = fopen(file, "r"))) { |
176 | fprintf(stderr, "fopen %s: %s\n", file, strerror(errno)); |
177 | return FAIL; |
178 | } |
179 | |
180 | ReadFile(cfgFp, handler, data); |
181 | fclose(cfgFp); |
182 | return SUCC; |
183 | } |
184 |