glib/tests/iochannel-test.c
Ron Steinke e070fdea39 Modified Files: glib/ChangeLog glib/glib.def glib/glib/giochannel.c
Modified Files:
 	glib/ChangeLog glib/glib.def glib/glib/giochannel.c
 	glib/glib/giochannel.h glib/glib/giounix.c
 	glib/glib/giowin32.c
 	glib/docs/reference/glib/glib-sections.txt
 	glib/tests/iochannel-test.c glib/tests/unicode-collate.c
 	glib/tests/unicode-normalize.c
     Added Files:
 	glib/tests/iochannel-test-infile

        * glib/giochannel.c: API changes, fixes to
        error handling, some internal restructuring
        * glib/giochannel.h: API changes, documentation for
        elements in GIOChannel structure
        * glib/giounix.c: Matched API changes, implemented
        backend to set is_readable, is_writeable, is_seekable
        flags, added a test to catch large values of count
        for which the behavior of write() is undefined
        * glib/giowin32.c: Changed to match new prototypes for
        io_close() and io_seek(), removed references to
        G_IO_STATUS_INTR, set is_seekable flag in channel
        creation functions
        * glib.def: Renamed g_channel_error_quark() and
        g_channel_error_from_errno() to g_io_channel_error_quark() and
        g_io_channel_error_from_errno(); added new functions
        g_io_channel_get_buffered() and g_io_channel_set_buffered()
        * docs/reference/glib/glib-sections.txt: Modified iochannel
        section to reflect new functions and API changes
        * tests/iochannel-test.c: Fixed to work with API changes
        * tests/iochannel-test-infile: New file; input file
        for iochannel-test
        * tests/unicode-collate.c tests/unicode-normalize.c:
        Changed G_IO_FILE_MODE_READ to "r" to match API change
2001-07-20 20:14:37 +00:00

136 lines
3.4 KiB
C

#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;
gint rlength = 0, wlength = 0, length_out, line_term_len;
gboolean block;
const gchar encoding[] = "EUC-JP", line_term[] = G_IO_CHANNEL_UNIX_LINE_TERM;
GIOStatus status;
GIOFlags flags;
setbuf(stdout, NULL); /* For debugging */
gio_r = g_io_channel_new_file ("iochannel-test-infile", "r", &gerr);
if (gerr)
{
g_warning(gerr->message);
g_warning("Unable to open file %s", "iochannel-test-infile");
g_free(gerr);
return 0;
}
gio_w = g_io_channel_new_file( "iochannel-test-outfile", "w", &gerr);
if (gerr)
{
g_warning(gerr->message);
g_warning("Unable to open file %s", "iochannel-test-outfile");
g_free(gerr);
return 0;
}
g_io_channel_set_encoding (gio_r, encoding, &gerr);
if (gerr)
{
g_warning(gerr->message);
g_free(gerr);
return 0;
}
g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
if (status == G_IO_STATUS_ERROR)
{
g_warning(gerr->message);
g_error_free(gerr);
gerr = NULL;
}
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");
line_term_len = strlen (line_term);
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.");
do
status = g_io_channel_write_chars (gio_w, line_term,
line_term_len, &length_out, &gerr);
while (status == G_IO_STATUS_AGAIN);
if (status != G_IO_STATUS_NORMAL)
break;
if (length_out < line_term_len)
g_warning ("Only wrote part of the line term.");
g_print (": %s\n", buffer->str);
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);
if (status == G_IO_STATUS_ERROR)
{
g_warning(gerr->message);
g_error_free(gerr);
gerr = NULL;
}
g_print ("read %d bytes, wrote %d bytes\n", rlength, wlength);
g_io_channel_unref(gio_r);
g_io_channel_unref(gio_w);
return 0;
}