Use the _LIBADD dependencies on libglib only on Win32.
[platform/upstream/glib.git] / tests / markup-test.c
1 #include <stdio.h>
2 #include <glib.h>
3
4 static int depth = 0;
5
6 static void
7 indent (int extra)
8 {
9   int i = 0;
10   while (i < depth)
11     {
12       fputs ("  ", stdout);
13       ++i;
14     }
15 }
16
17 static void
18 start_element_handler  (GMarkupParseContext *context,
19                         const gchar         *element_name,
20                         const gchar        **attribute_names,
21                         const gchar        **attribute_values,
22                         gpointer             user_data,
23                         GError             **error)
24 {
25   int i;
26   
27   indent (0);
28   printf ("ELEMENT '%s'\n", element_name);
29
30   i = 0;
31   while (attribute_names[i] != NULL)
32     {
33       indent (1);
34
35       printf ("%s=\"%s\"\n",
36               attribute_names[i],
37               attribute_values[i]);
38       
39       ++i;
40     }
41   
42   ++depth;
43 }
44
45 static void
46 end_element_handler    (GMarkupParseContext *context,
47                         const gchar         *element_name,
48                         gpointer             user_data,
49                         GError             **error)
50 {
51   --depth;
52   indent (0);
53   printf ("END '%s'\n", element_name);
54 }
55
56 static void
57 text_handler           (GMarkupParseContext *context,
58                         const gchar         *text,
59                         gint                 text_len,
60                         gpointer             user_data,
61                         GError             **error)
62 {
63   indent (0);
64   printf ("TEXT '%s'\n", text);
65 }
66
67
68 static void
69 passthrough_handler    (GMarkupParseContext *context,
70                         const gchar         *passthrough_text,
71                         gint                 text_len,
72                         gpointer             user_data,
73                         GError             **error)
74 {
75   indent (0);
76
77   printf ("PASS '%s'\n", passthrough_text);
78 }
79
80 static void
81 error_handler          (GMarkupParseContext *context,
82                         GError              *error,
83                         gpointer             user_data)
84 {
85   fprintf (stderr, " %s\n", error->message);
86 }
87
88 static GMarkupParser parser = {
89   start_element_handler,
90   end_element_handler,
91   text_handler,
92   passthrough_handler,
93   error_handler
94 };
95
96 static int
97 test_in_chunks (const gchar *contents,
98                 gint         length,
99                 gint         chunk_size)
100 {
101   GMarkupParseContext *context;
102   int i = 0;
103   
104   context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
105
106   while (i < length)
107     {
108       int this_chunk = MIN (length - i, chunk_size);
109
110       if (!g_markup_parse_context_parse (context,
111                                          contents + i,
112                                          this_chunk,
113                                          NULL))
114         {
115           g_markup_parse_context_free (context);
116           return 1;
117         }
118
119       i += this_chunk;
120     }
121       
122   if (!g_markup_parse_context_end_parse (context, NULL))
123     {
124       g_markup_parse_context_free (context);
125       return 1;
126     }
127
128   g_markup_parse_context_free (context);
129
130   return 0;
131 }
132
133 static int
134 test_file (const gchar *filename)
135 {
136   gchar *contents;
137   gint   length;
138   GError *error;
139   GMarkupParseContext *context;
140   
141   error = NULL;
142   if (!g_file_get_contents (filename,
143                             &contents,
144                             &length,
145                             &error))
146     {
147       fprintf (stderr, "%s\n", error->message);
148       g_error_free (error);
149       return 1;
150     }
151
152   context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
153
154   if (!g_markup_parse_context_parse (context, contents, length, NULL))
155     {
156       g_markup_parse_context_free (context);
157       return 1;
158     }
159
160   if (!g_markup_parse_context_end_parse (context, NULL))
161     {
162       g_markup_parse_context_free (context);
163       return 1;
164     }
165
166   g_markup_parse_context_free (context);
167
168   /* A byte at a time */
169   if (test_in_chunks (contents, length, 1) != 0)
170     return 1;
171
172   /* 2 bytes */
173   if (test_in_chunks (contents, length, 2) != 0)
174     return 1;
175
176   /*5 bytes */
177   if (test_in_chunks (contents, length, 5) != 0)
178     return 1;
179   
180   /* 12 bytes */
181   if (test_in_chunks (contents, length, 12) != 0)
182     return 1;
183   
184   /* 1024 bytes */
185   if (test_in_chunks (contents, length, 1024) != 0)
186     return 1;
187
188   return 0;
189 }
190
191 int
192 main (int   argc,
193       char *argv[])
194 {
195   if (argc > 1)
196     return test_file (argv[1]);
197   else
198     {
199       fprintf (stderr, "Give a markup file on the command line\n");
200       return 1;
201     }
202 }