add error reporting to create_preset_from_xml_file

This commit is contained in:
Tomasz Moń
2009-03-17 12:54:46 +01:00
parent cac7e74d71
commit d24cc97f5d
3 changed files with 18 additions and 11 deletions

9
gui.c
View File

@@ -665,9 +665,14 @@ static void action_open_preset_cb(GtkAction *action)
gboolean loaded = FALSE; gboolean loaded = FALSE;
while (!loaded && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) { while (!loaded && gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT) {
GError *error = NULL;
gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog)); gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
Preset *preset = create_preset_from_xml_file(filename); Preset *preset = create_preset_from_xml_file(filename, &error);
if (preset != NULL) { if (error) {
show_error_message(window, error->message);
g_error_free(error);
error = NULL;
} else if (preset != NULL) {
apply_preset_to_gui(preset); apply_preset_to_gui(preset);
gtk_widget_hide(dialog); gtk_widget_hide(dialog);

View File

@@ -118,21 +118,23 @@ static void XMLCALL text_cb(void *data, const char* text, int len)
/** /**
* \param filename valid path to file * \param filename valid path to file
* \param error return location for an error
* *
* Tries to open file pointed by path, then parses it. * Tries to open file pointed by path, then parses it.
* *
* \return Preset which must be freed using preset_free, or NULL on error. * \return Preset which must be freed using preset_free, or NULL on error.
**/ **/
Preset *create_preset_from_xml_file(gchar *filename) Preset *create_preset_from_xml_file(gchar *filename, GError **error)
{ {
GFile *file; GFile *file;
GError *error = NULL; GError *err = NULL;
gchar *contents; gchar *contents;
file = g_file_new_for_path(filename); file = g_file_new_for_path(filename);
if (g_file_get_contents(filename, &contents, NULL, &error) == FALSE) { if (g_file_get_contents(filename, &contents, NULL, &err) == FALSE) {
g_message("Failed to get %s contents: %s", filename, error->message); g_message("Failed to get %s contents: %s", filename, err->message);
g_error_free(error); *error = g_error_copy(err);
g_error_free(err);
g_object_unref(file); g_object_unref(file);
return NULL; return NULL;
} }
@@ -151,9 +153,9 @@ Preset *create_preset_from_xml_file(gchar *filename)
XML_SetCharacterDataHandler(p, text_cb); XML_SetCharacterDataHandler(p, text_cb);
if (XML_Parse(p, contents, strlen(contents), XML_TRUE) != XML_STATUS_OK) { if (XML_Parse(p, contents, strlen(contents), XML_TRUE) != XML_STATUS_OK) {
g_warning("Parse error at line %d:\n%s", g_set_error(error, 0, 0, "Parse error at line %d:\n%s",
(int)XML_GetCurrentLineNumber(p), (int)XML_GetCurrentLineNumber(p),
XML_ErrorString(XML_GetErrorCode(p))); XML_ErrorString(XML_GetErrorCode(p)));
preset_free(ad->preset); preset_free(ad->preset);
g_slice_free(AppData, ad); g_slice_free(AppData, ad);
g_free(contents); g_free(contents);

View File

@@ -30,7 +30,7 @@ typedef struct {
GList *params; GList *params;
} Preset; } Preset;
Preset *create_preset_from_xml_file(gchar *filename); Preset *create_preset_from_xml_file(gchar *filename, GError **error);
Preset *create_preset_from_data(GString *data); Preset *create_preset_from_data(GString *data);
void preset_free(Preset *preset); void preset_free(Preset *preset);