Mercurial > pidgin
comparison libpurple/util.c @ 25086:774ef2a2e7f8
added a more generic function to read xml files (and made the util version into a wrapper) so it can be used by the
theme loaders
author | Justin Rodriguez <ffdragon@soc.pidgin.im> |
---|---|
date | Mon, 16 Jun 2008 21:43:34 +0000 |
parents | dea8b856466e |
children | 60d3ba8c9047 |
comparison
equal
deleted
inserted
replaced
25085:fecc8e2612c4 | 25086:774ef2a2e7f8 |
---|---|
2748 } | 2748 } |
2749 | 2749 |
2750 xmlnode * | 2750 xmlnode * |
2751 purple_util_read_xml_from_file(const char *filename, const char *description) | 2751 purple_util_read_xml_from_file(const char *filename, const char *description) |
2752 { | 2752 { |
2753 const char *user_dir = purple_user_dir(); | 2753 return xmlnode_from_file(purple_user_dir(), filename, description, "util"); |
2754 gchar *filename_full; | |
2755 GError *error = NULL; | |
2756 gchar *contents = NULL; | |
2757 gsize length; | |
2758 xmlnode *node = NULL; | |
2759 | |
2760 g_return_val_if_fail(user_dir != NULL, NULL); | |
2761 | |
2762 purple_debug_info("util", "Reading file %s from directory %s\n", | |
2763 filename, user_dir); | |
2764 | |
2765 filename_full = g_build_filename(user_dir, filename, NULL); | |
2766 | |
2767 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS)) | |
2768 { | |
2769 purple_debug_info("util", "File %s does not exist (this is not " | |
2770 "necessarily an error)\n", filename_full); | |
2771 g_free(filename_full); | |
2772 return NULL; | |
2773 } | |
2774 | |
2775 if (!g_file_get_contents(filename_full, &contents, &length, &error)) | |
2776 { | |
2777 purple_debug_error("util", "Error reading file %s: %s\n", | |
2778 filename_full, error->message); | |
2779 g_error_free(error); | |
2780 } | |
2781 | |
2782 if ((contents != NULL) && (length > 0)) | |
2783 { | |
2784 node = xmlnode_from_str(contents, length); | |
2785 | |
2786 /* If we were unable to parse the file then save its contents to a backup file */ | |
2787 if (node == NULL) | |
2788 { | |
2789 gchar *filename_temp; | |
2790 | |
2791 filename_temp = g_strdup_printf("%s~", filename); | |
2792 purple_debug_error("util", "Error parsing file %s. Renaming old " | |
2793 "file to %s\n", filename_full, filename_temp); | |
2794 purple_util_write_data_to_file(filename_temp, contents, length); | |
2795 g_free(filename_temp); | |
2796 } | |
2797 | |
2798 g_free(contents); | |
2799 } | |
2800 | |
2801 /* If we could not parse the file then show the user an error message */ | |
2802 if (node == NULL) | |
2803 { | |
2804 gchar *title, *msg; | |
2805 title = g_strdup_printf(_("Error Reading %s"), filename); | |
2806 msg = g_strdup_printf(_("An error was encountered reading your " | |
2807 "%s. They have not been loaded, and the old file " | |
2808 "has been renamed to %s~."), description, filename_full); | |
2809 purple_notify_error(NULL, NULL, title, msg); | |
2810 g_free(title); | |
2811 g_free(msg); | |
2812 } | |
2813 | |
2814 g_free(filename_full); | |
2815 | |
2816 return node; | |
2817 } | 2754 } |
2818 | 2755 |
2819 /* | 2756 /* |
2820 * Like mkstemp() but returns a file pointer, uses a pre-set template, | 2757 * Like mkstemp() but returns a file pointer, uses a pre-set template, |
2821 * uses the semantics of tempnam() for the directory to use and allocates | 2758 * uses the semantics of tempnam() for the directory to use and allocates |