Repository navigation
Expand file tree
/
Copy pathparsefile.c
More file actions
190 lines (165 loc) · 4.92 KB
/
Copy pathparsefile.c
File metadata and controls
190 lines (165 loc) · 4.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
185
186
187
188
189
190
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "remote_config.h"
#include "config.h"
#define CC_MAX_LINE_LEN (400)
static void str_trim(char **s) {
int i;
int len = strlen(*s);
for (i = len - 1; i >= 0; i--) {
if ((*s)[i] <= ' ') {
(*s)[i] = 0;
} else {
break;
}
}
while (**s) {
if (**s <= ' ') {
(*s)++;
} else {
return;
}
}
}
static void trim_line_data(char *line_data_buf) {
char *tmp_ptr = NULL;
if (line_data_buf == NULL) {
return;
}
//trim comment string
tmp_ptr = line_data_buf;
while (*tmp_ptr && ((tmp_ptr - line_data_buf) <= CC_MAX_LINE_LEN)) {
if (*tmp_ptr == '#' || *tmp_ptr == ';') {
*tmp_ptr = '\0';
break;
}
tmp_ptr++;
}
//trim other character
tmp_ptr = line_data_buf;
str_trim(&tmp_ptr);
memmove(line_data_buf, tmp_ptr, strlen(tmp_ptr) + 1);
}
static int remote_config_set(char *name, char *value, remote_config_t *config) {
unsigned int i;
unsigned int *config_para = &config->factory_infcode;
for (i = 0; i < ARRAY_SIZE(config_item); i++) {
if (strcmp(config_item[i], name) == 0) {
config_para[i] = strtoul(value, NULL, 0);
printf("curpara:%s %08x\n", name, config_para[i]);
return 0;
}
}
return -1;
}
enum {
CUSTOM_LEVEL,
CONFIG_LEVEL,
KEYMAP_LEVEL
};
int get_config_from_file(FILE *fp, remote_config_t *remotes[]) {
char line_data_buf[CC_MAX_LINE_LEN];
char *name = NULL;
char *value;
remote_config_t *global_config_remote_data = NULL, *remote = NULL;
unsigned short ircode = 0, keycode = 0;
unsigned char parse_flag = CONFIG_LEVEL;
int remote_count = 0;
while (fgets(line_data_buf, CC_MAX_LINE_LEN, fp)) {
trim_line_data(line_data_buf);
if (strcasecmp((char *)line_data_buf, "custom_begin") == 0) {
parse_flag = CUSTOM_LEVEL;
break;
}
}
if (parse_flag == CONFIG_LEVEL)
{
malloc_new_remote(&remote);
remotes[remote_count] = remote;
}
else
malloc_new_remote(&global_config_remote_data);
fseek(fp, 0, SEEK_SET);
while (fgets(line_data_buf, CC_MAX_LINE_LEN, fp)) {
trim_line_data(line_data_buf);
name = line_data_buf;
switch (parse_flag) {
case CUSTOM_LEVEL:
if (strcasecmp(name, "custom_begin") == 0) {
malloc_new_remote(&remote);
size_t paramem = sizeof(remote_config_t) - (size_t)((size_t *)global_config_remote_data - (size_t *)&global_config_remote_data->factory_infcode);
memcpy(&remote->factory_infcode, &global_config_remote_data->factory_infcode, paramem);
remotes[remote_count] = remote;
parse_flag = CONFIG_LEVEL;
continue;
}
value = strchr(line_data_buf, '=');
if (value) {
*value++ = 0;
str_trim(&value);
}
else
continue;
str_trim(&name);
if (!*name) {
continue;
}
if (remote_config_set(name, value, global_config_remote_data)) {
printf("config file has not supported parameter:%s=%s\r\n", name, value);
}
continue;
case CONFIG_LEVEL:
if (strcasecmp(name, "key_begin") == 0) {
parse_flag = KEYMAP_LEVEL;
continue;
}
if (strcasecmp(name, "custom_end") == 0) {
remote_count++;
parse_flag = CUSTOM_LEVEL;
continue;
}
value = strchr(line_data_buf, '=');
if (value) {
*value++ = 0;
str_trim(&value);
}
else
continue;
str_trim(&name);
if (!*name) {
continue;
}
if (remote_config_set(name, value, remote)) {
printf("config file has not supported parameter:%s=%s\r\n", name, value);
}
continue;
case KEYMAP_LEVEL:
if (strcasecmp(name, "key_end") == 0) {
parse_flag = CONFIG_LEVEL;
continue;
}
value = strchr(line_data_buf, ' ');
if (value) {
*value++ = 0;
str_trim(&value);
}
str_trim(&name);
if (!*name) {
continue;
}
ircode = strtoul(name, NULL, 0);
if (ircode > 0xff) {
continue;
}
keycode = strtoul(value, NULL, 0) & 0xffff;
if (keycode) {
remote->key_map[ircode] = keycode;
printf("KEYMAP_LEVEL: ircode = 0x%x, keycode = %d\n", ircode, keycode);
remote->key_count++;
}
continue;
}
}
return 0;
}