image: check image width and height
[awesome.git] / common / markup.c
blobe34c585a72be59a2967faec2269e6288b610647d
1 /*
2 * markup.c - markup functions
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* asprintf */
23 #define _GNU_SOURCE
25 #include <glib.h>
27 #include <stdio.h>
29 #include "common/markup.h"
31 /** Callback to invoke when the opening tag of an element is seen.
32 * Here is just copies element elements we do not care about, and copies
33 * values we care.
34 * \param context the context of GMarkup
35 * \param element_name element name
36 * \param attribute_names array of attribute names
37 * \param attribute_values array of attribute values
38 * \param user_data pointer to user data, here a markup_parser_data_t pointer
39 * \param error a GError
41 static void
42 markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused)),
43 const gchar *element_name,
44 const gchar **attribute_names,
45 const gchar **attribute_values,
46 gpointer user_data,
47 GError **error __attribute__ ((unused)))
49 markup_parser_data_t *p = (markup_parser_data_t *) user_data;
50 int i;
52 for(i = 0; p->elements[i]; i++)
53 if(!a_strcmp(element_name, p->elements[i])) {
54 (*p->on_element)(p, element_name, attribute_names,
55 attribute_values);
56 return;
59 if(a_strcmp(element_name, "markup"))
61 buffer_addf(&p->text, "<%s", element_name);
62 for(i = 0; attribute_names[i]; i++)
64 buffer_addf(&p->text, " %s=\"%s\"", attribute_names[i],
65 attribute_values[i]);
67 buffer_addc(&p->text, '>');
71 /** Callback to invoke when the closing tag of an element is seen. Note that
72 * this is also called for empty tags like \<empty/\>.
73 * Here is just copies element elements we do not care about.
74 * \param context the context of GMarkup
75 * \param element_name element name
76 * \param user_data pointer to user data, here a markup_parser_data_t pointer
77 * \param error a GError
79 static void
80 markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)),
81 const gchar *element_name,
82 gpointer user_data,
83 GError **error __attribute__ ((unused)))
85 markup_parser_data_t *p = (markup_parser_data_t *) user_data;
86 int i;
88 for(i = 0; p->elements[i]; i++)
89 if(!a_strcmp(element_name, p->elements[i]))
90 return;
92 if(a_strcmp(element_name, "markup"))
93 buffer_addf(&p->text, "</%s>", element_name);
96 /** Callback to invoke when some text is seen (text is always inside an
97 * element). Note that the text of an element may be spread over multiple calls
98 * of this function. If the G_MARKUP_TREAT_CDATA_AS_TEXT flag is set, this
99 * function is also called for the content of CDATA marked sections.
100 * Here it recopies blindly the text in the text attribute of user_data.
101 * \param context the context of GMarkup
102 * \param text the text
103 * \param text_len the text length
104 * \param user_data pointer to user data, here a markup_parser_data_t pointer
105 * \param error a GError
107 static void
108 markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
109 const gchar *text,
110 gsize text_len,
111 gpointer user_data,
112 GError **error __attribute__ ((unused)))
114 markup_parser_data_t *p = (markup_parser_data_t *) user_data;
116 if (text_len) {
117 buffer_grow(&p->text, text_len);
118 buffer_add_xmlescaped(&p->text, text);
122 /** Create a markup_parser_data_t structure with elements list.
123 * \param a pointer to an allocated markup_parser_data_t which must be wiped
124 * with markup_parser_data_wipe()
126 void markup_parser_data_init(markup_parser_data_t *p)
128 buffer_init(&p->text);
131 /** Wipe a markup_parser_data_t initialized with markup_parser_data_init.
132 * \param p markup_parser_data_t address
134 void
135 markup_parser_data_wipe(markup_parser_data_t *p)
137 buffer_wipe(&p->text);
140 /** Parse markup defined in data on the string str.
141 * \param data A markup_parser_data_t allocated by markup_parser_data_new().
142 * \param str A string to parse markup from.
143 * \param slen String length.
144 * \return True if success, false otherwise.
146 bool
147 markup_parse(markup_parser_data_t *data, const char *str, ssize_t slen)
149 static GMarkupParser const parser =
151 /* start_element */
152 markup_parse_start_element,
153 /* end_element */
154 markup_parse_end_element,
155 /* text */
156 markup_parse_text,
157 /* passthrough */
158 NULL,
159 /* error */
160 NULL
162 GMarkupParseContext *mkp_ctx;
163 GError *error = NULL;
165 if(slen <= 0)
166 return false;
168 mkp_ctx = g_markup_parse_context_new(&parser, 0, data, NULL);
170 if(!g_markup_parse_context_parse(mkp_ctx, "<markup>", -1, &error)
171 || !g_markup_parse_context_parse(mkp_ctx, str, slen, &error)
172 || !g_markup_parse_context_parse(mkp_ctx, "</markup>", -1, &error)
173 || !g_markup_parse_context_end_parse(mkp_ctx, &error))
175 warn("unable to parse text \"%s\": %s", str, error ? error->message : "unknown error");
176 if(error)
177 g_error_free(error);
178 g_markup_parse_context_free(mkp_ctx);
179 return false;
182 g_markup_parse_context_free(mkp_ctx);
184 return true;
187 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80