9 #define BUFFER_SIZE 1024
11 gint main (gint argc, gchar * argv[])
13 GIOChannel *gio_r, *gio_w ;
17 char *srcdir = getenv ("srcdir");
18 gint rlength = 0, wlength = 0, length_out, line_term_len;
20 const gchar encoding[] = "EUC-JP", line_term[] = "\n";
26 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
28 setbuf(stdout, NULL); /* For debugging */
30 gio_r = g_io_channel_new_file (filename, "r", &gerr);
33 g_warning("Unable to open file %s: %s", filename, gerr->message);
37 gio_w = g_io_channel_new_file( "iochannel-test-outfile", "w", &gerr);
40 g_warning("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
45 g_io_channel_set_encoding (gio_r, encoding, &gerr);
48 g_warning(gerr->message);
53 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
55 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
56 if (status == G_IO_STATUS_ERROR)
58 g_warning(gerr->message);
62 flags = g_io_channel_get_flags (gio_r);
63 block = ! (flags & G_IO_FLAG_NONBLOCK);
65 g_print (" BLOCKING TRUE \n\n");
67 g_print (" BLOCKING FALSE \n\n");
69 line_term_len = strlen (line_term);
70 buffer = g_string_sized_new (BUFFER_SIZE);
75 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
76 while (status == G_IO_STATUS_AGAIN);
77 if (status != G_IO_STATUS_NORMAL)
80 rlength += buffer->len;
83 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
85 while (status == G_IO_STATUS_AGAIN);
86 if (status != G_IO_STATUS_NORMAL)
89 wlength += length_out;
91 if (length_out < buffer->len)
92 g_warning ("Only wrote part of the line.");
94 g_print ("%s", buffer->str);
95 g_string_truncate (buffer, 0);
100 case G_IO_STATUS_EOF:
102 case G_IO_STATUS_ERROR:
103 g_warning (gerr->message);
108 g_warning ("Abnormal exit from write loop.");
113 status = g_io_channel_flush (gio_w, &gerr);
114 while (status == G_IO_STATUS_AGAIN);
116 if (status == G_IO_STATUS_ERROR)
118 g_warning(gerr->message);
123 g_print ("read %d bytes, wrote %d bytes\n", rlength, wlength);
125 g_io_channel_unref(gio_r);
126 g_io_channel_unref(gio_w);