1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
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>
33 #include <glib/gstdio.h>
35 #include "e-xml-utils.h"
43 * @filename: path to an XML file
45 * Reads a local XML file and parses the contents into an XML document
46 * structure. If the XML file cannot be read or its contents are malformed,
47 * the function returns %NULL.
49 * Returns: an XML document structure, or %NULL
52 e_xml_parse_file (const gchar *filename)
54 xmlDocPtr result = NULL;
56 GMappedFile *mapped_file;
58 mapped_file = g_mapped_file_new (filename, FALSE, NULL);
60 result = xmlParseMemory (
61 g_mapped_file_get_contents (mapped_file),
62 g_mapped_file_get_length (mapped_file));
63 g_mapped_file_unref (mapped_file);
71 * @filename: path to a file to save to
72 * @doc: an XML document structure
74 * Writes the given XML document structure to the file given by @filename.
75 * If an error occurs while saving, the function returns -1 and sets errno.
77 * Returns: 0 on success, -1 on failure
80 e_xml_save_file (const gchar *filename,
89 gchar *dirname = g_path_get_dirname (filename);
90 gchar *basename = g_path_get_basename (filename);
91 gchar *savebasename = g_strconcat (".#", basename, NULL);
94 filesave = g_build_filename (dirname, savebasename, NULL);
95 g_free (savebasename);
98 fd = g_open (filesave, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0600);
104 xmlDocDumpFormatMemory (doc, &xmlbuf, &size, TRUE);
116 w = write (fd, xmlbuf + written, n - written);
117 } while (w == -1 && errno == EINTR);
121 } while (w != -1 && written < n);
125 if (written < n || fsync (fd) == -1) {
134 while ((ret = close (fd)) == -1 && errno == EINTR)
142 if (g_rename (filesave, filename) == -1) {
155 * e_xml_get_child_by_name:
156 * @parent: an XML node structure
157 * @child_name: element name of a child node
159 * Attempts to find a child element of @parent named @child_name.
160 * If no such child exists, the function returns %NULL.
162 * Returns: a child XML node structure, or %NULL
165 e_xml_get_child_by_name (const xmlNode *parent,
166 const xmlChar *child_name)
170 g_return_val_if_fail (parent != NULL, NULL);
171 g_return_val_if_fail (child_name != NULL, NULL);
173 for (child = parent->xmlChildrenNode; child != NULL; child = child->next) {
174 if (xmlStrcmp (child->name, child_name) == 0) {