2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
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 3 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/>.
28 #include "../codeconv.h"
31 /* if this is defined all attr.names and tag.names are stored
33 #if defined(SPARSE_MEMORY)
34 #include "stringtable.h"
36 static StringTable *xml_string_table;
38 static void xml_string_table_create(void)
40 if (xml_string_table == NULL)
41 xml_string_table = string_table_new();
43 #define XML_STRING_ADD(str) \
44 string_table_insert_string(xml_string_table, (str))
45 #define XML_STRING_FREE(str) \
46 string_table_free_string(xml_string_table, (str))
48 #define XML_STRING_TABLE_CREATE() \
49 xml_string_table_create()
51 #else /* !SPARSE_MEMORY */
53 #define XML_STRING_ADD(str) \
55 #define XML_STRING_FREE(str) \
58 #define XML_STRING_TABLE_CREATE()
60 #endif /* SPARSE_MEMORY */
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 newfile = g_new(XMLFile, 1);
74 newfile->fp = g_fopen(path, "rb");
80 XML_STRING_TABLE_CREATE();
82 newfile->buf = g_string_new(NULL);
83 newfile->bufp = newfile->buf->str;
86 newfile->encoding = NULL;
87 newfile->tag_stack = NULL;
89 newfile->is_empty_element = FALSE;
94 void xml_close_file(XMLFile *file)
96 g_return_if_fail(file != NULL);
98 if (file->fp) fclose(file->fp);
100 g_string_free(file->buf, TRUE);
103 g_free(file->encoding);
105 while (file->tag_stack != NULL)
111 static GNode *xml_build_tree(XMLFile *file, GNode *parent, guint level)
117 while (xml_parse_next_tag(file) == 0) {
118 if (file->level < level) break;
119 if (file->level == level) {
120 g_warning("xml_build_tree(): Parse error\n");
124 tag = xml_get_current_tag(file);
126 xmlnode = xml_node_new(xml_copy_tag(tag), NULL);
127 xmlnode->element = xml_get_element(file);
129 node = g_node_new(xmlnode);
131 node = g_node_append_data(parent, xmlnode);
133 xml_build_tree(file, node, file->level);
134 if (file->level == 0) break;
140 GNode *xml_parse_file(const gchar *path)
145 file = xml_open_file(path);
146 g_return_val_if_fail(file != NULL, NULL);
150 node = xml_build_tree(file, NULL, file->level);
152 xml_close_file(file);
154 #if defined(SPARSE_MEMORY)
155 if (debug_get_mode())
156 string_table_get_stats(xml_string_table);
162 gint xml_get_dtd(XMLFile *file)
164 gchar buf[XMLBUFSIZE];
167 if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) return -1;
169 if ((*bufp++ == '?') &&
170 (bufp = strcasestr(bufp, "xml")) &&
171 (bufp = strcasestr(bufp + 3, "version")) &&
172 (bufp = strchr(bufp + 7, '?'))) {
173 file->dtd = g_strdup(buf);
174 if ((bufp = strcasestr(buf, "encoding=\""))) {
176 extract_quote(bufp, '"');
177 file->encoding = g_strdup(bufp);
178 file->need_codeconv =
179 strcmp2(bufp, CS_INTERNAL);
181 file->encoding = g_strdup(CS_INTERNAL);
182 file->need_codeconv = FALSE;
185 g_warning("Can't get xml dtd\n");
192 gint xml_parse_next_tag(XMLFile *file)
194 gchar buf[XMLBUFSIZE];
200 if (file->is_empty_element == TRUE) {
201 file->is_empty_element = FALSE;
206 if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
207 g_warning("xml_parse_next_tag(): Can't parse next tag\n");
213 if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
214 g_warning("xml_parse_next_tag(): Tag name mismatch: %s (%s)\n", buf, xml_get_current_tag(file)->tag);
221 tag = xml_tag_new(NULL);
222 xml_push_tag(file, tag);
225 if (len > 0 && buf[len - 1] == '/') {
226 file->is_empty_element = TRUE;
230 if (strlen(buf) == 0) {
231 g_warning("xml_parse_next_tag(): Tag name is empty\n");
235 while (*bufp != '\0' && !g_ascii_isspace(*bufp)) bufp++;
237 if (file->need_codeconv) {
238 tag_str = conv_codeset_strdup(buf, file->encoding, CS_INTERNAL);
240 tag->tag = XML_STRING_ADD(tag_str);
243 tag->tag = XML_STRING_ADD(buf);
245 tag->tag = XML_STRING_ADD(buf);
249 if (file->need_codeconv) {
250 tag_str = conv_codeset_strdup(buf, file->encoding, CS_INTERNAL);
252 tag->tag = XML_STRING_ADD(tag_str);
255 tag->tag = XML_STRING_ADD(buf);
257 tag->tag = XML_STRING_ADD(buf);
260 /* parse attributes ( name=value ) */
265 gchar *utf8_attr_name;
266 gchar *utf8_attr_value;
270 while (g_ascii_isspace(*bufp)) bufp++;
272 if ((p = strchr(attr_name, '=')) == NULL) {
273 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
278 while (g_ascii_isspace(*bufp)) bufp++;
280 if (*bufp != '"' && *bufp != '\'') {
281 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
287 if ((p = strchr(attr_value, quote)) == NULL) {
288 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
294 g_strchomp(attr_name);
295 xml_unescape_str(attr_value);
296 if (file->need_codeconv) {
297 utf8_attr_name = conv_codeset_strdup
298 (attr_name, file->encoding, CS_INTERNAL);
299 utf8_attr_value = conv_codeset_strdup
300 (attr_value, file->encoding, CS_INTERNAL);
302 utf8_attr_name = g_strdup(attr_name);
303 if (!utf8_attr_value)
304 utf8_attr_value = g_strdup(attr_value);
306 attr = xml_attr_new(utf8_attr_name, utf8_attr_value);
307 g_free(utf8_attr_value);
308 g_free(utf8_attr_name);
310 attr = xml_attr_new(attr_name, attr_value);
312 xml_tag_add_attr(tag, attr);
315 tag->attr = g_list_reverse(tag->attr);
320 void xml_push_tag(XMLFile *file, XMLTag *tag)
322 g_return_if_fail(tag != NULL);
324 file->tag_stack = g_list_prepend(file->tag_stack, tag);
328 void xml_pop_tag(XMLFile *file)
332 if (!file->tag_stack) return;
334 tag = (XMLTag *)file->tag_stack->data;
337 file->tag_stack = g_list_remove(file->tag_stack, tag);
341 XMLTag *xml_get_current_tag(XMLFile *file)
344 return (XMLTag *)file->tag_stack->data;
349 GList *xml_get_current_tag_attr(XMLFile *file)
353 tag = xml_get_current_tag(file);
354 if (!tag) return NULL;
359 gchar *xml_get_element(XMLFile *file)
365 while ((end = strchr(file->bufp, '<')) == NULL)
366 if (xml_read_line(file) < 0) return NULL;
368 if (end == file->bufp)
371 str = g_strndup(file->bufp, end - file->bufp);
372 /* this is not XML1.0 strict */
374 xml_unescape_str(str);
377 xml_truncate_buf(file);
379 if (str[0] == '\0') {
384 if (!file->need_codeconv)
387 new_str = conv_codeset_strdup(str, file->encoding, CS_INTERNAL);
389 new_str = g_strdup(str);
395 gint xml_read_line(XMLFile *file)
397 gchar buf[XMLBUFSIZE];
400 if (fgets(buf, sizeof(buf), file->fp) == NULL)
403 index = file->bufp - file->buf->str;
405 g_string_append(file->buf, buf);
407 file->bufp = file->buf->str + index;
412 void xml_truncate_buf(XMLFile *file)
416 len = file->bufp - file->buf->str;
418 g_string_erase(file->buf, 0, len);
419 file->bufp = file->buf->str;
423 gboolean xml_compare_tag(XMLFile *file, const gchar *name)
427 tag = xml_get_current_tag(file);
429 if (tag && strcmp(tag->tag, name) == 0)
435 XMLNode *xml_node_new(XMLTag *tag, const gchar *text)
439 node = g_new(XMLNode, 1);
441 node->element = g_strdup(text);
446 XMLTag *xml_tag_new(const gchar *tag)
450 new_tag = g_new(XMLTag, 1);
452 new_tag->tag = XML_STRING_ADD(tag);
455 new_tag->attr = NULL;
460 XMLAttr *xml_attr_new(const gchar *name, const gchar *value)
464 new_attr = g_new(XMLAttr, 1);
465 new_attr->name = XML_STRING_ADD(name);
466 new_attr->value = g_strdup(value);
471 XMLAttr *xml_attr_new_int(const gchar *name, const gint value)
476 valuestr = g_strdup_printf("%d", value);
478 new_attr = g_new(XMLAttr, 1);
479 new_attr->name = XML_STRING_ADD(name);
480 new_attr->value = g_strdup(valuestr);
487 void xml_tag_add_attr(XMLTag *tag, XMLAttr *attr)
489 tag->attr = g_list_prepend(tag->attr, attr);
492 XMLTag *xml_copy_tag(XMLTag *tag)
498 new_tag = xml_tag_new(tag->tag);
499 for (list = tag->attr; list != NULL; list = list->next) {
500 attr = xml_copy_attr((XMLAttr *)list->data);
501 xml_tag_add_attr(new_tag, attr);
503 tag->attr = g_list_reverse(tag->attr);
508 XMLAttr *xml_copy_attr(XMLAttr *attr)
510 return xml_attr_new(attr->name, attr->value);
513 gint xml_unescape_str(gchar *str)
522 while ((start = strchr(p, '&')) != NULL) {
523 if ((end = strchr(start + 1, ';')) == NULL) {
524 g_warning("Unescaped '&' appeared\n");
528 len = end - start + 1;
534 Xstrndup_a(esc_str, start, len, return -1);
535 if (!strcmp(esc_str, "<"))
537 else if (!strcmp(esc_str, ">"))
539 else if (!strcmp(esc_str, "&"))
541 else if (!strcmp(esc_str, "'"))
543 else if (!strcmp(esc_str, """))
551 memmove(start + 1, end + 1, strlen(end + 1) + 1);
558 gint xml_file_put_escape_str(FILE *fp, const gchar *str)
562 g_return_val_if_fail(fp != NULL, -1);
566 for (p = str; *p != '\0'; p++) {
569 result = fputs("<", fp);
572 result = fputs(">", fp);
575 result = fputs("&", fp);
578 result = fputs("'", fp);
581 result = fputs(""", fp);
584 result = fputc(*p, fp);
588 return (result == EOF ? -1 : 0);
591 gint xml_file_put_xml_decl(FILE *fp)
593 g_return_val_if_fail(fp != NULL, -1);
594 XML_STRING_TABLE_CREATE();
596 return fprintf(fp, "<?xml version=\"1.0\" encoding=\"%s\"?>\n", CS_INTERNAL);
599 void xml_free_node(XMLNode *node)
603 xml_free_tag(node->tag);
604 g_free(node->element);
608 static gboolean xml_free_func(GNode *node, gpointer data)
610 XMLNode *xmlnode = node->data;
612 xml_free_node(xmlnode);
616 void xml_free_tree(GNode *node)
618 g_return_if_fail(node != NULL);
620 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, xml_free_func,
623 g_node_destroy(node);
626 void xml_free_tag(XMLTag *tag)
630 XML_STRING_FREE(tag->tag);
631 while (tag->attr != NULL) {
632 XMLAttr *attr = (XMLAttr *)tag->attr->data;
633 tag->attr = g_list_remove(tag->attr, tag->attr->data);
634 XML_STRING_FREE(attr->name);
635 g_free(attr->value); /* __not__ XML_STRING_FREE */
641 static gint xml_get_parenthesis(XMLFile *file, gchar *buf, gint len)
648 while ((start = strchr(file->bufp, '<')) == NULL)
649 if (xml_read_line(file) < 0) return -1;
654 while ((end = strchr(file->bufp, '>')) == NULL)
655 if (xml_read_line(file) < 0) return -1;
657 strncpy2(buf, file->bufp, MIN(end - file->bufp + 1, len));
659 file->bufp = end + 1;
660 xml_truncate_buf(file);
668 g_warning("failed to write part of xml tree\n"); \
672 static int xml_write_tree_recursive(GNode *node, FILE *fp)
678 g_return_val_if_fail(node != NULL, -1);
679 g_return_val_if_fail(fp != NULL, -1);
681 depth = g_node_depth(node) - 1;
682 for (i = 0; i < depth; i++)
683 TRY(fputs(" ", fp) != EOF);
685 tag = ((XMLNode *) node->data)->tag;
687 TRY(fprintf(fp, "<%s", tag->tag) > 0);
689 for (cur = tag->attr; cur != NULL; cur = g_list_next(cur)) {
690 XMLAttr *attr = (XMLAttr *) cur->data;
692 TRY(fprintf(fp, " %s=\"", attr->name) > 0);
693 TRY(xml_file_put_escape_str(fp, attr->value) == 0);
694 TRY(fputs("\"", fp) != EOF);
698 if (node->children) {
700 TRY(fputs(">\n", fp) != EOF);
702 child = node->children;
708 TRY(xml_write_tree_recursive(cur, fp) == 0);
711 for (i = 0; i < depth; i++)
712 TRY(fputs(" ", fp) != EOF);
713 TRY(fprintf(fp, "</%s>\n", tag->tag) > 0);
715 TRY(fputs(" />\n", fp) != EOF);
722 int xml_write_tree(GNode *node, FILE *fp)
724 return xml_write_tree_recursive(node, fp);
727 static gpointer copy_node_func(gpointer nodedata, gpointer data)
729 XMLNode *xmlnode = (XMLNode *) nodedata;
732 newxmlnode = g_new0(XMLNode, 1);
733 newxmlnode->tag = xml_copy_tag(xmlnode->tag);
734 newxmlnode->element = g_strdup(xmlnode->element);
739 GNode *xml_copy_tree(GNode *node)
741 return g_node_map(node, copy_node_func, NULL);