1 #undef G_DISABLE_ASSERT
12 #define BUFFER_SIZE 1024
15 test_small_writes (void)
18 GIOStatus status = G_IO_STATUS_ERROR;
23 io = g_io_channel_new_file ("iochannel-test-outfile", "w", &error);
26 g_warning ("Unable to open file %s: %s",
27 "iochannel-test-outfile",
29 g_clear_error (&error);
34 g_io_channel_set_encoding (io, NULL, NULL);
35 g_io_channel_set_buffer_size (io, 1022);
37 cnt = 2 * g_io_channel_get_buffer_size (io);
42 status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL);
43 if (status == G_IO_STATUS_ERROR)
45 if (status == G_IO_STATUS_NORMAL)
49 g_assert (status == G_IO_STATUS_NORMAL);
51 g_io_channel_unref (io);
55 gint main (gint argc, gchar * argv[])
57 GIOChannel *gio_r, *gio_w ;
61 char *srcdir = getenv ("srcdir");
65 const gchar encoding[] = "EUC-JP";
70 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL);
72 setbuf (stdout, NULL); /* For debugging */
74 gio_r = g_io_channel_new_file (filename, "r", &gerr);
77 g_warning ("Unable to open file %s: %s", filename, gerr->message);
78 g_clear_error (&gerr);
81 gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &gerr);
84 g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
85 g_clear_error (&gerr);
89 g_io_channel_set_encoding (gio_r, encoding, &gerr);
92 g_warning ("%s", gerr->message);
93 /* Keep going if this is just a case of iconv not supporting EUC-JP, see bug 428048 */
94 if (gerr->code != G_CONVERT_ERROR_NO_CONVERSION)
96 g_clear_error (&gerr);
99 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
101 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
102 if (status == G_IO_STATUS_ERROR)
104 g_warning ("%s", gerr->message);
105 g_clear_error (&gerr);
107 buffer = g_string_sized_new (BUFFER_SIZE);
112 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr);
113 while (status == G_IO_STATUS_AGAIN);
114 if (status != G_IO_STATUS_NORMAL)
117 rlength += buffer->len;
120 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
122 while (status == G_IO_STATUS_AGAIN);
123 if (status != G_IO_STATUS_NORMAL)
126 wlength += length_out;
128 if (length_out < buffer->len)
129 g_warning ("Only wrote part of the line.");
132 g_print ("%s", buffer->str);
134 g_string_truncate (buffer, 0);
139 case G_IO_STATUS_EOF:
141 case G_IO_STATUS_ERROR:
142 g_warning ("%s", gerr->message);
143 g_clear_error (&gerr);
146 g_warning ("Abnormal exit from write loop.");
151 status = g_io_channel_flush (gio_w, &gerr);
152 while (status == G_IO_STATUS_AGAIN);
154 if (status == G_IO_STATUS_ERROR)
156 g_warning ("%s", gerr->message);
157 g_clear_error (&gerr);
161 g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
164 g_io_channel_unref(gio_r);
165 g_io_channel_unref(gio_w);
167 test_small_writes ();