The following function reads a file asyncrhonously, with the output sent to stdout.
#define BUFFER_SIZE 4096
/* This is the callback that
will be called whenever
something happens on the
I/O channel associated
with the file. */
static gboolean
io_channel_callback
(GIOChannel *source,
GIOCondition condition,
gpointer data)
{
char buffer[BUFFER_SIZE + 1];
unsigned int bytes_read;
unsigned int i;
if (condition & G_IO_IN) {
/* Data is available. */
g_io_channel_read
(source, buffer,
sizeof (buffer),
&bytes_read);
for (i = 0; i < bytes_read; i++)
putchar (buffer[i]);
fflush (stdout);
}
/* An error happened while reading
the file. */
if (condition & G_IO_NVAL)
return FALSE;
/* We have reached the end of the
file. */
if (condition & G_IO_HUP) {
g_io_channel_close (source);
return FALSE;
}
/* Returning TRUE will make sure
the callback remains associated
to the channel. */
return TRUE;
}
static void
open_callback (GnomeVFSAsyncHandle *handle,
GIOChannel *channel,
GnomeVFSResult result,
gpointer data)
{
if (result != GNOME_VFS_OK) {
printf ("Error: %s.\n",
gnome_vfs_result_to_string
(result));
return;
}
printf ("Open: `%s'.\n",
(char *) data);
g_io_add_watch_full
(channel, G_PRIORITY_HIGH,
G_IO_IN | G_IO_NVAL | G_IO_HUP,
io_channel_callback,
handle, NULL);
}
void
start_cat (const char *uri)
{
GnomeVFSAsyncHandle *handle;
/* Start the `read' operation. */
gnome_vfs_async_open_as_channel
(&handle, uri, GNOME_VFS_OPEN_READ,
BUFFER_SIZE, open_callback, "data");
}