Added SILC XML API
[runtime.git] / lib / silcutil / tests / test_silcxml.c
1 /* SILC XML tests */
2
3 #include "silcruntime.h"
4
5 SilcXMLParser parser;
6 SilcBool success = FALSE;
7
8 static void xml_start_element(SilcXMLParser parser,
9                               const char *name,
10                               SilcHashTable attributes,
11                               void *context)
12 {
13   fprintf(stderr, "<%s", name);
14
15   if (attributes) {
16     SilcHashTableList htl;
17     char *att, *val;
18
19     silc_hash_table_list(attributes, &htl);
20     while (silc_hash_table_get(&htl, (void *)&att, (void *)&val))
21       fprintf(stderr, " %s='%s'", att, val);
22
23     silc_hash_table_list_reset(&htl);
24   }
25   fprintf(stderr, ">");
26 }
27
28 static void xml_end_element(SilcXMLParser parser,
29                             const char *name,
30                             void *context)
31 {
32   fprintf(stderr, "</%s>", name);
33 }
34
35 static void xml_data(SilcXMLParser parser,
36                      const unsigned char *data,
37                      SilcUInt32 data_len,
38                      void *context)
39 {
40   silc_file_write(2, data, data_len);
41 }
42
43 static void xml_pi(SilcXMLParser parser,
44                    const char *target,
45                    const char *data,
46                    void *context)
47 {
48   fprintf(stderr, "%s %s", target, data);
49 }
50
51 static SilcXMLParserHandlerStruct handler =
52 {
53   xml_start_element,
54   xml_end_element,
55   xml_data,
56   xml_pi
57 };
58
59 int main(int argc, char **argv)
60 {
61   SilcXMLParamsStruct params;
62   SilcUInt32 cur_line;
63   char *file;
64
65   silc_runtime_init();
66
67   if (argc != 2) {
68     fprintf(stderr, "Usage: test_silcxml <filename>\n");
69     goto err;
70   }
71
72   memset(&params, 0, sizeof(params));
73   parser = silc_xml_parser_create(&params, &handler, NULL);
74   if (!parser)
75     goto err;
76
77   if (!silc_xml_parse_file(parser, argv[1])) {
78     silc_errno_location(&file, &cur_line, NULL);
79     fprintf(stderr, "%s:%d: %s\n", file, cur_line, silc_errno_reason());
80     goto err;
81   }
82
83   silc_xml_parser_free(parser);
84
85   success = TRUE;
86
87  err:
88   SILC_LOG_DEBUG(("Testing was %s", success ? "SUCCESS" : "FAILURE"));
89   fprintf(stderr, "Testing was %s\n", success ? "SUCCESS" : "FAILURE");
90
91   silc_runtime_uninit();
92
93   return !success;
94 }