-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
148 lines (130 loc) · 3.94 KB
/
parse.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
#include "parse.h"
static void parseParameter(xmlDocPtr doc, xmlNode *cur_node)
{
char *element;
char *must = NULL;
if (parameter_count >= parameter_array_size)
{
exit(4);
}
while (cur_node != NULL)
{
printf("%d %d\n", parameter[parameter_count].count, variable_array_size);
if (parameter[parameter_count].count >= variable_array_size)
{
exit(3);
}
if ((!xmlStrcmp(cur_node->name, (const xmlChar *)"MUST")))
{
must = ((char *)(xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1)));
if (strcmp("true", must) == 0)
{
parameter[parameter_count].must = 1;
}
else if (strcmp("false", must) == 0)
{
parameter[parameter_count].must = 0;
}
}
else if ((!xmlStrcmp(cur_node->name, (const xmlChar *)"ELEMENT")))
{
element = ((char *)(xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1)));
if (strlen(element) < parameter_strings_long)
{
int p = parameter[parameter_count].count;
strncpy(parameter[parameter_count].parameter[p], element, strlen(element));
}
else
{
exit(2);
}
parameter[parameter_count].count++;
}
else if ((!xmlStrcmp(cur_node->name, (const xmlChar *)"text")))
{
}
cur_node = cur_node->next;
}
parameter_count++;
}
static void
parseArgvParameter(xmlDocPtr doc, xmlNode *cur_node)
{
char *element;
origin_argc = 0;
origin_argv = (char**)malloc(sizeof(char*) * 200);
for(int i = 0; i < 200; i++)
origin_argv[i] = (char*)malloc(sizeof(char) * 200);
while (cur_node != NULL)
{
if ((!xmlStrcmp(cur_node->name, (const xmlChar *)"ELEMENT")))
{
element = ((char *)(xmlNodeListGetString(doc, cur_node->xmlChildrenNode, 1)));
char* substr = NULL;
printf("%s\n", element);
substr = strtok(element, " ");
while(substr != NULL) {
sprintf(origin_argv[origin_argc], "%s", substr);
origin_argv[origin_argc][strlen(substr)] = '\0';
origin_argc ++;
substr = strtok(NULL, " ");
}
origin_argv[origin_argc++] = NULL;
}
cur_node = cur_node->next;
}
}
static void
parseRoot(xmlDocPtr doc, xmlNode *a_node)
{
a_node = a_node->children;
xmlNode *cur_node = NULL;
for (cur_node = a_node; cur_node; cur_node = cur_node->next)
{
if (!(xmlStrcmp(cur_node->name, (const xmlChar *)"PARAMETER")))
{
parseParameter(doc, cur_node->xmlChildrenNode);
}
else if (!(xmlStrcmp(cur_node->name, (const xmlChar *)"ARGV")))
{
parseArgvParameter(doc, cur_node->xmlChildrenNode);
}
else if (!(xmlStrcmp(cur_node->name, (const xmlChar *)"text")))
{
}
}
return;
}
void print_parse_result()
{
int i, j;
printf("parameter :\n");
for (i = 0; i < parameter_count; i++)
{
printf("must = %d,count = %d\n", parameter[i].must, parameter[i].count);
for (j = 0; j < parameter[i].count; j++)
{
printf("%s\n", parameter[i].parameter[j]);
}
}
printf("parameter count = %d\n", parameter_count);
printf("file:\n%s:\n", file_parameter);
}
void parse_xml(char *xml_posion)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
LIBXML_TEST_VERSION
/*parse the file and get the DOM */
doc = xmlReadFile(xml_posion, NULL, 0);
if (doc == NULL)
{
printf("error: could not parse file %s\n", xml_posion);
}
/*Get the root element node */
root_element = xmlDocGetRootElement(doc);
parseRoot(doc, root_element);
xmlFreeDoc(doc);
xmlCleanupParser();
print_parse_result();
}