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.
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, "r");
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)
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 ) */
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);
268 attr->name = XML_STRING_ADD(attr_name);
269 attr->value = g_strdup(attr_value);
270 tag->attr = g_list_append(tag->attr, attr);
276 void xml_push_tag(XMLFile *file, XMLTag *tag)
278 g_return_if_fail(tag != NULL);
280 file->tag_stack = g_list_prepend(file->tag_stack, tag);
284 void xml_pop_tag(XMLFile *file)
288 if (!file->tag_stack) return;
290 tag = (XMLTag *)file->tag_stack->data;
293 file->tag_stack = g_list_remove(file->tag_stack, tag);
297 XMLTag *xml_get_current_tag(XMLFile *file)
300 return (XMLTag *)file->tag_stack->data;
305 GList *xml_get_current_tag_attr(XMLFile *file)
309 tag = xml_get_current_tag(file);
310 if (!tag) return NULL;
315 gchar *xml_get_element(XMLFile *file)
320 while ((end = strchr(file->bufp, '<')) == NULL)
321 if (xml_read_line(file) < 0) return NULL;
323 if (end == file->bufp)
326 str = g_strndup(file->bufp, end - file->bufp);
327 /* this is not XML1.0 strict */
329 xml_unescape_str(str);
332 xml_truncate_buf(file);
334 if (str[0] == '\0') {
342 gint xml_read_line(XMLFile *file)
344 gchar buf[XMLBUFSIZE];
347 if (fgets(buf, sizeof(buf), file->fp) == NULL)
350 index = file->bufp - file->buf->str;
352 g_string_append(file->buf, buf);
354 file->bufp = file->buf->str + index;
359 void xml_truncate_buf(XMLFile *file)
363 len = file->bufp - file->buf->str;
365 g_string_erase(file->buf, 0, len);
366 file->bufp = file->buf->str;
370 gboolean xml_compare_tag(XMLFile *file, const gchar *name)
374 tag = xml_get_current_tag(file);
376 if (tag && strcmp(tag->tag, name) == 0)
382 XMLTag *xml_copy_tag(XMLTag *tag)
388 new_tag = g_new(XMLTag, 1);
389 new_tag->tag = XML_STRING_ADD(tag->tag);
390 new_tag->attr = NULL;
391 for (list = tag->attr; list != NULL; list = list->next) {
392 attr = xml_copy_attr((XMLAttr *)list->data);
393 new_tag->attr = g_list_append(new_tag->attr, attr);
399 XMLAttr *xml_copy_attr(XMLAttr *attr)
403 new_attr = g_new(XMLAttr, 1);
404 new_attr->name = XML_STRING_ADD(attr->name);
405 new_attr->value = g_strdup(attr->value);
410 gint xml_unescape_str(gchar *str)
419 while ((start = strchr(p, '&')) != NULL) {
420 if ((end = strchr(start + 1, ';')) == NULL) {
421 g_warning("Unescaped `&' appeared\n");
425 len = end - start + 1;
431 Xstrndup_a(esc_str, start, len, return -1);
432 if (!strcmp(esc_str, "<"))
434 else if (!strcmp(esc_str, ">"))
436 else if (!strcmp(esc_str, "&"))
438 else if (!strcmp(esc_str, "'"))
440 else if (!strcmp(esc_str, """))
448 memmove(start + 1, end + 1, strlen(end + 1) + 1);
455 gint xml_file_put_escape_str(FILE *fp, const gchar *str)
459 g_return_val_if_fail(fp != NULL, -1);
463 for (p = str; *p != '\0'; p++) {
488 void xml_free_node(XMLNode *node)
492 xml_free_tag(node->tag);
493 g_free(node->element);
497 static gboolean xml_free_func(GNode *node, gpointer data)
499 XMLNode *xmlnode = node->data;
501 xml_free_node(xmlnode);
505 void xml_free_tree(GNode *node)
507 g_return_if_fail(node != NULL);
509 g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, xml_free_func,
512 g_node_destroy(node);
515 static void xml_free_tag(XMLTag *tag)
519 XML_STRING_FREE(tag->tag);
520 while (tag->attr != NULL) {
521 XMLAttr *attr = (XMLAttr *)tag->attr->data;
522 XML_STRING_FREE(attr->name);
525 tag->attr = g_list_remove(tag->attr, tag->attr->data);
530 static gint xml_get_parenthesis(XMLFile *file, gchar *buf, gint len)
537 while ((start = strchr(file->bufp, '<')) == NULL)
538 if (xml_read_line(file) < 0) return -1;
543 while ((end = strchr(file->bufp, '>')) == NULL)
544 if (xml_read_line(file) < 0) return -1;
546 strncpy2(buf, file->bufp, MIN(end - file->bufp + 1, len));
548 file->bufp = end + 1;
549 xml_truncate_buf(file);