2002-07-04 17:19:30 +02:00
|
|
|
#undef G_DISABLE_ASSERT
|
|
|
|
#undef G_LOG_DOMAIN
|
|
|
|
|
2001-06-30 17:22:13 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <glib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 1024
|
|
|
|
|
|
|
|
gint main (gint argc, gchar * argv[])
|
|
|
|
{
|
|
|
|
GIOChannel *gio_r, *gio_w ;
|
|
|
|
GError *gerr = NULL;
|
|
|
|
GString *buffer;
|
2001-08-12 16:12:02 +02:00
|
|
|
char *filename;
|
|
|
|
char *srcdir = getenv ("srcdir");
|
2002-10-16 00:39:16 +02:00
|
|
|
gint rlength = 0;
|
|
|
|
glong wlength = 0;
|
|
|
|
gsize length_out;
|
2001-06-30 17:22:13 +02:00
|
|
|
gboolean block;
|
2002-10-16 00:39:16 +02:00
|
|
|
const gchar encoding[] = "EUC-JP";
|
2001-06-30 17:22:13 +02:00
|
|
|
GIOStatus status;
|
|
|
|
GIOFlags flags;
|
|
|
|
|
2001-08-12 16:12:02 +02:00
|
|
|
if (!srcdir)
|
|
|
|
srcdir = ".";
|
|
|
|
filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
|
|
|
|
|
2001-06-30 17:22:13 +02:00
|
|
|
setbuf(stdout, NULL); /* For debugging */
|
|
|
|
|
2001-08-12 16:12:02 +02:00
|
|
|
gio_r = g_io_channel_new_file (filename, "r", &gerr);
|
2001-07-20 22:14:37 +02:00
|
|
|
if (gerr)
|
|
|
|
{
|
2001-08-12 16:12:02 +02:00
|
|
|
g_warning("Unable to open file %s: %s", filename, gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
return 1;
|
2001-07-20 22:14:37 +02:00
|
|
|
}
|
|
|
|
gio_w = g_io_channel_new_file( "iochannel-test-outfile", "w", &gerr);
|
|
|
|
if (gerr)
|
|
|
|
{
|
2001-08-12 16:12:02 +02:00
|
|
|
g_warning("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
return 1;
|
2001-07-20 22:14:37 +02:00
|
|
|
}
|
2001-06-30 17:22:13 +02:00
|
|
|
|
|
|
|
g_io_channel_set_encoding (gio_r, encoding, &gerr);
|
2001-07-20 22:14:37 +02:00
|
|
|
if (gerr)
|
|
|
|
{
|
|
|
|
g_warning(gerr->message);
|
2001-08-12 16:12:02 +02:00
|
|
|
g_error_free(gerr);
|
|
|
|
return 1;
|
2001-07-20 22:14:37 +02:00
|
|
|
}
|
2001-06-30 17:22:13 +02:00
|
|
|
|
|
|
|
g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
|
|
|
|
|
|
|
|
status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
|
2001-07-20 22:14:37 +02:00
|
|
|
if (status == G_IO_STATUS_ERROR)
|
|
|
|
{
|
|
|
|
g_warning(gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
gerr = NULL;
|
|
|
|
}
|
2001-06-30 17:22:13 +02:00
|
|
|
flags = g_io_channel_get_flags (gio_r);
|
|
|
|
block = ! (flags & G_IO_FLAG_NONBLOCK);
|
|
|
|
if (block)
|
|
|
|
g_print (" BLOCKING TRUE \n\n");
|
|
|
|
else
|
|
|
|
g_print (" BLOCKING FALSE \n\n");
|
|
|
|
|
|
|
|
buffer = g_string_sized_new (BUFFER_SIZE);
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
|
|
|
|
while (status == G_IO_STATUS_AGAIN);
|
|
|
|
if (status != G_IO_STATUS_NORMAL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
rlength += buffer->len;
|
|
|
|
|
|
|
|
do
|
|
|
|
status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
|
|
|
|
&length_out, &gerr);
|
|
|
|
while (status == G_IO_STATUS_AGAIN);
|
|
|
|
if (status != G_IO_STATUS_NORMAL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
wlength += length_out;
|
|
|
|
|
|
|
|
if (length_out < buffer->len)
|
|
|
|
g_warning ("Only wrote part of the line.");
|
|
|
|
|
2001-08-03 22:42:06 +02:00
|
|
|
g_print ("%s", buffer->str);
|
2001-06-30 17:22:13 +02:00
|
|
|
g_string_truncate (buffer, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case G_IO_STATUS_EOF:
|
|
|
|
break;
|
|
|
|
case G_IO_STATUS_ERROR:
|
|
|
|
g_warning (gerr->message);
|
|
|
|
g_error_free (gerr);
|
|
|
|
gerr = NULL;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("Abnormal exit from write loop.");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
status = g_io_channel_flush (gio_w, &gerr);
|
|
|
|
while (status == G_IO_STATUS_AGAIN);
|
|
|
|
|
2001-07-20 22:14:37 +02:00
|
|
|
if (status == G_IO_STATUS_ERROR)
|
|
|
|
{
|
|
|
|
g_warning(gerr->message);
|
|
|
|
g_error_free(gerr);
|
|
|
|
gerr = NULL;
|
|
|
|
}
|
2001-06-30 17:22:13 +02:00
|
|
|
|
2002-10-16 00:39:16 +02:00
|
|
|
g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
|
2001-06-30 17:22:13 +02:00
|
|
|
|
|
|
|
g_io_channel_unref(gio_r);
|
|
|
|
g_io_channel_unref(gio_w);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|