9 #define BUFFER_SIZE 1024
11 gint main (gint argc, gchar * argv[])
13 GIOChannel *gio_r, *gio_w ;
16 gint rlength = 0, wlength = 0, length_out, line_term_len;
18 const gchar encoding[] = "EUC-JP", line_term[] = G_IO_CHANNEL_UNIX_LINE_TERM;
22 setbuf(stdout, NULL); /* For debugging */
24 gio_r = g_io_channel_new_file ("iochannel-test-infile", "r", &gerr);
27 g_warning(gerr->message);
28 g_warning("Unable to open file %s", "iochannel-test-infile");
32 gio_w = g_io_channel_new_file( "iochannel-test-outfile", "w", &gerr);
35 g_warning(gerr->message);
36 g_warning("Unable to open file %s", "iochannel-test-outfile");
41 g_io_channel_set_encoding (gio_r, encoding, &gerr);
44 g_warning(gerr->message);
49 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
51 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
52 if (status == G_IO_STATUS_ERROR)
54 g_warning(gerr->message);
58 flags = g_io_channel_get_flags (gio_r);
59 block = ! (flags & G_IO_FLAG_NONBLOCK);
61 g_print (" BLOCKING TRUE \n\n");
63 g_print (" BLOCKING FALSE \n\n");
65 line_term_len = strlen (line_term);
66 buffer = g_string_sized_new (BUFFER_SIZE);
71 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
72 while (status == G_IO_STATUS_AGAIN);
73 if (status != G_IO_STATUS_NORMAL)
76 rlength += buffer->len;
79 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
81 while (status == G_IO_STATUS_AGAIN);
82 if (status != G_IO_STATUS_NORMAL)
85 wlength += length_out;
87 if (length_out < buffer->len)
88 g_warning ("Only wrote part of the line.");
91 status = g_io_channel_write_chars (gio_w, line_term,
92 line_term_len, &length_out, &gerr);
93 while (status == G_IO_STATUS_AGAIN);
94 if (status != G_IO_STATUS_NORMAL)
97 if (length_out < line_term_len)
98 g_warning ("Only wrote part of the line term.");
100 g_print (": %s\n", buffer->str);
101 g_string_truncate (buffer, 0);
106 case G_IO_STATUS_EOF:
108 case G_IO_STATUS_ERROR:
109 g_warning (gerr->message);
114 g_warning ("Abnormal exit from write loop.");
119 status = g_io_channel_flush (gio_w, &gerr);
120 while (status == G_IO_STATUS_AGAIN);
122 if (status == G_IO_STATUS_ERROR)
124 g_warning(gerr->message);
129 g_print ("read %d bytes, wrote %d bytes\n", rlength, wlength);
131 g_io_channel_unref(gio_r);
132 g_io_channel_unref(gio_w);