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