-
Notifications
You must be signed in to change notification settings - Fork 7
/
lua_pb_package.c
247 lines (241 loc) · 7.36 KB
/
lua_pb_package.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
/*
* \file: lua_pb_package.c
* \brief: Created by hushouguo at Nov 07 2014 01:27:43
*/
#include "zebra.h"
struct
{
PB_PACKAGE_VALUE_TYPE_ENUM value_type;
const char* type_string;
} lua_pb_package_value_type_array[] =
{
{PB_PACKAGE_VALUE_TYPE_NONE, "none"},
{PB_PACKAGE_VALUE_TYPE_CHAR, "char"},
{PB_PACKAGE_VALUE_TYPE_SHORT, "short"},
{PB_PACKAGE_VALUE_TYPE_INT, "int"},
{PB_PACKAGE_VALUE_TYPE_LONG, "long"},
{PB_PACKAGE_VALUE_TYPE_BOOL, "bool"},
{PB_PACKAGE_VALUE_TYPE_DOUBLE, "double"},
{PB_PACKAGE_VALUE_TYPE_STRING, "string"},
};
PB_PACKAGE_VALUE_TYPE_ENUM lua_pb_package_parse_valuetype(const char* type_string)
{
int n;
for (n = 0; n < sizeof(lua_pb_package_value_type_array)/sizeof(lua_pb_package_value_type_array[0]); ++n)
{
if (strcasecmp(lua_pb_package_value_type_array[n].type_string, type_string) == 0)
{
return lua_pb_package_value_type_array[n].value_type;
}
}
return PB_PACKAGE_VALUE_TYPE_NONE;
}
bool lua_pb_package_parse_node(xmlDocPtr doc, xmlNodePtr node, lua_pb_package_node_t* packagenode);
bool lua_pb_package_parse_macro(xmlDocPtr doc, lua_pb_package_node_t* packagenode, const char* macroname)
{
xmlNodePtr macronode;
xmlNodePtr root = xml_parser_root_node(doc);
for (macronode = xml_parser_child_node(doc, root, "macro"); macronode; macronode = xml_parser_next_node(doc, macronode, "macro"))
{
const char* desc = xml_parser_node_value_string(doc, macronode, "desc");
if (desc && strcasecmp(macroname, desc) == 0)
{
return lua_pb_package_parse_node(doc, macronode, packagenode);
}
}
return false;
}
bool lua_pb_package_parse_node(xmlDocPtr doc, xmlNodePtr node, lua_pb_package_node_t* packagenode)
{
bool retcode = true;
xmlNodePtr currnode;
for (currnode = xml_parser_child_node(doc, node, NULL); currnode && retcode; currnode = xml_parser_next_node(doc, currnode, NULL))
{
if (strcasecmp((char*)currnode->name, "v") == 0)
{
PB_PACKAGE_VALUE_TYPE_ENUM valuetype;
lua_pb_package_node_t* newnode;
const char* desc = xml_parser_node_value_string(doc, currnode, "desc");
const char* type = xml_parser_node_value_string(doc, currnode, "type");
if (!desc || !type)
{
error_log("invalid v node, desc:%s, type:%s\n", desc ? desc : "NULL", type ? type : "NULL");
return false;
}
valuetype = lua_pb_package_parse_valuetype(type);
if (valuetype == PB_PACKAGE_VALUE_TYPE_NONE)
{
return false;
}
newnode = (lua_pb_package_node_t*)malloc(sizeof(lua_pb_package_node_t));
newnode->node_type = PB_PACKAGE_NODE_TYPE_V;
newnode->value_type = valuetype;
strncpy(newnode->desc, desc, sizeof(newnode->desc));
newnode->size = 0;
packagenode->child[packagenode->size++] = newnode;
}
else if (strcasecmp((char*)currnode->name, "o") == 0)
{
lua_pb_package_node_t* newnode;
const char* desc = xml_parser_node_value_string(doc, currnode, "desc");
if (!desc)
{
error_log("invalid o node, no found desc\n");
return false;
}
newnode = (lua_pb_package_node_t*)malloc(sizeof(lua_pb_package_node_t));
newnode->node_type = PB_PACKAGE_NODE_TYPE_O;
newnode->value_type = PB_PACKAGE_VALUE_TYPE_NONE;
strncpy(newnode->desc, desc, sizeof(newnode->desc));
newnode->size = 0;
packagenode->child[packagenode->size++] = newnode;
retcode = lua_pb_package_parse_node(doc, currnode, newnode);
}
else if (strcasecmp((char*)currnode->name, "ary") == 0)
{
lua_pb_package_node_t* newnode;
const char* desc = xml_parser_node_value_string(doc, currnode, "desc");
if (!desc)
{
error_log("invalid ary node, no found desc\n");
return false;
}
newnode = (lua_pb_package_node_t*)malloc(sizeof(lua_pb_package_node_t));
newnode->node_type = PB_PACKAGE_NODE_TYPE_ARY;
newnode->value_type = PB_PACKAGE_VALUE_TYPE_NONE;
strncpy(newnode->desc, desc, sizeof(newnode->desc));
newnode->size = 0;
packagenode->child[packagenode->size++] = newnode;
retcode = lua_pb_package_parse_node(doc, currnode, newnode);
}
else
{
if (!lua_pb_package_parse_macro(doc, packagenode, (char*)currnode->name))
{
error_log("Unknown node name:%s\n", currnode->name);
return false;
}
}
}
return retcode;
}
bool lua_pb_package_parse_opnode(lua_pb_t* pb, xmlDocPtr doc, xmlNodePtr node)
{
bool retcode = true;
xmlNodePtr opnode;
for (opnode = xml_parser_child_node(doc, node, "cmd"); opnode && retcode; opnode = xml_parser_next_node(doc, opnode, "cmd"))
{
lua_pb_package_t* package = NULL;
int id = xml_parser_node_value_int(doc, opnode, "id");
const char* desc = xml_parser_node_value_string(doc, opnode, "desc");
if (id == 0 || !desc || strlen(desc) == 0)
{
error_log("invalid cmd.id:%u or cmd.desc:\"%s\"\n", id, desc ? desc : "NULL");
return false;
}
package = (lua_pb_package_t*)malloc(sizeof(lua_pb_package_t));
package->root = (lua_pb_package_node_t*)malloc(sizeof(lua_pb_package_node_t));
package->root->cmd = id;
strncpy(package->root->desc, desc, sizeof(package->root->desc));
package->root->size = 0;
retcode = lua_pb_package_parse_node(doc, opnode, package->root);
if (retcode)
{
if (!hash_table_insert(pb->pb_package_table, package->root->cmd, package))
{
error_log("duplicate cmd:%d,%s\n", package->root->cmd, package->root->desc);
return false;
}
}
}
return retcode;
}
bool lua_pb_package_parse_file(lua_pb_t* pb, const char* file)
{
bool retcode;
xmlNodePtr root;
xmlDocPtr doc = xml_parser_open(file);
if (!doc)
{
return false;
}
root = xml_parser_root_node(doc);
if (!root)
{
error_log("not found root node:%s\n", file);
return false;
}
retcode = lua_pb_package_parse_opnode(pb, doc, root);
xml_parser_close(doc);
return retcode;
}
void lua_pb_package_node_dump(lua_pb_package_node_t* node, const char* prefix)
{
size_t n;
debug_log("%sdesc:%s\n", prefix, node->desc);
for (n = 0; n < node->size; ++n)
{
char buffer[96];
snprintf(buffer, sizeof(buffer), "%s\t", prefix);
lua_pb_package_node_dump(node->child[n], buffer);
}
}
void lua_pb_package_dump(lua_pb_t* pb)
{
size_t n = 0;
debug_log("lua_pb_package_dump\n");
for (n = 0; n < pb->pb_package_table->nodesize; ++n)
{
hash_node_t* node = pb->pb_package_table->nodes[n];
while (node)
{
lua_pb_package_t* package = (lua_pb_package_t*)node->value;
size_t i;
debug_log("cmd:%d\n", package->root->cmd);
for (i = 0; i < package->root->size; ++i)
{
lua_pb_package_node_dump(package->root->child[i], "\t");
}
node = node->next;
}
}
}
bool lua_pb_package_init(lua_pb_t* pb, const char* file)
{
bool retcode = lua_pb_package_parse_file(pb, file);
/* lua_pb_package_dump(pb); */
return retcode;
}
void lua_pb_package_destroy_node(lua_pb_package_node_t* node)
{
size_t n;
for (n = 0; n < node->size; ++n)
{
lua_pb_package_destroy_node(node->child[n]);
}
SAFE_FREE(node);
}
void lua_pb_package_destroy(lua_pb_t* pb)
{
size_t n = 0;
for (n = 0; n < pb->pb_package_table->nodesize; ++n)
{
hash_node_t* node = pb->pb_package_table->nodes[n];
while (node)
{
lua_pb_package_t* package = (lua_pb_package_t*)node->value;
size_t i;
for (i = 0; i < package->root->size; ++i)
{
lua_pb_package_destroy_node(package->root->child[i]);
}
SAFE_FREE(package);
node = node->next;
}
}
hash_table_clear(pb->pb_package_table);
}
lua_pb_package_t* lua_pb_package_find(lua_pb_t* pb, int cmd)
{
return (lua_pb_package_t*)hash_table_find(pb->pb_package_table, cmd);
}