|
1 /* Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.*/ |
|
2 |
|
3 #undef G_DISABLE_ASSERT |
|
4 #undef G_LOG_DOMAIN |
|
5 |
|
6 #include <sys/types.h> |
|
7 #include <sys/stat.h> |
|
8 #include <fcntl.h> |
|
9 #include <glib.h> |
|
10 #include <string.h> |
|
11 #include <stdio.h> |
|
12 #include <stdlib.h> |
|
13 |
|
14 #ifdef __SYMBIAN32__ |
|
15 #include "mrt2_glib2_test.h" |
|
16 #endif /*__SYMBIAN32__*/ |
|
17 |
|
18 |
|
19 #define BUFFER_SIZE 1024 |
|
20 |
|
21 gint main (gint argc, gchar * argv[]) |
|
22 { |
|
23 GIOChannel *gio_r, *gio_w ; |
|
24 GError *gerr = NULL; |
|
25 GString *buffer; |
|
26 char *filename; |
|
27 char *srcdir = getenv ("srcdir"); |
|
28 gint rlength = 0; |
|
29 glong wlength = 0; |
|
30 gsize length_out; |
|
31 const gchar encoding[] = "ISO-8859-5"; |
|
32 GIOStatus status; |
|
33 GIOFlags flags; |
|
34 |
|
35 #ifdef __SYMBIAN32__ |
|
36 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); |
|
37 g_set_print_handler(mrtPrintHandler); |
|
38 #endif /*__SYMBIAN32__*/ |
|
39 |
|
40 |
|
41 if (!srcdir) |
|
42 srcdir = "c:"; |
|
43 filename = g_strconcat (srcdir, G_DIR_SEPARATOR_S, "iochannel-test-infile", NULL); |
|
44 |
|
45 setbuf (stdout, NULL); /* For debugging */ |
|
46 |
|
47 gio_r = g_io_channel_new_file (filename, "r", &gerr); |
|
48 if (gerr) |
|
49 { |
|
50 g_warning ("Unable to open file %s: %s", filename, gerr->message); |
|
51 g_error_free (gerr); |
|
52 |
|
53 g_assert(FALSE && "iochannel-test failed"); |
|
54 |
|
55 #if __SYMBIAN32__ |
|
56 testResultXml("iochannel-test"); |
|
57 #endif /* EMULATOR */ |
|
58 |
|
59 return 1; |
|
60 } |
|
61 gio_w = g_io_channel_new_file ("c:\\iochannel-test-outfile", "w", &gerr); |
|
62 if (gerr) |
|
63 { |
|
64 g_warning ("Unable to open file %s: %s", "iochannel-test-outfile", gerr->message); |
|
65 g_error_free (gerr); |
|
66 |
|
67 g_assert(FALSE && "iochannel-test failed"); |
|
68 |
|
69 #if __SYMBIAN32__ |
|
70 testResultXml("iochannel-test"); |
|
71 #endif /* EMULATOR */ |
|
72 |
|
73 return 1; |
|
74 } |
|
75 |
|
76 g_io_channel_set_encoding (gio_r, encoding, &gerr); |
|
77 if (gerr) |
|
78 { |
|
79 g_warning (gerr->message); |
|
80 g_error_free (gerr); |
|
81 |
|
82 g_assert(FALSE && "iochannel-test failed"); |
|
83 |
|
84 #if __SYMBIAN32__ |
|
85 testResultXml("iochannel-test"); |
|
86 #endif /* EMULATOR */ |
|
87 |
|
88 return 1; |
|
89 } |
|
90 |
|
91 g_io_channel_set_buffer_size (gio_r, BUFFER_SIZE); |
|
92 |
|
93 status = g_io_channel_set_flags (gio_r, G_IO_FLAG_NONBLOCK, &gerr); |
|
94 if (status == G_IO_STATUS_ERROR) |
|
95 { |
|
96 g_warning (gerr->message); |
|
97 g_assert(FALSE && "iochannel-test failed"); |
|
98 g_error_free (gerr); |
|
99 gerr = NULL; |
|
100 } |
|
101 flags = g_io_channel_get_flags (gio_r); |
|
102 buffer = g_string_sized_new (BUFFER_SIZE); |
|
103 |
|
104 while (TRUE) |
|
105 { |
|
106 do |
|
107 status = g_io_channel_read_line_string (gio_r, buffer, NULL, &gerr); |
|
108 while (status == G_IO_STATUS_AGAIN); |
|
109 if (status != G_IO_STATUS_NORMAL) |
|
110 break; |
|
111 |
|
112 rlength += buffer->len; |
|
113 |
|
114 do |
|
115 status = g_io_channel_write_chars (gio_w, buffer->str, buffer->len, |
|
116 &length_out, &gerr); |
|
117 while (status == G_IO_STATUS_AGAIN); |
|
118 if (status != G_IO_STATUS_NORMAL) |
|
119 break; |
|
120 |
|
121 wlength += length_out; |
|
122 |
|
123 if (length_out < buffer->len) |
|
124 { |
|
125 g_warning ("Only wrote part of the line."); |
|
126 g_assert(FALSE && "iochannel-test failed"); |
|
127 } |
|
128 |
|
129 |
|
130 #ifdef VERBOSE |
|
131 g_print ("%s", buffer->str); |
|
132 #endif |
|
133 g_string_truncate (buffer, 0); |
|
134 } |
|
135 |
|
136 switch (status) |
|
137 { |
|
138 case G_IO_STATUS_EOF: |
|
139 break; |
|
140 case G_IO_STATUS_ERROR: |
|
141 g_warning (gerr->message); |
|
142 g_error_free (gerr); |
|
143 gerr = NULL; |
|
144 break; |
|
145 default: |
|
146 g_warning ("Abnormal exit from write loop."); |
|
147 g_assert(FALSE && "iochannel-test failed"); |
|
148 break; |
|
149 } |
|
150 |
|
151 do |
|
152 status = g_io_channel_flush (gio_w, &gerr); |
|
153 while (status == G_IO_STATUS_AGAIN); |
|
154 |
|
155 if (status == G_IO_STATUS_ERROR) |
|
156 { |
|
157 g_warning (gerr->message); |
|
158 g_assert(FALSE && "iochannel-test failed"); |
|
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 #if __SYMBIAN32__ |
|
171 testResultXml("iochannel-test"); |
|
172 #endif /* EMULATOR */ |
|
173 |
|
174 return 0; |
|
175 } |