Commit 26988f57 authored by Tim Janik's avatar Tim Janik

glib/gtester.c:Implemented nonblocking reading properly now

svn path=/trunk/; revision=5892
parent a09ee4dc
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment