2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2001 Hiroyuki Yamamoto
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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "../codeconv.h"
30 /* if this is defined all attr.names and tag.names are stored
32 #if defined(SPARSE_MEMORY)
33 #include "stringtable.h"
35 static StringTable *xml_string_table;
37 static void xml_string_table_create(void)
39 if (xml_string_table == NULL)
40 xml_string_table = string_table_new();
42 #define XML_STRING_ADD(str) \
43 string_table_insert_string(xml_string_table, (str))
44 #define XML_STRING_FREE(str) \
45 string_table_free_string(xml_string_table, (str))
47 #define XML_STRING_TABLE_CREATE() \
48 xml_string_table_create()
50 #else /* !SPARSE_MEMORY */
52 #define XML_STRING_ADD(str) \
54 #define XML_STRING_FREE(str) \
57 #define XML_STRING_TABLE_CREATE()
59 #endif /* SPARSE_MEMORY */
61 static void xml_free_tag (XMLTag *tag);
62 static gint xml_get_parenthesis (XMLFile *file,
66 XMLFile *xml_open_file(const gchar *path)
70 g_return_val_if_fail(path != NULL, NULL);
72 XML_STRING_TABLE_CREATE();
74 newfile = g_new(XMLFile, 1);
76 newfile->fp = fopen(path, "rb");
82 newfile->buf = g_string_new(NULL);
83 newfile->bufp = newfile->buf->str;
86 newfile->tag_stack = NULL;
88 newfile->is_empty_element = FALSE;
93 void xml_close_file(XMLFile *file)
95 g_return_if_fail(file != NULL);
97 if (file->fp) fclose(file->fp);
99 g_string_free(file->buf, TRUE);
103 while (file->tag_stack != NULL)
109 static GNode *xml_build_tree(XMLFile *file, GNode *parent, guint level)
115 while (xml_parse_next_tag(file) == 0) {
116 if (file->level < level) break;
117 if (file->level == level) {
118 g_warning("xml_build_tree(): Parse error\n");
122 tag = xml_get_current_tag(file);
124 xmlnode = g_new(XMLNode, 1);
125 xmlnode->tag = xml_copy_tag(tag);
126 xmlnode->element = xml_get_element(file);
128 node = g_node_new(xmlnode);
130 node = g_node_append_data(parent, xmlnode);
132 xml_build_tree(file, node, file->level);
133 if (file->level == 0) break;
139 GNode *xml_parse_file(const gchar *path)
144 file = xml_open_file(path);
145 g_return_val_if_fail(file != NULL, NULL);
149 node = xml_build_tree(file, NULL, file->level);
151 xml_close_file(file);
153 #if defined(SPARSE_MEMORY)
154 if (debug_get_mode())
155 string_table_get_stats(xml_string_table);
161 gint xml_get_dtd(XMLFile *file)
163 gchar buf[XMLBUFSIZE];
166 if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) return -1;
168 if ((*bufp++ == '?') &&
169 (bufp = strcasestr(bufp, "xml")) &&
170 (bufp = strcasestr(bufp + 3, "version")) &&
171 (bufp = strchr(bufp + 7, '?')))
172 file->dtd = g_strdup(buf);
174 g_warning("Can't get xml dtd\n");
181 gint xml_parse_next_tag(XMLFile *file)
183 gchar buf[XMLBUFSIZE];
188 if (file->is_empty_element == TRUE) {
189 file->is_empty_element = FALSE;
194 if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
195 g_warning("xml_parse_next_tag(): Can't parse next tag\n");
201 if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
202 g_warning("xml_parse_next_tag(): Tag name mismatch: %s\n", buf);
209 tag = g_new0(XMLTag, 1);
210 xml_push_tag(file, tag);
213 if (len > 0 && buf[len - 1] == '/') {
214 file->is_empty_element = TRUE;
218 if (strlen(buf) == 0) {
219 g_warning("xml_parse_next_tag(): Tag name is empty\n");
223 while (*bufp != '\0' && !isspace(*bufp)) bufp++;
225 tag->tag = XML_STRING_ADD(buf);
229 tag->tag = XML_STRING_ADD(buf);
232 /* parse attributes ( name=value ) */
235 gchar *attr_name, *attr_value;
236 gchar *utf8attr_name, *utf8attr_value;
240 while (isspace(*bufp)) bufp++;
242 if ((p = strchr(attr_name, '=')) == NULL) {
243 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
248 while (isspace(*bufp)) bufp++;
250 if (*bufp != '"' && *bufp != '\'') {
251 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
257 if ((p = strchr(attr_value, quote)) == NULL) {
258 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
264 g_strchomp(attr_name);
265 xml_unescape_str(attr_value);
267 attr = g_new(XMLAttr, 1);
269 utf8attr_name = conv_codeset_strdup
271 conv_get_current_charset_str(),
273 utf8attr_value = conv_codeset_strdup
275 conv_get_current_charset_str(),
277 if (!utf8attr_name) {
278 g_warning("xml_parse_next_tag(): "
279 "faild to convert character set of attr_name\n");
280 utf8attr_name = g_strdup(attr_name);
282 if (!utf8attr_value) {
283 g_warning("xml_parse_next_tag(): "
284 "faild to convert character set of attr_value\n");
285 utf8attr_value = g_strdup(attr_value);
288 attr->name = XML_STRING_ADD(utf8attr_name);
289 attr->value = utf8attr_value;
290 tag->attr = g_list_append(tag->attr, attr);
292 g_free(utf8attr_name);
298 void xml_push_tag(XMLFile *file, XMLTag *tag)
300 g_return_if_fail(tag != NULL);
302 file->tag_stack = g_list_prepend(file->tag_stack, tag);
306 void xml_pop_tag(XMLFile *file)
310 if (!file->tag_stack) return;
312 tag = (XMLTag *)file->tag_stack->data;
315 file->tag_stack = g_list_remove(file->tag_stack, tag);
319 XMLTag *xml_get_current_tag(XMLFile *file)
322 return (XMLTag *)file->tag_stack->data;
327 GList *xml_get_current_tag_attr(XMLFile *file)
331 tag = xml_get_current_tag(file);
332 if (!tag) return NULL;
337 gchar *xml_get_element(XMLFile *file)
343 while ((end = strchr(file->bufp, '<')) == NULL)
344 if (xml_read_line(file) < 0) return NULL;
346 if (end == file->bufp)
349 str = g_strndup(file->bufp, end - file->bufp);
350 /* this is not XML1.0 strict */
352 xml_unescape_str(str);
355 xml_truncate_buf(file);
357 if (str[0] == '\0') {
362 utf8str = conv_codeset_strdup
364 conv_get_current_charset_str(),
367 g_warning("xml_get_element(): "
368 "faild to convert character set.\n");
376 gint xml_read_line(XMLFile *file)
378 gchar buf[XMLBUFSIZE];
381 if (fgets(buf, sizeof(buf), file->fp) == NULL)
384 index = file->bufp - file->buf->str;
386 g_string_append(file->buf, buf);
388 file->bufp = file->buf->str + index;
393 void xml_truncate_buf(XMLFile *file)
397 len = file->bufp - file->buf->str;
399 g_string_erase(file->buf, 0, len);
400 file->bufp = file->buf->str;
404 gboolean xml_compare_tag(XMLFile *file, const gchar *name)
408 tag = xml_get_current_tag(file);
410 if (tag && strcmp(tag->tag, name) == 0)
416 XMLTag *xml_copy_tag(XMLTag *tag)
422 new_tag = g_new(XMLTag, 1);
423 new_tag->tag = XML_STRING_ADD(tag->tag);
424 new_tag->attr = NULL;
425 for (list = tag->attr; list != NULL; list = list->next) {
426 attr = xml_copy_attr((XMLAttr *)list->data);
427 new_tag->attr = g_list_append(new_tag->attr, attr);
433 XMLAttr *xml_copy_attr(XMLAttr *attr)
437 new_attr = g_new(XMLAttr, 1);
438 new_attr->name = XML_STRING_ADD(attr->name);
439 new_attr->value = g_strdup(attr->value);
444 gint xml_unescape_str(gchar *str)
453 while ((start = strchr(p, '&')) != NULL) {
454 if ((end = strchr(start + 1, ';')) == NULL) {
455 g_warning("Unescaped `&' appeared\n");
459 len = end - start + 1;
465 Xstrndup_a(esc_str, start, len, return -1);
466 if (!strcmp(esc_str, "<"))
468 else if (!strcmp(esc_str, ">"))
470 else if (!strcmp(esc_str, "&"))
472 else if (!strcmp(esc_str, "'"))
474 else if (!strcmp(esc_str, """))
482 memmove(start + 1, end + 1, strlen(end + 1) + 1);
490 gint xml_file_put_escape_str(FILE *fp, const gchar *str)
492 const gchar *src_codeset = CS_UTF_8;
493 const gchar *dest_codeset = conv_get_current_charset_str();
494 gchar *tmpstr = NULL;
497 g_return_val_if_fail(fp != NULL, -1);
501 tmpstr = conv_codeset_strdup(str, src_codeset, dest_codeset);
503 g_warning("xml_file_put_escape_str(): Faild to convert character set.");
504 tmpstr = g_strdup(str);
507 for (p = tmpstr; *p != '\0'; p++) {
534 void xml_free_node(XMLNode *node)
538 xml_free_tag(node->tag);
539 g_free(node->element);
543 static gboolean xml_free_func(GNode *node, gpointer data)
545 XMLNode *xmlnode = node->data;
547 xml_free_node(xmlnode);
551 void xml_free_tree(GNode *node)
553 g_return_if_fail(node != NULL);
555 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, xml_free_func,
558 g_node_destroy(node);
561 static void xml_free_tag(XMLTag *tag)
565 XML_STRING_FREE(tag->tag);
566 while (tag->attr != NULL) {
567 XMLAttr *attr = (XMLAttr *)tag->attr->data;
568 XML_STRING_FREE(attr->name);
571 tag->attr = g_list_remove(tag->attr, tag->attr->data);
576 static gint xml_get_parenthesis(XMLFile *file, gchar *buf, gint len)
583 while ((start = strchr(file->bufp, '<')) == NULL)
584 if (xml_read_line(file) < 0) return -1;
589 while ((end = strchr(file->bufp, '>')) == NULL)
590 if (xml_read_line(file) < 0) return -1;
592 strncpy2(buf, file->bufp, MIN(end - file->bufp + 1, len));
594 file->bufp = end + 1;
595 xml_truncate_buf(file);