tests: Drop unnecessary % from .test pattern match rule
[platform/upstream/glib.git] / tests / iochannel-test.c
1 #undef G_DISABLE_ASSERT
2 #undef G_LOG_DOMAIN
3
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <glib.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #define BUFFER_SIZE 1024
13
14 static const gchar *datapath;
15
16 static void
17 test_small_writes (void)
18 {
19   GIOChannel *io;
20   GIOStatus status = G_IO_STATUS_ERROR;
21   guint cnt; 
22   gchar tmp;
23   GError *error = NULL;
24
25   io = g_io_channel_new_file ("iochannel-test-outfile", "w", &error);
26   if (error)
27     {
28       g_warning ("Unable to open file %s: %s", 
29                  "iochannel-test-outfile", 
30                  error->message);
31       g_clear_error (&error);
32       
33       exit (1);
34     }
35
36   g_io_channel_set_encoding (io, NULL, NULL);
37   g_io_channel_set_buffer_size (io, 1022);
38
39   cnt = 2 * g_io_channel_get_buffer_size (io);
40   tmp = 0;
41  
42   while (cnt)
43     {
44       status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL);
45       if (status == G_IO_STATUS_ERROR)
46         break;
47       if (status == G_IO_STATUS_NORMAL)
48         cnt--;
49     }
50
51   g_assert (status == G_IO_STATUS_NORMAL);
52
53   g_io_channel_unref (io);
54 }
55
56
57 gint main (gint argc, gchar * argv[])
58 {
59     GIOChannel *gio_r, *gio_w ;
60     GError *gerr = NULL;
61     GString *buffer;
62     char *filename;
63     gint rlength = 0;
64     glong wlength = 0;
65     gsize length_out;
66     const gchar encoding[] = "EUC-JP";
67     GIOStatus status;
68
69     if (g_getenv ("G_TEST_DATA"))
70       datapath = g_getenv ("G_TEST_DATA");
71     else
72       datapath = ".";
73
74     filename = g_build_filename (datapath, "iochannel-test-infile", NULL);
75   
76     setbuf (stdout, NULL); /* For debugging */
77
78     gio_r = g_io_channel_new_file (filename, "r", &gerr);
79     if (gerr)
80       {
81         g_warning ("Unable to open file %s: %s", filename, gerr->message);
82         g_clear_error (&gerr);
83         return 1;
84       }
85     gio_w = g_io_channel_new_file ("iochannel-test-outfile", "w", &gerr);
86     if (gerr)
87       {
88         g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message);
89         g_clear_error (&gerr);
90         return 1;
91       }
92
93     g_io_channel_set_encoding (gio_r, encoding, &gerr);
94     if (gerr)
95       {
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)
99           return 1;
100         g_clear_error (&gerr);
101       }
102     
103     g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE);
104
105     status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr);
106     if (status == G_IO_STATUS_ERROR)
107       {
108         g_warning ("%s", gerr->message);
109         g_clear_error (&gerr);
110       }
111     buffer = g_string_sized_new (BUFFER_SIZE);
112
113     while (TRUE)
114     {
115         do
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)
119           break;
120
121         rlength += buffer->len;
122
123         do
124           status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len,
125             &length_out, &gerr);
126         while (status == G_IO_STATUS_AGAIN);
127         if (status != G_IO_STATUS_NORMAL)
128           break;
129
130         wlength += length_out;
131
132         if (length_out < buffer->len)
133           g_warning ("Only wrote part of the line.");
134
135 #ifdef VERBOSE
136         g_print ("%s", buffer->str);
137 #endif
138         g_string_truncate (buffer, 0);
139     }
140
141     switch (status)
142       {
143         case G_IO_STATUS_EOF:
144           break;
145         case G_IO_STATUS_ERROR:
146           g_warning ("%s", gerr->message);
147           g_clear_error (&gerr);
148           break;
149         default:
150           g_warning ("Abnormal exit from write loop.");
151           break;
152       }
153
154     do
155       status = g_io_channel_flush (gio_w, &gerr);
156     while (status == G_IO_STATUS_AGAIN);
157
158     if (status == G_IO_STATUS_ERROR)
159       {
160         g_warning ("%s", gerr->message);
161         g_clear_error (&gerr);
162       }
163
164 #ifdef VERBOSE
165     g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength);
166 #endif
167
168     g_io_channel_unref(gio_r);
169     g_io_channel_unref(gio_w);
170
171     test_small_writes ();
172     
173     return 0;
174 }