client: geometry() honors size hints hint
[awesome.git] / common / markup.c
blob6de94d7f2764c9b3fa08638983c404a6a1ba6ae6
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 #include <glib.h>
24 #include "common/markup.h"
26 /** Callback to invoke when the opening tag of an element is seen.
27 * Here is just copies element elements we do not care about, and copies
28 * values we care.
29 * \param context the context of GMarkup
30 * \param element_name element name
31 * \param attribute_names array of attribute names
32 * \param attribute_values array of attribute values
33 * \param user_data pointer to user data, here a markup_parser_data_t pointer
34 * \param error a GError
36 static void
37 markup_parse_start_element(GMarkupParseContext *context __attribute__ ((unused)),
38 const gchar *element_name,
39 const gchar **attribute_names,
40 const gchar **attribute_values,
41 gpointer user_data,
42 GError **error __attribute__ ((unused)))
44 markup_parser_data_t *p = (markup_parser_data_t *) user_data;
45 int i;
47 for(i = 0; p->elements[i]; i++)
48 if(!a_strcmp(element_name, p->elements[i])) {
49 (*p->on_element)(p, element_name, attribute_names,
50 attribute_values);
51 return;
54 if(a_strcmp(element_name, "markup"))
56 buffer_addf(&p->text, "<%s", element_name);
57 for(i = 0; attribute_names[i]; i++)
59 buffer_addf(&p->text, " %s=\"%s\"", attribute_names[i],
60 attribute_values[i]);
62 buffer_addc(&p->text, '>');
66 /** Callback to invoke when the closing tag of an element is seen. Note that
67 * this is also called for empty tags like \<empty/\>.
68 * Here is just copies element elements we do not care about.
69 * \param context the context of GMarkup
70 * \param element_name element name
71 * \param user_data pointer to user data, here a markup_parser_data_t pointer
72 * \param error a GError
74 static void
75 markup_parse_end_element(GMarkupParseContext *context __attribute__ ((unused)),
76 const gchar *element_name,
77 gpointer user_data,
78 GError **error __attribute__ ((unused)))
80 markup_parser_data_t *p = (markup_parser_data_t *) user_data;
81 int i;
83 for(i = 0; p->elements[i]; i++)
84 if(!a_strcmp(element_name, p->elements[i]))
85 return;
87 if(a_strcmp(element_name, "markup"))
88 buffer_addf(&p->text, "</%s>", element_name);
91 /** Callback to invoke when some text is seen (text is always inside an
92 * element). Note that the text of an element may be spread over multiple calls
93 * of this function. If the G_MARKUP_TREAT_CDATA_AS_TEXT flag is set, this
94 * function is also called for the content of CDATA marked sections.
95 * Here it recopies blindly the text in the text attribute of user_data.
96 * \param context the context of GMarkup
97 * \param text the text
98 * \param text_len the text length
99 * \param user_data pointer to user data, here a markup_parser_data_t pointer
100 * \param error a GError
102 static void
103 markup_parse_text(GMarkupParseContext *context __attribute__ ((unused)),
104 const gchar *text,
105 gsize text_len,
106 gpointer user_data,
107 GError **error __attribute__ ((unused)))
109 markup_parser_data_t *p = (markup_parser_data_t *) user_data;
111 if (text_len) {
112 buffer_grow(&p->text, text_len);
113 buffer_add_xmlescaped(&p->text, text);
117 /** Create a markup_parser_data_t structure with elements list.
118 * \param a pointer to an allocated markup_parser_data_t which must be wiped
119 * with markup_parser_data_wipe()
121 void markup_parser_data_init(markup_parser_data_t *p)
123 buffer_init(&p->text);
126 /** Wipe a markup_parser_data_t initialized with markup_parser_data_init.
127 * \param p markup_parser_data_t address
129 void
130 markup_parser_data_wipe(markup_parser_data_t *p)
132 buffer_wipe(&p->text);
135 /** Parse markup defined in data on the string str.
136 * \param data A markup_parser_data_t allocated by markup_parser_data_new().
137 * \param str A string to parse markup from.
138 * \param slen String length.
139 * \return True if success, false otherwise.
141 bool
142 markup_parse(markup_parser_data_t *data, const char *str, ssize_t slen)
144 static GMarkupParser const parser =
146 /* start_element */
147 markup_parse_start_element,
148 /* end_element */
149 markup_parse_end_element,
150 /* text */
151 markup_parse_text,
152 /* passthrough */
153 NULL,
154 /* error */
155 NULL
157 GMarkupParseContext *mkp_ctx;
158 GError *error = NULL;
160 if(slen <= 0)
161 return false;
163 mkp_ctx = g_markup_parse_context_new(&parser, 0, data, NULL);
165 if(!g_markup_parse_context_parse(mkp_ctx, "<markup>", -1, &error)
166 || !g_markup_parse_context_parse(mkp_ctx, str, slen, &error)
167 || !g_markup_parse_context_parse(mkp_ctx, "</markup>", -1, &error)
168 || !g_markup_parse_context_end_parse(mkp_ctx, &error))
170 warn("unable to parse text \"%s\": %s", str, error ? error->message : "unknown error");
171 if(error)
172 g_error_free(error);
173 g_markup_parse_context_free(mkp_ctx);
174 return false;
177 g_markup_parse_context_free(mkp_ctx);
179 return true;
182 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80