comparison libpurple/xmlnode.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 feb92c44db87
children 2ecd716746e6
comparison
equal deleted inserted replaced
25085:fecc8e2612c4 25086:774ef2a2e7f8
727 g_free(xpd); 727 g_free(xpd);
728 return ret; 728 return ret;
729 } 729 }
730 730
731 xmlnode * 731 xmlnode *
732 xmlnode_from_file(const char *dir, const char *filename, const char *description, const char *process)
733 {
734 gchar *filename_full;
735 GError *error = NULL;
736 gchar *contents = NULL;
737 gsize length;
738 xmlnode *node = NULL;
739
740 g_return_val_if_fail(dir != NULL, NULL);
741
742 purple_debug_info(process, "Reading file %s from directory %s\n",
743 filename, dir);
744
745 filename_full = g_build_filename(dir, filename, NULL);
746
747 if (!g_file_test(filename_full, G_FILE_TEST_EXISTS))
748 {
749 purple_debug_info(process, "File %s does not exist (this is not "
750 "necessarily an error)\n", filename_full);
751 g_free(filename_full);
752 return NULL;
753 }
754
755 if (!g_file_get_contents(filename_full, &contents, &length, &error))
756 {
757 purple_debug_error(process, "Error reading file %s: %s\n",
758 filename_full, error->message);
759 g_error_free(error);
760 }
761
762 if ((contents != NULL) && (length > 0))
763 {
764 node = xmlnode_from_str(contents, length);
765
766 /* If we were unable to parse the file then save its contents to a backup file */
767 if (node == NULL)
768 {
769 gchar *filename_temp, *filename_temp_full;
770
771 filename_temp = g_strdup_printf("%s~", filename);
772 filename_temp_full = g_build_filename(dir, filename_temp, NULL);
773
774 purple_debug_error("util", "Error parsing file %s. Renaming old "
775 "file to %s\n", filename_full, filename_temp);
776 purple_util_write_data_to_file_absolute(filename_temp_full, contents, length);
777
778 g_free(filename_temp_full);
779 g_free(filename_temp);
780 }
781
782 g_free(contents);
783 }
784
785 /* If we could not parse the file then show the user an error message */
786 if (node == NULL)
787 {
788 gchar *title, *msg;
789 title = g_strdup_printf(_("Error Reading %s"), filename);
790 msg = g_strdup_printf(_("An error was encountered reading your "
791 "%s. The file has not been loaded, and the old file "
792 "has been renamed to %s~."), description, filename_full);
793 purple_notify_error(NULL, NULL, title, msg);
794 g_free(title);
795 g_free(msg);
796 }
797
798 g_free(filename_full);
799
800 return node;
801 }
802
803 xmlnode *
732 xmlnode_copy(const xmlnode *src) 804 xmlnode_copy(const xmlnode *src)
733 { 805 {
734 xmlnode *ret; 806 xmlnode *ret;
735 xmlnode *child; 807 xmlnode *child;
736 xmlnode *sibling = NULL; 808 xmlnode *sibling = NULL;