1 #undef G_DISABLE_ASSERT
12 #define BUFFER_SIZE 1024
14 static const gchar *datapath;
17 test_small_writes (void)
20 GIOStatus status = G_IO_STATUS_ERROR;
25 io = g_io_channel_new_file ("iochannel-test-outfile", "w", &error);
28 g_warning ("Unable to open file %s: %s",
29 "iochannel-test-outfile",
31 g_clear_error (&error);
36 g_io_channel_set_encoding (io, NULL, NULL);
37 g_io_channel_set_buffer_size (io, 1022);
39 cnt = 2 * g_io_channel_get_buffer_size (io);
44 status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL);
45 if (status == G_IO_STATUS_ERROR)
47 if (status == G_IO_STATUS_NORMAL)
51 g_assert (status == G_IO_STATUS_NORMAL);
53 g_io_channel_unref (io);
57 gint main (gint argc, gchar * argv[])
59 GIOChannel *gio_r, *gio_w ;
66 const gchar encoding[] = "EUC-JP";
69 if (g_getenv ("G_TEST_DATA"))
70 datapath = g_getenv ("G_TEST_DATA");
74 filename = g_build_filename (datapath, "iochannel-test-infile", NULL);
76 setbuf (stdout, NULL); /* For debugging */
78 gio_r = g_io_channel_new_file (filename, "r", &gerr);
81 g_warning ("Unable to open file %s: %s", filename, gerr->message);
82 g_clear_error (&gerr);
85 gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &gerr);
88 g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
89 g_clear_error (&gerr);
93 g_io_channel_set_encoding (gio_r, encoding, &gerr);
96 g_warning ("%s", gerr->message);
97 /* Keep going if this is just a case of iconv not supporting EUC-JP, see bug 428048 */
98 if (gerr->code != G_CONVERT_ERROR_NO_CONVERSION)
100 g_clear_error (&gerr);
103 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
105 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
106 if (status == G_IO_STATUS_ERROR)
108 g_warning ("%s", gerr->message);
109 g_clear_error (&gerr);
111 buffer = g_string_sized_new (BUFFER_SIZE);
116 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
117 while (status == G_IO_STATUS_AGAIN);
118 if (status != G_IO_STATUS_NORMAL)
121 rlength += buffer->len;
124 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
126 while (status == G_IO_STATUS_AGAIN);
127 if (status != G_IO_STATUS_NORMAL)
130 wlength += length_out;
132 if (length_out < buffer->len)
133 g_warning ("Only wrote part of the line.");
136 g_print ("%s", buffer->str);
138 g_string_truncate (buffer, 0);
143 case G_IO_STATUS_EOF:
145 case G_IO_STATUS_ERROR:
146 g_warning ("%s", gerr->message);
147 g_clear_error (&gerr);
150 g_warning ("Abnormal exit from write loop.");
155 status = g_io_channel_flush (gio_w, &gerr);
156 while (status == G_IO_STATUS_AGAIN);
158 if (status == G_IO_STATUS_ERROR)
160 g_warning ("%s", gerr->message);
161 g_clear_error (&gerr);
165 g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
168 g_io_channel_unref(gio_r);
169 g_io_channel_unref(gio_w);
171 test_small_writes ();