|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #undef G_DISABLE_ASSERT |
|
20 #undef G_LOG_DOMAIN |
|
21 |
|
22 #include <sys/types.h> |
|
23 #include <sys/stat.h> |
|
24 #include <fcntl.h> |
|
25 #include <glib.h> |
|
26 #include <string.h> |
|
27 #include <stdio.h> |
|
28 #include <stdlib.h> |
|
29 |
|
30 #ifdef __SYMBIAN32__ |
|
31 #include "mrt2_glib2_test.h" |
|
32 #endif /*__SYMBIAN32__*/ |
|
33 |
|
34 |
|
35 #define BUFFER_SIZE 1024 |
|
36 |
|
37 static void |
|
38 test_small_writes (void) |
|
39 { |
|
40 GIOChannel *io; |
|
41 GIOStatus status; |
|
42 guint cnt; |
|
43 gchar tmp; |
|
44 GError *error = NULL; |
|
45 |
|
46 io = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &error); |
|
47 if (error) |
|
48 { |
|
49 g_warning ("Unable to open file %s: %s", |
|
50 "iochannel-test-outfile", |
|
51 error->message); |
|
52 g_error_free (error); |
|
53 |
|
54 exit (1); |
|
55 } |
|
56 |
|
57 g_io_channel_set_encoding (io, NULL, NULL); |
|
58 g_io_channel_set_buffer_size (io, 1022); |
|
59 |
|
60 cnt = 2 * g_io_channel_get_buffer_size (io); |
|
61 tmp = 0; |
|
62 |
|
63 while (cnt) |
|
64 { |
|
65 status = g_io_channel_write_chars (io, &tmp, 1, NULL, NULL); |
|
66 if (status == G_IO_STATUS_ERROR) |
|
67 break; |
|
68 if (status == G_IO_STATUS_NORMAL) |
|
69 cnt--; |
|
70 } |
|
71 |
|
72 g_assert (status == G_IO_STATUS_NORMAL); |
|
73 |
|
74 g_io_channel_unref (io); |
|
75 } |
|
76 |
|
77 |
|
78 gint main (gint argc, gchar * argv[]) |
|
79 { |
|
80 GIOChannel *gio_r, *gio_w ; |
|
81 GError *gerr = NULL; |
|
82 GString *buffer; |
|
83 char *filename; |
|
84 char *srcdir = getenv ("srcdir"); |
|
85 gint rlength = 0; |
|
86 glong wlength = 0; |
|
87 gsize length_out; |
|
88 const gchar encoding[] = "ISO-8859-5"; |
|
89 GIOStatus status; |
|
90 GIOFlags flags; |
|
91 |
|
92 #ifdef __SYMBIAN32__ |
|
93 g_log_set_handler (NULL, G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL); |
|
94 g_set_print_handler(mrtPrintHandler); |
|
95 #endif /*__SYMBIAN32__*/ |
|
96 |
|
97 |
|
98 if (!srcdir) |
|
99 srcdir = "c:"; |
|
100 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL); |
|
101 |
|
102 setbuf (stdout, NULL); /* For debugging */ |
|
103 |
|
104 gio_r = g_io_channel_new_file (filename, "r", &gerr); |
|
105 if (gerr) |
|
106 { |
|
107 g_warning ("Unable to open file %s: %s", filename, gerr->message); |
|
108 g_error_free (gerr); |
|
109 |
|
110 g_assert(FALSE && "iochannel-test failed"); |
|
111 |
|
112 #if __SYMBIAN32__ |
|
113 testResultXml("iochannel-test"); |
|
114 #endif /* EMULATOR */ |
|
115 return 1; |
|
116 } |
|
117 gio_w = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &gerr); |
|
118 if (gerr) |
|
119 { |
|
120 g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message); |
|
121 g_error_free (gerr); |
|
122 #if __SYMBIAN32__ |
|
123 testResultXml("iochannel-test"); |
|
124 #endif /* EMULATOR */ |
|
125 |
|
126 return 1; |
|
127 } |
|
128 |
|
129 g_io_channel_set_encoding (gio_r, encoding, &gerr); |
|
130 if (gerr) |
|
131 { |
|
132 g_warning (gerr->message); |
|
133 /* Keep going if this is just a case of iconv not supporting EUC-JP, see bug 428048 */ |
|
134 if (gerr->code != G_CONVERT_ERROR_NO_CONVERSION) |
|
135 return 1; |
|
136 g_error_free (gerr); |
|
137 |
|
138 g_assert(FALSE && "iochannel-test failed"); |
|
139 |
|
140 #if __SYMBIAN32__ |
|
141 testResultXml("iochannel-test"); |
|
142 #endif /* EMULATOR */ |
|
143 |
|
144 return 1; |
|
145 } |
|
146 |
|
147 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE); |
|
148 |
|
149 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr); |
|
150 if (status == G_IO_STATUS_ERROR) |
|
151 { |
|
152 g_warning (gerr->message); |
|
153 g_assert(FALSE && "iochannel-test failed"); |
|
154 g_error_free (gerr); |
|
155 gerr = NULL; |
|
156 } |
|
157 flags = g_io_channel_get_flags (gio_r); |
|
158 buffer = g_string_sized_new (BUFFER_SIZE); |
|
159 |
|
160 while (TRUE) |
|
161 { |
|
162 do |
|
163 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr); |
|
164 while (status == G_IO_STATUS_AGAIN); |
|
165 if (status != G_IO_STATUS_NORMAL) |
|
166 break; |
|
167 |
|
168 rlength += buffer->len; |
|
169 |
|
170 do |
|
171 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len, |
|
172 &length_out, &gerr); |
|
173 while (status == G_IO_STATUS_AGAIN); |
|
174 if (status != G_IO_STATUS_NORMAL) |
|
175 break; |
|
176 |
|
177 wlength += length_out; |
|
178 |
|
179 if (length_out < buffer->len) |
|
180 { |
|
181 g_warning ("Only wrote part of the line."); |
|
182 g_assert(FALSE && "iochannel-test failed"); |
|
183 } |
|
184 |
|
185 |
|
186 #ifdef VERBOSE |
|
187 g_print ("%s", buffer->str); |
|
188 #endif |
|
189 g_string_truncate (buffer, 0); |
|
190 } |
|
191 |
|
192 switch (status) |
|
193 { |
|
194 case G_IO_STATUS_EOF: |
|
195 break; |
|
196 case G_IO_STATUS_ERROR: |
|
197 g_warning (gerr->message); |
|
198 g_error_free (gerr); |
|
199 gerr = NULL; |
|
200 break; |
|
201 default: |
|
202 g_warning ("Abnormal exit from write loop."); |
|
203 g_assert(FALSE && "iochannel-test failed"); |
|
204 break; |
|
205 } |
|
206 |
|
207 do |
|
208 status = g_io_channel_flush (gio_w, &gerr); |
|
209 while (status == G_IO_STATUS_AGAIN); |
|
210 |
|
211 if (status == G_IO_STATUS_ERROR) |
|
212 { |
|
213 g_warning (gerr->message); |
|
214 g_assert(FALSE && "iochannel-test failed"); |
|
215 g_error_free (gerr); |
|
216 gerr = NULL; |
|
217 } |
|
218 |
|
219 #ifdef VERBOSE |
|
220 g_print ("read %d bytes, wrote %ld bytes\n", rlength, wlength); |
|
221 #endif |
|
222 |
|
223 g_io_channel_unref(gio_r); |
|
224 g_io_channel_unref(gio_w); |
|
225 |
|
226 test_small_writes (); |
|
227 |
|
228 #if __SYMBIAN32__ |
|
229 testResultXml("iochannel-test"); |
|
230 #endif /* EMULATOR */ |
|
231 |
|
232 return 0; |
|
233 } |