]> git.mxchange.org Git - flightgear.git/blob - utils/xmlgrep/testxml.c
Merge branch 'ehofman/version'
[flightgear.git] / utils / xmlgrep / testxml.c
1 #include <stdio.h>
2 #include <malloc.h>
3 #include <assert.h>
4 #include "xml.h"
5
6 #define ROOTNODE        "/Configuration/output/menu"
7 #define LEAFNODE        "name"
8 #define PATH            ROOTNODE"/"LEAFNODE
9 #define BUFLEN          4096
10
11 #define PRINT_ERROR_AND_EXIT(id) \
12     if (xmlErrorGetNo(id, 0) != XML_NO_ERROR) { \
13          const char *errstr = xmlErrorGetString(id, 0); \
14          size_t column = xmlErrorGetColumnNo(id, 0); \
15          size_t lineno = xmlErrorGetLineNo(id, 1); \
16          printf("Error at line %i, column %i: %s\n", lineno, column, errstr); \
17          exit(-1); \
18       }
19
20 int main()
21 {
22    void *root_id;
23
24    root_id = xmlOpen("sample.xml");
25    if (root_id)
26    {
27       void *path_id, *node_id;
28       char *s;
29
30       printf("\nTesting xmlNodeGetString for /*/*/test:\t\t\t\t\t");
31       s = xmlNodeGetString(root_id , "/*/*/test");
32       if (s)
33       {
34          printf("failed.\n\t'%s' should be empty\n", s);
35          free(s);
36       }
37       else
38          printf("succes.\n");
39
40       printf("Testing xmlGetString for /Configuration/output/test:\t\t\t");
41       path_id = xmlNodeGet(root_id, "/Configuration/output/test");
42       if (path_id)
43       {
44          s = xmlGetString(path_id);
45          if (s)
46          {
47             printf("failed.\n\t'%s' should be empty\n", s);
48             free(s);
49          }
50          else
51             printf("succes.\n");
52       }
53       else
54          PRINT_ERROR_AND_EXIT(root_id);
55
56       path_id = xmlNodeGet(root_id, PATH);
57       node_id = xmlNodeGet(root_id, ROOTNODE);
58
59       if (path_id && node_id)
60       {
61          char buf[BUFLEN];
62          size_t len;
63         
64          xmlCopyString(path_id, buf, BUFLEN);
65          printf("Testing xmlNodeCopyString against xmlGetString:\t\t\t\t");
66          if ((s = xmlGetString(path_id)) != 0)
67          {
68             if (strcmp(s, buf)) /* not the same */
69                printf("failed.\n\t'%s' differs from '%s'\n", s, buf);
70             else
71                printf("succes.\n");
72
73             printf("Testing xmlCopyString against xmlGetString:\t\t\t\t");
74             xmlCopyString(path_id, buf, BUFLEN);
75             if (strcmp(s, buf)) /* not the same */
76                printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf);
77             else
78                printf("succes.\n");
79          }
80          else
81             PRINT_ERROR_AND_EXIT(path_id);
82
83          printf("Testing xmlCopyString against xmlCompareString:\t\t\t\t");
84          if (xmlCompareString(path_id, buf)) /* not the same */
85             printf ("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf);
86          else
87             printf("succes.\n");
88
89          printf("Testing xmlCopyString against xmlNodeCompareString:\t\t\t");
90          if (xmlNodeCompareString(node_id, LEAFNODE, buf)) /* not the same */
91             printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf);
92          else
93             printf("succes.\n");
94
95          if (s) free(s);
96  
97          printf("Testing xmlCopyString against xmlNodeGetString:\t\t\t\t");
98          if ((s = xmlNodeGetString(node_id, LEAFNODE)) != 0)
99          {
100             if (strcmp(s, buf)) /* not the same */
101                printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf);
102             else
103                printf("succes.\n");
104             free(s);
105          }
106          else
107             PRINT_ERROR_AND_EXIT(node_id);
108
109          free(path_id);
110          path_id = xmlNodeGet(root_id, "/Configuration/backend/name");
111          if (path_id)
112          {
113             printf("Testing xmlAttributeCopyString against xmlAttributeCompareString:\t");
114             xmlAttributeCopyString(path_id, "type", buf, BUFLEN);
115             if (xmlAttributeCompareString(path_id, "type", buf)) /* no match */
116                printf("failed.\n\t'%s' differs\n", buf);
117             else
118                printf("succes.\n");
119
120             printf("Testing xmlAttributeCopyString against xmlAttributeGetString:\t\t");
121             if ((s = xmlAttributeGetString(path_id, "type")) != 0)
122             {
123                 if (strcmp(s, buf)) /* not the same */
124                    printf("failed.\n\t'%s' differs from '%s'\n", s, buf);
125                 else
126                    printf("succes.\n");
127                 free(s);
128             }
129             else
130                 PRINT_ERROR_AND_EXIT(path_id);
131
132          }
133          else
134             PRINT_ERROR_AND_EXIT(root_id);
135
136          free(node_id);
137          free(path_id);
138
139          path_id = xmlNodeGet(root_id, "Configuration/output/sample/test");
140          if (path_id)
141          {
142             xmlNodeCopyString(root_id ,"Configuration/output/menu/name", buf, BUFLEN);
143             printf("Testing xmlCompareString against a fixed string: \t\t\t");
144             if (xmlCompareString(path_id, buf))         /* no match */
145                printf("failed.\n\t'%s' differs\n", buf);
146             else
147                printf("succes.\n");
148
149             s = xmlGetString(path_id);
150             if (s)
151             {
152                printf("Testing xmlGetString  against a fixed string: \t\t\t\t");
153                if (strcmp(s, buf))                      /* mismatch */
154                   printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf);
155                else
156                   printf("succes.\n");
157
158                printf("Testing xmlCopyString gainst a fixed string: \t\t\t\t");
159                xmlCopyString(path_id, buf, BUFLEN);
160                if (strcmp(s, buf))                      /* mismatch */
161                   printf("failed.\n\t'%s' differs from\n\t'%s'\n", s, buf);
162                else
163                   printf("succes.\n"); 
164
165                free(s);
166             }
167             else
168                PRINT_ERROR_AND_EXIT(path_id);
169
170             free(path_id);
171          }
172       }
173
174       if (xmlErrorGetNo(root_id, 0) != XML_NO_ERROR)
175       {
176          const char *errstr = xmlErrorGetString(root_id, 0);
177          size_t column = xmlErrorGetColumnNo(root_id, 0);
178          size_t lineno = xmlErrorGetLineNo(root_id, 1);
179
180          printf("Error at line %i, column %i: %s\n", lineno, column, errstr);
181       }
182
183       xmlClose(root_id);
184    }
185    printf("\n");
186
187    return 0;
188 }