1 #undef G_DISABLE_ASSERT
12 #define BUFFER_SIZE 1024
14 gint main (gint argc, gchar * argv[])
16 GIOChannel *gio_r, *gio_w ;
20 char *srcdir = getenv ("srcdir");
24 const gchar encoding[] = "EUC-JP";
30 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
32 setbuf (stdout, NULL); /* For debugging */
34 gio_r = g_io_channel_new_file (filename, "r", &gerr);
37 g_warning ("Unable to open file %s: %s", filename, gerr->message);
41 gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &gerr);
44 g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
49 g_io_channel_set_encoding (gio_r, encoding, &gerr);
52 g_warning (gerr->message);
57 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
59 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
60 if (status == G_IO_STATUS_ERROR)
62 g_warning (gerr->message);
66 flags = g_io_channel_get_flags (gio_r);
67 buffer = g_string_sized_new (BUFFER_SIZE);
72 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
73 while (status == G_IO_STATUS_AGAIN);
74 if (status != G_IO_STATUS_NORMAL)
77 rlength += buffer->len;
80 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
82 while (status == G_IO_STATUS_AGAIN);
83 if (status != G_IO_STATUS_NORMAL)
86 wlength += length_out;
88 if (length_out < buffer->len)
89 g_warning ("Only wrote part of the line.");
92 g_print ("%s", buffer->str);
94 g_string_truncate (buffer, 0);
101 case G_IO_STATUS_ERROR:
102 g_warning (gerr->message);
107 g_warning ("Abnormal exit from write loop.");
112 status = g_io_channel_flush (gio_w, &gerr);
113 while (status == G_IO_STATUS_AGAIN);
115 if (status == G_IO_STATUS_ERROR)
117 g_warning (gerr->message);
123 g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
126 g_io_channel_unref(gio_r);
127 g_io_channel_unref(gio_w);