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.
29 /* if this is defined all attr.names and tag.names are stored
31 #if defined(SPARSE_MEMORY)
32 #include "stringtable.h"
34 static StringTable *xml_string_table;
36 static void xml_string_table_create(void)
38 if (xml_string_table == NULL)
39 xml_string_table = string_table_new();
41 #define XML_STRING_ADD(str) \
42 string_table_insert_string(xml_string_table, (str))
43 #define XML_STRING_FREE(str) \
44 string_table_free_string(xml_string_table, (str))
46 #define XML_STRING_TABLE_CREATE() \
47 xml_string_table_create()
49 #else /* !SPARSE_MEMORY */
51 #define XML_STRING_ADD(str) \
53 #define XML_STRING_FREE(str) \
56 #define XML_STRING_TABLE_CREATE()
58 #endif /* SPARSE_MEMORY */
60 static void xml_free_tag (XMLTag *tag);
61 static gint xml_get_parenthesis (XMLFile *file,
65 XMLFile *xml_open_file(const gchar *path)
69 g_return_val_if_fail(path != NULL, NULL);
71 XML_STRING_TABLE_CREATE();
73 newfile = g_new(XMLFile, 1);
75 newfile->fp = fopen(path, "rb");
81 newfile->buf = g_string_new(NULL);
82 newfile->bufp = newfile->buf->str;
85 newfile->tag_stack = NULL;
87 newfile->is_empty_element = FALSE;
92 void xml_close_file(XMLFile *file)
94 g_return_if_fail(file != NULL);
96 if (file->fp) fclose(file->fp);
98 g_string_free(file->buf, TRUE);
102 while (file->tag_stack != NULL)
108 static GNode *xml_build_tree(XMLFile *file, GNode *parent, guint level)
114 while (xml_parse_next_tag(file) == 0) {
115 if (file->level < level) break;
116 if (file->level == level) {
117 g_warning("xml_build_tree(): Parse error\n");
121 tag = xml_get_current_tag(file);
123 xmlnode = g_new(XMLNode, 1);
124 xmlnode->tag = xml_copy_tag(tag);
125 xmlnode->element = xml_get_element(file);
127 node = g_node_new(xmlnode);
129 node = g_node_append_data(parent, xmlnode);
131 xml_build_tree(file, node, file->level);
132 if (file->level == 0) break;
138 GNode *xml_parse_file(const gchar *path)
143 file = xml_open_file(path);
144 g_return_val_if_fail(file != NULL, NULL);
148 node = xml_build_tree(file, NULL, file->level);
150 xml_close_file(file);
152 #if defined(SPARSE_MEMORY)
153 if (debug_get_mode())
154 string_table_get_stats(xml_string_table);
160 gint xml_get_dtd(XMLFile *file)
162 gchar buf[XMLBUFSIZE];
165 if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) return -1;
167 if ((*bufp++ == '?') &&
168 (bufp = strcasestr(bufp, "xml")) &&
169 (bufp = strcasestr(bufp + 3, "version")) &&
170 (bufp = strchr(bufp + 7, '?')))
171 file->dtd = g_strdup(buf);
173 g_warning("Can't get xml dtd\n");
180 gint xml_parse_next_tag(XMLFile *file)
182 gchar buf[XMLBUFSIZE];
187 if (file->is_empty_element == TRUE) {
188 file->is_empty_element = FALSE;
193 if (xml_get_parenthesis(file, buf, sizeof(buf)) < 0) {
194 g_warning("xml_parse_next_tag(): Can't parse next tag\n");
200 if (strcmp(xml_get_current_tag(file)->tag, buf + 1) != 0) {
201 g_warning("xml_parse_next_tag(): Tag name mismatch: %s\n", buf);
208 tag = g_new0(XMLTag, 1);
209 xml_push_tag(file, tag);
212 if (len > 0 && buf[len - 1] == '/') {
213 file->is_empty_element = TRUE;
217 if (strlen(buf) == 0) {
218 g_warning("xml_parse_next_tag(): Tag name is empty\n");
222 while (*bufp != '\0' && !isspace(*bufp)) bufp++;
224 tag->tag = XML_STRING_ADD(buf);
228 tag->tag = XML_STRING_ADD(buf);
231 /* parse attributes ( name=value ) */
239 while (isspace(*bufp)) bufp++;
241 if ((p = strchr(attr_name, '=')) == NULL) {
242 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
247 while (isspace(*bufp)) bufp++;
249 if (*bufp != '"' && *bufp != '\'') {
250 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
256 if ((p = strchr(attr_value, quote)) == NULL) {
257 g_warning("xml_parse_next_tag(): Syntax error in tag\n");
263 g_strchomp(attr_name);
264 xml_unescape_str(attr_value);
266 attr = g_new(XMLAttr, 1);
267 attr->name = XML_STRING_ADD(attr_name);
268 attr->value = g_strdup(attr_value);
269 tag->attr = g_list_append(tag->attr, attr);
275 void xml_push_tag(XMLFile *file, XMLTag *tag)
277 g_return_if_fail(tag != NULL);
279 file->tag_stack = g_list_prepend(file->tag_stack, tag);
283 void xml_pop_tag(XMLFile *file)
287 if (!file->tag_stack) return;
289 tag = (XMLTag *)file->tag_stack->data;
292 file->tag_stack = g_list_remove(file->tag_stack, tag);
296 XMLTag *xml_get_current_tag(XMLFile *file)
299 return (XMLTag *)file->tag_stack->data;
304 GList *xml_get_current_tag_attr(XMLFile *file)
308 tag = xml_get_current_tag(file);
309 if (!tag) return NULL;
314 gchar *xml_get_element(XMLFile *file)
319 while ((end = strchr(file->bufp, '<')) == NULL)
320 if (xml_read_line(file) < 0) return NULL;
322 if (end == file->bufp)
325 str = g_strndup(file->bufp, end - file->bufp);
326 /* this is not XML1.0 strict */
328 xml_unescape_str(str);
331 xml_truncate_buf(file);
333 if (str[0] == '\0') {
341 gint xml_read_line(XMLFile *file)
343 gchar buf[XMLBUFSIZE];
346 if (fgets(buf, sizeof(buf), file->fp) == NULL)
349 index = file->bufp - file->buf->str;
351 g_string_append(file->buf, buf);
353 file->bufp = file->buf->str + index;
358 void xml_truncate_buf(XMLFile *file)
362 len = file->bufp - file->buf->str;
364 g_string_erase(file->buf, 0, len);
365 file->bufp = file->buf->str;
369 gboolean xml_compare_tag(XMLFile *file, const gchar *name)
373 tag = xml_get_current_tag(file);
375 if (tag && strcmp(tag->tag, name) == 0)
381 XMLTag *xml_copy_tag(XMLTag *tag)
387 new_tag = g_new(XMLTag, 1);
388 new_tag->tag = XML_STRING_ADD(tag->tag);
389 new_tag->attr = NULL;
390 for (list = tag->attr; list != NULL; list = list->next) {
391 attr = xml_copy_attr((XMLAttr *)list->data);
392 new_tag->attr = g_list_append(new_tag->attr, attr);
398 XMLAttr *xml_copy_attr(XMLAttr *attr)
402 new_attr = g_new(XMLAttr, 1);
403 new_attr->name = XML_STRING_ADD(attr->name);
404 new_attr->value = g_strdup(attr->value);
409 gint xml_unescape_str(gchar *str)
418 while ((start = strchr(p, '&')) != NULL) {
419 if ((end = strchr(start + 1, ';')) == NULL) {
420 g_warning("Unescaped `&' appeared\n");
424 len = end - start + 1;
430 Xstrndup_a(esc_str, start, len, return -1);
431 if (!strcmp(esc_str, "<"))
433 else if (!strcmp(esc_str, ">"))
435 else if (!strcmp(esc_str, "&"))
437 else if (!strcmp(esc_str, "'"))
439 else if (!strcmp(esc_str, """))
447 memmove(start + 1, end + 1, strlen(end + 1) + 1);
454 gint xml_file_put_escape_str(FILE *fp, const gchar *str)
458 g_return_val_if_fail(fp != NULL, -1);
462 for (p = str; *p != '\0'; p++) {
487 void xml_free_node(XMLNode *node)
491 xml_free_tag(node->tag);
492 g_free(node->element);
496 static gboolean xml_free_func(GNode *node, gpointer data)
498 XMLNode *xmlnode = node->data;
500 xml_free_node(xmlnode);
504 void xml_free_tree(GNode *node)
506 g_return_if_fail(node != NULL);
508 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, xml_free_func,
511 g_node_destroy(node);
514 static void xml_free_tag(XMLTag *tag)
518 XML_STRING_FREE(tag->tag);
519 while (tag->attr != NULL) {
520 XMLAttr *attr = (XMLAttr *)tag->attr->data;
521 XML_STRING_FREE(attr->name);
524 tag->attr = g_list_remove(tag->attr, tag->attr->data);
529 static gint xml_get_parenthesis(XMLFile *file, gchar *buf, gint len)
536 while ((start = strchr(file->bufp, '<')) == NULL)
537 if (xml_read_line(file) < 0) return -1;
542 while ((end = strchr(file->bufp, '>')) == NULL)
543 if (xml_read_line(file) < 0) return -1;
545 strncpy2(buf, file->bufp, MIN(end - file->bufp + 1, len));
547 file->bufp = end + 1;
548 xml_truncate_buf(file);