[libsoup] tld-parser: Add error handling



commit 520683b97f57c3677eddb0d906cdca8308a7686c
Author: Colin Walters <walters verbum org>
Date:   Tue Jun 26 11:20:33 2012 -0400

    tld-parser: Add error handling

 libsoup/tld-parser.c |   72 +++++++++++++++++++++++++++-----------------------
 1 files changed, 39 insertions(+), 33 deletions(-)
---
diff --git a/libsoup/tld-parser.c b/libsoup/tld-parser.c
index b50f90e..8eed694 100644
--- a/libsoup/tld-parser.c
+++ b/libsoup/tld-parser.c
@@ -18,51 +18,49 @@
 int
 main(int argc, char **argv)
 {
-	GFile *tlds_file, *inc_file;
-	GFileInputStream *file_reader;
-	GFileOutputStream *file_writer;
-	GDataInputStream *data_reader;
-	char *rule;
+	gboolean ret = FALSE;
+	GError *local_error = NULL;
+	GError **error = &local_error;
+	GFile *tlds_file = NULL;
+	GFile *inc_file = NULL;
+	GFileInputStream *file_reader = NULL;
+	GFileOutputStream *file_writer = NULL;
+	GDataInputStream *data_reader = NULL;
+	char *rule = NULL;
 
 	g_type_init ();
 
 	tlds_file = g_file_new_for_path (argv[1]);
-	file_reader = g_file_read (tlds_file, NULL, NULL);
-	g_object_unref (tlds_file);
-
+	file_reader = g_file_read (tlds_file, NULL, error);
 	if (!file_reader)
-		return 1;
+		goto out;
 
 	data_reader = g_data_input_stream_new (G_INPUT_STREAM (file_reader));
-	g_object_unref (file_reader);
-	if (!data_reader)
-		return 1;
 
 	inc_file = g_file_new_for_path (argv[2]);
-	file_writer = g_file_replace (inc_file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL);
-	g_object_unref (inc_file);
-
-	if (!file_writer) {
-		g_object_unref (data_reader);
-		return 1;
-	}
+	file_writer = g_file_replace (inc_file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, error);
+	if (!file_writer)
+		goto out;
 
 	do {
 		char *domain;
 		gsize size;
 		char output_line[MAX_LINE_LENGTH];
 		guint flags;
+		GError *temp_error = NULL;
 
-		rule = g_data_input_stream_read_line (data_reader, &size, NULL, NULL);
-
-		if (!rule)
+		g_free (rule);
+		rule = g_data_input_stream_read_line (data_reader, &size, NULL, &temp_error);
+		if (temp_error) {
+			g_propagate_error (error, temp_error);
+			goto out;
+		}
+		if (rule == NULL)
 			break;
 
 		/* If the line is empty or is a comment then ignore. */
-		if (!size || g_str_has_prefix (rule, "//")) {
-			g_free (rule);
+		if (!size || g_str_has_prefix (rule, "//"))
 			continue;
-		}
 
 		flags = 0;
 		domain = rule;
@@ -82,14 +80,22 @@ main(int argc, char **argv)
 			domain++;
 
 		size = g_snprintf (output_line, MAX_LINE_LENGTH, "{ \"%s\", %d },\n", g_strstrip(domain), flags);
-		g_output_stream_write (G_OUTPUT_STREAM (file_writer), output_line, size, NULL, NULL);
-
-		g_free (rule);
-
+		if (!g_output_stream_write (G_OUTPUT_STREAM (file_writer), output_line, size, NULL, error))
+			goto out;
 	} while (TRUE);
 
-	g_object_unref (data_reader);
-	g_object_unref (file_writer);
-
-	return 0;
+	ret = TRUE;
+ out:
+	if (local_error) {
+		g_printerr ("%s\n", local_error->message);
+		g_clear_error (&local_error);
+	}
+	g_free (rule);
+	g_clear_object (&tlds_file);
+	g_clear_object (&inc_file);
+	g_clear_object (&file_reader);
+	g_clear_object (&data_reader);
+	g_clear_object (&file_writer);
+
+	return ret ? 0 : 1;
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]