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