1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2005 Novell, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU Lesser General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
34 #include <glib/gstdio.h>
36 #include "e-xml-utils.h"
43 e_xml_parse_file (const char *filename)
45 xmlDocPtr result = NULL;
47 GMappedFile *mapped_file;
49 mapped_file = g_mapped_file_new (filename, FALSE, NULL);
51 result = xmlParseMemory (g_mapped_file_get_contents (mapped_file),
52 g_mapped_file_get_length (mapped_file));
53 g_mapped_file_free (mapped_file);
59 e_xml_save_file (const char *filename, xmlDocPtr doc)
63 size_t n, written = 0;
67 gchar *dirname = g_path_get_dirname (filename);
68 gchar *basename = g_path_get_basename (filename);
69 gchar *savebasename = g_strconcat (".#", basename, NULL);
72 filesave = g_build_filename (dirname, savebasename, NULL);
73 g_free (savebasename);
76 fd = g_open (filesave, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
82 xmlDocDumpFormatMemory (doc, &xmlbuf, &size, TRUE);
94 w = write (fd, xmlbuf + written, n - written);
95 } while (w == -1 && errno == EINTR);
99 } while (w != -1 && written < n);
103 if (written < n || fsync (fd) == -1) {
112 while ((ret = close (fd)) == -1 && errno == EINTR)
120 if (g_rename (filesave, filename) == -1) {
133 e_xml_get_child_by_name (const xmlNode *parent, const xmlChar *child_name)
137 g_return_val_if_fail (parent != NULL, NULL);
138 g_return_val_if_fail (child_name != NULL, NULL);
140 for (child = parent->xmlChildrenNode; child != NULL; child = child->next) {
141 if (xmlStrcmp (child->name, child_name) == 0) {