glib/gtester.c:Implemented nonblocking reading properly now
authorTim Janik <timj@src.gnome.org>
Tue, 20 Nov 2007 15:00:38 +0000 (15:00 +0000)
committerTim Janik <timj@src.gnome.org>
Tue, 20 Nov 2007 15:00:38 +0000 (15:00 +0000)
svn path=/trunk/; revision=5892

glib/gtester.c

index 23ad5aeeb134032080604efb3197a625ae1fbda8..f685b2e62a9a83b4b9f8e6b2feca102cef282c34 100644 (file)
 
 #include <glib.h>
 
-static GIOChannel* out = NULL;
-
-static void
-child_watch_cb (GPid     pid,
-               gint     status,
-               gpointer data)
-{
-  GMainLoop* loop = data;
-
-  g_spawn_close_pid (pid);
+/* the read buffer size in bytes */
+#define READ_BUFFER_SIZE 1024
 
-  //g_main_loop_quit (loop);
-}
+static GIOChannel* out = NULL;
 
 static gboolean
 child_out_cb (GIOChannel  * source,
@@ -44,22 +35,45 @@ child_out_cb (GIOChannel  * source,
 {
   GError* error = NULL;
   gsize length = 0;
-  gchar buffer[10];
-  GIOStatus status;
-
-  status = g_io_channel_read_chars (source, buffer, sizeof (buffer), &length, &error);
-  if (status == G_IO_STATUS_NORMAL)
-    {
-      g_print ("%d\n", length);
+  gchar buffer[READ_BUFFER_SIZE];
+  GIOStatus status = G_IO_STATUS_NORMAL;
+
+  while (status == G_IO_STATUS_NORMAL) {
+    status = g_io_channel_read_chars (source, buffer, sizeof (buffer), &length, &error);
+
+    switch (status) {
+    case G_IO_STATUS_NORMAL:
+           // FIXME: this is where the parsing happens
+           g_print ("%d\n", length);
+           break;
+    case G_IO_STATUS_AGAIN:
+           /* retry later */
+           break;
+    case G_IO_STATUS_ERROR:
+           /* fall through into EOF */
+           g_warning ("Error while reading data: %s",
+                      error->message);
+           g_error_free (error);
+    case G_IO_STATUS_EOF:
+           return FALSE;
     }
+  }
 
-  if (status != G_IO_STATUS_NORMAL || length != sizeof (buffer))
-    {
-      g_main_loop_quit (data);
-      return FALSE;
-    }
-  else
-    return TRUE;
+  return TRUE;
+}
+
+static void
+child_watch_cb (GPid     pid,
+               gint     status,
+               gpointer data)
+{
+  GMainLoop* loop = data;
+
+  g_spawn_close_pid (pid);
+
+  /* read the remaining data - also stops the io watch from being polled */
+  child_out_cb (out, G_IO_IN, data);
+  g_main_loop_quit (data);
 }
 
 int
@@ -71,13 +85,14 @@ main (int   argc,
   GPid       pid = 0;
   gchar    * working_folder;
   gchar    * child_argv[] = {
-    "cat",
-    "/proc/cpuinfo",
+    "git-annotate",
+    "--incremental",
+    "ChangeLog",
     NULL
   };
   gint        child_out;
 
-  working_folder = g_get_current_dir ();
+  working_folder = g_strdup ("/home/herzi/Hacking/Imendio/WebKit/WebCore"); //g_get_current_dir ();
   g_spawn_async_with_pipes (working_folder,
                 child_argv, NULL /* envp */,
                 G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
@@ -102,6 +117,7 @@ main (int   argc,
                     loop);
 
   out = g_io_channel_unix_new (child_out);
+  g_io_channel_set_flags (out, G_IO_FLAG_NONBLOCK, NULL); // FIXME: GError
   g_io_add_watch (out, G_IO_IN,
                  child_out_cb, loop);