[epiphany/gnome-3-12] uri-tester: Use gio async API to parse the patterns file



commit a31318f9fd0c80044a78f8b06175a38d8db3de0c
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Wed May 21 12:45:37 2014 +0200

    uri-tester: Use gio async API to parse the patterns file
    
    https://bugzilla.gnome.org/show_bug.cgi?id=730129

 embed/uri-tester.c |   73 +++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 56 insertions(+), 17 deletions(-)
---
diff --git a/embed/uri-tester.c b/embed/uri-tester.c
index 42ef3c7..eebd7b6 100644
--- a/embed/uri-tester.c
+++ b/embed/uri-tester.c
@@ -71,7 +71,7 @@ G_DEFINE_TYPE (UriTester, uri_tester, G_TYPE_OBJECT)
 static GString *
 uri_tester_fixup_regexp (const char *prefix, char *src);
 
-static gboolean
+static void
 uri_tester_parse_file_at_uri (UriTester *tester, const char *fileuri);
 
 static char *
@@ -708,26 +708,65 @@ uri_tester_parse_line (UriTester *tester, char *line)
   uri_tester_add_url_pattern (tester, "", "uri", line);
 }
 
-static gboolean
-uri_tester_parse_file_at_uri (UriTester *tester, const char *fileuri)
+static void
+file_parse_cb (GDataInputStream *stream, GAsyncResult *result, UriTester *tester)
 {
-  FILE *file;
-  char line[2000];
-  char *path = NULL;
-  gboolean result = FALSE;
-
-  path = g_filename_from_uri (fileuri, NULL, NULL);
-  if ((file = g_fopen (path, "r")))
-    {
-      while (fgets (line, 2000, file))
-        uri_tester_parse_line (tester, line);
-      fclose (file);
+  char *line;
+  GError *error = NULL;
 
-      result = TRUE;
+  line = g_data_input_stream_read_line_finish (stream, result, NULL, &error);
+  if (!line) {
+    if (error) {
+      LOG ("Error parsing file: %s\n", error->message);
+      g_error_free (error);
     }
-  g_free (path);
 
-  return result;
+    return;
+  }
+
+  uri_tester_parse_line (tester, line);
+  g_free (line);
+
+  g_data_input_stream_read_line_async (stream, G_PRIORITY_DEFAULT_IDLE, NULL,
+                                       (GAsyncReadyCallback)file_parse_cb, tester);
+}
+
+static void
+file_read_cb (GFile *file, GAsyncResult *result, UriTester *tester)
+{
+  GFileInputStream *stream;
+  GDataInputStream *data_stream;
+  GError *error = NULL;
+
+  stream = g_file_read_finish (file, result, &error);
+  if (!stream) {
+    char *path;
+
+    path = g_file_get_path (file);
+    LOG ("Error opening file %s for parsing: %s\n", path, error->message);
+    g_free (path);
+    g_error_free (error);
+
+    return;
+  }
+
+  data_stream = g_data_input_stream_new (G_INPUT_STREAM (stream));
+  g_object_unref (stream);
+
+  g_data_input_stream_read_line_async (data_stream, G_PRIORITY_DEFAULT_IDLE, NULL,
+                                       (GAsyncReadyCallback)file_parse_cb, tester);
+  g_object_unref (data_stream);
+}
+
+static void
+uri_tester_parse_file_at_uri (UriTester *tester, const char *fileuri)
+{
+  GFile *file;
+  GFileInputStream *stream;
+
+  file = g_file_new_for_uri (fileuri);
+  g_file_read_async (file, G_PRIORITY_DEFAULT_IDLE, NULL, (GAsyncReadyCallback)file_read_cb, tester);
+  g_object_unref (file);
 }
 
 static void


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