Fix #132858, Sven Neumann, patch by James Henstridge:
[glib.git] / tests / markup-test.c
blobaf19a5ee3f9c85e184877b33036018be396dda46
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
4 #include <stdio.h>
5 #include <glib.h>
7 static int depth = 0;
9 static void
10 indent (int extra)
12 int i = 0;
13 while (i < depth)
15 fputs (" ", stdout);
16 ++i;
20 static void
21 start_element_handler (GMarkupParseContext *context,
22 const gchar *element_name,
23 const gchar **attribute_names,
24 const gchar **attribute_values,
25 gpointer user_data,
26 GError **error)
28 int i;
30 indent (0);
31 printf ("ELEMENT '%s'\n", element_name);
33 i = 0;
34 while (attribute_names[i] != NULL)
36 indent (1);
38 printf ("%s=\"%s\"\n",
39 attribute_names[i],
40 attribute_values[i]);
42 ++i;
45 ++depth;
48 static void
49 end_element_handler (GMarkupParseContext *context,
50 const gchar *element_name,
51 gpointer user_data,
52 GError **error)
54 --depth;
55 indent (0);
56 printf ("END '%s'\n", element_name);
59 static void
60 text_handler (GMarkupParseContext *context,
61 const gchar *text,
62 gsize text_len,
63 gpointer user_data,
64 GError **error)
66 indent (0);
67 printf ("TEXT '%.*s'\n", (int)text_len, text);
71 static void
72 passthrough_handler (GMarkupParseContext *context,
73 const gchar *passthrough_text,
74 gsize text_len,
75 gpointer user_data,
76 GError **error)
78 indent (0);
80 printf ("PASS '%.*s'\n", (int)text_len, passthrough_text);
83 static void
84 error_handler (GMarkupParseContext *context,
85 GError *error,
86 gpointer user_data)
88 fprintf (stderr, " %s\n", error->message);
91 static GMarkupParser parser = {
92 start_element_handler,
93 end_element_handler,
94 text_handler,
95 passthrough_handler,
96 error_handler
99 static int
100 test_in_chunks (const gchar *contents,
101 gint length,
102 gint chunk_size)
104 GMarkupParseContext *context;
105 int i = 0;
107 context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
109 while (i < length)
111 int this_chunk = MIN (length - i, chunk_size);
113 if (!g_markup_parse_context_parse (context,
114 contents + i,
115 this_chunk,
116 NULL))
118 g_markup_parse_context_free (context);
119 return 1;
122 i += this_chunk;
125 if (!g_markup_parse_context_end_parse (context, NULL))
127 g_markup_parse_context_free (context);
128 return 1;
131 g_markup_parse_context_free (context);
133 return 0;
136 static int
137 test_file (const gchar *filename)
139 gchar *contents;
140 gsize length;
141 GError *error;
142 GMarkupParseContext *context;
144 error = NULL;
145 if (!g_file_get_contents (filename,
146 &contents,
147 &length,
148 &error))
150 fprintf (stderr, "%s\n", error->message);
151 g_error_free (error);
152 return 1;
155 context = g_markup_parse_context_new (&parser, 0, NULL, NULL);
157 if (!g_markup_parse_context_parse (context, contents, length, NULL))
159 g_markup_parse_context_free (context);
160 return 1;
163 if (!g_markup_parse_context_end_parse (context, NULL))
165 g_markup_parse_context_free (context);
166 return 1;
169 g_markup_parse_context_free (context);
171 /* A byte at a time */
172 if (test_in_chunks (contents, length, 1) != 0)
173 return 1;
175 /* 2 bytes */
176 if (test_in_chunks (contents, length, 2) != 0)
177 return 1;
179 /*5 bytes */
180 if (test_in_chunks (contents, length, 5) != 0)
181 return 1;
183 /* 12 bytes */
184 if (test_in_chunks (contents, length, 12) != 0)
185 return 1;
187 /* 1024 bytes */
188 if (test_in_chunks (contents, length, 1024) != 0)
189 return 1;
191 return 0;
195 main (int argc,
196 char *argv[])
198 if (argc > 1)
199 return test_file (argv[1]);
200 else
202 fprintf (stderr, "Give a markup file on the command line\n");
203 return 1;