comparison src/util.c @ 10425:9903182f2aac

[gaim-migrate @ 11677] Added a util function to read an xml file and parse it into an xmlnode tree Changed accounts.xml reading to use xmlnode's committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Sun, 26 Dec 2004 22:52:52 +0000
parents 5b7a74d397cc
children bbe937302b47
comparison
equal deleted inserted replaced
10424:108151be77a3 10425:9903182f2aac
22 */ 22 */
23 #include "internal.h" 23 #include "internal.h"
24 24
25 #include "conversation.h" 25 #include "conversation.h"
26 #include "debug.h" 26 #include "debug.h"
27 #include "notify.h"
27 #include "prpl.h" 28 #include "prpl.h"
28 #include "prefs.h" 29 #include "prefs.h"
29 #include "util.h" 30 #include "util.h"
30 31
31 typedef struct 32 typedef struct
2046 g_free(filename_temp); 2047 g_free(filename_temp);
2047 2048
2048 return TRUE; 2049 return TRUE;
2049 } 2050 }
2050 2051
2052 xmlnode *
2053 gaim_util_read_xml_from_file(const char *filename, const char *description)
2054 {
2055 const char *user_dir = gaim_user_dir();
2056 gchar *filename_full;
2057 GError *error;
2058 gchar *contents = NULL;
2059 gsize length;
2060 xmlnode *node = NULL;
2061
2062 g_return_val_if_fail(user_dir != NULL, NULL);
2063
2064 gaim_debug_info("util", "Reading file %s from directory %s\n",
2065 filename, user_dir);
2066
2067 filename_full = g_build_filename(user_dir, filename, NULL);
2068
2069 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS))
2070 {
2071 gaim_debug_info("util", "File %s does not exist (this is not "
2072 "necessarily an error)\n", filename_full);
2073 g_free(filename_full);
2074 return NULL;
2075 }
2076
2077 if (!g_file_get_contents(filename_full, &contents, &length, &error))
2078 {
2079 gaim_debug_error("util", "Error reading file %s: %s\n",
2080 filename_full, error->message);
2081 g_error_free(error);
2082 }
2083
2084 if ((contents != NULL) && (length > 0))
2085 {
2086 node = xmlnode_from_str(contents, length);
2087
2088 /* If we were unable to parse the file then save its contents to a backup file */
2089 if (node == NULL)
2090 {
2091 gchar *filename_temp;
2092
2093 filename_temp = g_strdup_printf("%s~", filename);
2094 gaim_debug_error("util", "Error parsing file %s. Rrenaming old "
2095 "file to %s\n", filename_full, filename_temp);
2096 gaim_util_write_data_to_file(filename_temp, contents, length);
2097 g_free(filename_temp);
2098 }
2099
2100 g_free(contents);
2101 }
2102
2103 /* If we could not parse the file then show the user an error message */
2104 if (node == NULL)
2105 {
2106 gchar *title, *msg;
2107 title = g_strdup_printf(_("Error Reading %s"), filename);
2108 msg = g_strdup_printf(_("An error was encountered reading your "
2109 "%s. They have not been loaded, and the old file "
2110 "been renamed to %s~."), description, filename_full);
2111 gaim_notify_error(NULL, NULL, title, msg);
2112 g_free(title);
2113 g_free(msg);
2114 }
2115
2116 g_free(filename_full);
2117
2118 return node;
2119 }
2120
2051 /* 2121 /*
2052 * Like mkstemp() but returns a file pointer, uses a pre-set template, 2122 * Like mkstemp() but returns a file pointer, uses a pre-set template,
2053 * uses the semantics of tempnam() for the directory to use and allocates 2123 * uses the semantics of tempnam() for the directory to use and allocates
2054 * the space for the filepath. 2124 * the space for the filepath.
2055 * 2125 *