Update Serbian translation
[yelp.git] / tests / test-transform.c
blob481e27caf1451458e1c67c89b4e3b9e5df97a43e
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2006 Shaun McCance
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * Author: Shaun McCance <shaunm@gnome.org>
21 #include <config.h>
23 #include <glib.h>
24 #include <libxml/parser.h>
25 #include <libxml/xinclude.h>
27 #include "yelp-error.h"
28 #include "yelp-transform.h"
30 static gint num_chunks = 0;
31 static gboolean freed;
33 static gint timeout = -1;
34 static gboolean random_timeout = FALSE;
35 static gchar **files = NULL;
36 static const GOptionEntry options[] = {
37 { "random-timeout", 'r',
38 0, G_OPTION_ARG_NONE,
39 &random_timeout,
40 "Time out after a random amount of time", NULL },
41 { "timeout", 't',
42 0, G_OPTION_ARG_INT,
43 &timeout,
44 "Time out after N milliseconds", "N" },
45 { G_OPTION_REMAINING,
46 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
47 &files, NULL, NULL },
48 { NULL }
51 GMainLoop *loop;
53 static gboolean
54 transform_release (YelpTransform *transform)
56 printf ("\nRELEASE\n");
57 if (!freed) {
58 yelp_transform_cancel (transform);
59 g_object_unref (transform);
61 freed = TRUE;
62 return FALSE;
65 static void
66 transform_chunk (YelpTransform *transform,
67 const gchar *chunk_id,
68 gpointer user_data)
70 gchar *chunk, *small;
71 num_chunks++;
72 printf ("\nCHUNK %i: %s\n", num_chunks, chunk_id);
74 chunk = yelp_transform_take_chunk (transform, chunk_id);
75 small = g_strndup (chunk, 300);
76 printf ("%s\n", small);
78 g_free (small);
79 g_free (chunk);
82 static void
83 transform_finished (YelpTransform *transform,
84 gpointer user_data)
86 printf ("\nFINAL\n");
87 if (!freed) {
88 yelp_transform_cancel (transform);
89 g_object_unref (transform);
91 freed = TRUE;
94 static void
95 transform_error (YelpTransform *transform,
96 gpointer user_data)
98 printf ("\nERROR\n");
101 static void
102 transform_destroyed (gpointer data,
103 GObject *object)
105 printf ("\nFREED\n");
106 g_main_loop_quit (loop);
109 gint
110 main (gint argc, gchar **argv)
112 GOptionContext *context;
113 xmlParserCtxtPtr parser;
114 xmlDocPtr doc;
115 YelpTransform *transform;
116 gchar **params;
117 const gchar *stylesheet;
118 gchar *file;
120 context = g_option_context_new ("[STYLESHEET] FILE");
121 g_option_context_add_main_entries (context, options, NULL);
122 g_option_context_parse (context, &argc, &argv, NULL);
124 if (files == NULL || files[0] == NULL) {
125 g_printerr ("Usage: test-transform [OPTION...] [STYLESHEET] FILE\n");
126 return 1;
129 if (files[1] == NULL) {
130 stylesheet = DATADIR"/yelp/xslt/db2html.xsl";
131 file = files[0];
132 } else {
133 stylesheet = files[0];
134 file = files[1];
137 params = g_new0 (gchar *, 7);
138 params[0] = g_strdup ("db.chunk.extension");
139 params[1] = g_strdup ("\"\"");
140 params[2] = g_strdup ("db.chunk.info_basename");
141 params[3] = g_strdup ("\"x-yelp-titlepage\"");
142 params[4] = g_strdup ("db.chunk.max_depth");
143 params[5] = g_strdup ("2");
144 params[6] = NULL;
146 transform = yelp_transform_new (stylesheet);
147 g_object_weak_ref ((GObject *) transform, transform_destroyed, NULL);
149 g_signal_connect (transform, "chunk-ready", (GCallback) transform_chunk, NULL);
150 g_signal_connect (transform, "finished", (GCallback) transform_finished, NULL);
151 g_signal_connect (transform, "error", (GCallback) transform_error, NULL);
153 parser = xmlNewParserCtxt ();
154 doc = xmlCtxtReadFile (parser,
155 file,
156 NULL,
157 XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA |
158 XML_PARSE_NOENT | XML_PARSE_NONET );
159 xmlFreeParserCtxt (parser);
160 xmlXIncludeProcessFlags (doc,
161 XML_PARSE_DTDLOAD | XML_PARSE_NOCDATA |
162 XML_PARSE_NOENT | XML_PARSE_NONET );
163 if (!yelp_transform_start (transform, doc, NULL, (const gchar **) params))
164 return 1;
166 if (random_timeout) {
167 GRand *rand = g_rand_new ();
168 timeout = g_rand_int_range (rand, 80, 280);
169 g_rand_free (rand);
172 if (timeout >= 0)
173 g_timeout_add (timeout, (GSourceFunc) transform_release, transform);
175 loop = g_main_loop_new (NULL, FALSE);
176 g_main_loop_run (loop);
178 return 0;