summaryrefslogtreecommitdiff
path: root/glib/gtester.c
diff options
context:
space:
mode:
authorTim Janik <timj@src.gnome.org>2007-11-20 15:00:38 +0000
committerTim Janik <timj@src.gnome.org>2007-11-20 15:00:38 +0000
commit26988f57765fb624342abb8d722ad00140316462 (patch)
treed38178da42d5f36d0b84c56f257917bf70722af3 /glib/gtester.c
parenta09ee4dc20ccc2447b4efe238c97833a67150b3b (diff)
downloadglib-26988f57765fb624342abb8d722ad00140316462.tar.gz
glib/gtester.c:Implemented nonblocking reading properly now
svn path=/trunk/; revision=5892
Diffstat (limited to 'glib/gtester.c')
-rw-r--r--glib/gtester.c74
1 files changed, 45 insertions, 29 deletions
diff --git a/glib/gtester.c b/glib/gtester.c
index 23ad5aeeb..f685b2e62 100644
--- a/glib/gtester.c
+++ b/glib/gtester.c
@@ -23,19 +23,10 @@
#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);