]>
Commit | Line | Data |
---|---|---|
cfb88c55 JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: typetest.cpp | |
3 | // Purpose: Types wxWindows sample | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
8e124873 | 9 | // Licence: wxWindows license |
cfb88c55 JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | #ifdef __GNUG__ | |
13 | #pragma implementation "typetest.h" | |
14 | #endif | |
15 | ||
16 | // For compilers that support precompilation, includes "wx/wx.h". | |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/time.h" | |
28 | #include "wx/date.h" | |
29 | #include "wx/variant.h" | |
8e124873 | 30 | #include "wx/mimetype.h" |
cfb88c55 JS |
31 | |
32 | #include "typetest.h" | |
33 | ||
b412f9be | 34 | #if defined(__WXGTK__) || defined(__WXMOTIF__) |
cfb88c55 JS |
35 | #include "mondrian.xpm" |
36 | #endif | |
37 | ||
38830220 RR |
38 | #include "wx/ioswrap.h" |
39 | ||
40 | #if wxUSE_IOSTREAMH | |
41 | #include <fstream.h> | |
42 | #else | |
43 | #include <fstream> | |
44 | #endif | |
45 | ||
46 | #include "wx/wfstream.h" | |
53daeada | 47 | #include "wx/datstrm.h" |
fae05df5 | 48 | #include "wx/txtstrm.h" |
f6bcfd97 | 49 | #include "wx/mstream.h" |
38830220 | 50 | |
cfb88c55 | 51 | // Create a new application object |
8e124873 | 52 | IMPLEMENT_APP (MyApp) |
cfb88c55 | 53 | |
8e124873 | 54 | IMPLEMENT_DYNAMIC_CLASS (MyApp, wxApp) |
cfb88c55 JS |
55 | |
56 | BEGIN_EVENT_TABLE(MyApp, wxApp) | |
8e124873 VZ |
57 | EVT_MENU(TYPES_DATE, MyApp::DoDateDemo) |
58 | EVT_MENU(TYPES_TIME, MyApp::DoTimeDemo) | |
59 | EVT_MENU(TYPES_VARIANT, MyApp::DoVariantDemo) | |
60 | EVT_MENU(TYPES_BYTEORDER, MyApp::DoByteOrderDemo) | |
dcf924a3 | 61 | #if wxUSE_UNICODE |
8e124873 | 62 | EVT_MENU(TYPES_UNICODE, MyApp::DoUnicodeDemo) |
dcf924a3 | 63 | #endif |
8e124873 | 64 | EVT_MENU(TYPES_STREAM, MyApp::DoStreamDemo) |
aa51b2e1 RR |
65 | EVT_MENU(TYPES_STREAM2, MyApp::DoStreamDemo2) |
66 | EVT_MENU(TYPES_STREAM3, MyApp::DoStreamDemo3) | |
2bf8e4eb | 67 | EVT_MENU(TYPES_STREAM4, MyApp::DoStreamDemo4) |
f6bcfd97 | 68 | EVT_MENU(TYPES_STREAM5, MyApp::DoStreamDemo5) |
8e124873 | 69 | EVT_MENU(TYPES_MIME, MyApp::DoMIMEDemo) |
cfb88c55 JS |
70 | END_EVENT_TABLE() |
71 | ||
8e124873 | 72 | bool MyApp::OnInit() |
cfb88c55 | 73 | { |
8e124873 VZ |
74 | // Create the main frame window |
75 | MyFrame *frame = new MyFrame((wxFrame *) NULL, "wxWindows Types Demo", | |
76 | wxPoint(50, 50), wxSize(450, 340)); | |
77 | ||
78 | // Give it an icon | |
79 | frame->SetIcon(wxICON(mondrian)); | |
80 | ||
81 | // Make a menubar | |
82 | wxMenu *file_menu = new wxMenu; | |
83 | ||
84 | file_menu->Append(TYPES_ABOUT, "&About"); | |
85 | file_menu->AppendSeparator(); | |
86 | file_menu->Append(TYPES_QUIT, "E&xit\tAlt-X"); | |
87 | ||
88 | wxMenu *test_menu = new wxMenu; | |
89 | test_menu->Append(TYPES_DATE, "&Date test"); | |
90 | test_menu->Append(TYPES_TIME, "&Time test"); | |
91 | test_menu->Append(TYPES_VARIANT, "&Variant test"); | |
92 | test_menu->Append(TYPES_BYTEORDER, "&Byteorder test"); | |
dcf924a3 | 93 | #if wxUSE_UNICODE |
8e124873 | 94 | test_menu->Append(TYPES_UNICODE, "&Unicode test"); |
dcf924a3 | 95 | #endif |
8e124873 | 96 | test_menu->Append(TYPES_STREAM, "&Stream test"); |
1b055864 | 97 | test_menu->Append(TYPES_STREAM2, "&Stream seek test"); |
aa51b2e1 | 98 | test_menu->Append(TYPES_STREAM3, "&Stream error test"); |
2bf8e4eb | 99 | test_menu->Append(TYPES_STREAM4, "&Stream buffer test"); |
f6bcfd97 | 100 | test_menu->Append(TYPES_STREAM5, "&Stream peek test"); |
8e124873 VZ |
101 | test_menu->AppendSeparator(); |
102 | test_menu->Append(TYPES_MIME, "&MIME database test"); | |
cfb88c55 | 103 | |
8e124873 VZ |
104 | wxMenuBar *menu_bar = new wxMenuBar; |
105 | menu_bar->Append(file_menu, "&File"); | |
106 | menu_bar->Append(test_menu, "&Tests"); | |
107 | frame->SetMenuBar(menu_bar); | |
cfb88c55 | 108 | |
8e124873 | 109 | m_textCtrl = new wxTextCtrl(frame, -1, "", wxPoint(0, 0), wxDefaultSize, wxTE_MULTILINE); |
cfb88c55 | 110 | |
8e124873 VZ |
111 | // Show the frame |
112 | frame->Show(TRUE); | |
113 | ||
114 | SetTopWindow(frame); | |
115 | ||
116 | return TRUE; | |
cfb88c55 JS |
117 | } |
118 | ||
38830220 RR |
119 | void MyApp::DoStreamDemo(wxCommandEvent& WXUNUSED(event)) |
120 | { | |
121 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
8e124873 | 122 | |
38830220 | 123 | textCtrl.Clear(); |
c980c992 | 124 | textCtrl << _T("\nTest fstream vs. wxFileStream:\n\n"); |
38830220 | 125 | |
e57e26dd | 126 | textCtrl.WriteText( "Writing to ofstream and wxFileOutputStream:\n" ); |
8e124873 | 127 | |
38830220 | 128 | ofstream std_file_output( "test_std.dat" ); |
56b79cb9 | 129 | wxFileOutputStream file_output( wxString("test_wx.dat") ); |
fae05df5 GL |
130 | wxBufferedOutputStream buf_output( file_output ); |
131 | wxTextOutputStream text_output( buf_output ); | |
38830220 | 132 | |
38830220 RR |
133 | wxString tmp; |
134 | signed int si = 0xFFFFFFFF; | |
c980c992 | 135 | tmp.Printf( _T("Signed int: %d\n"), si ); |
38830220 | 136 | textCtrl.WriteText( tmp ); |
fae05df5 | 137 | text_output << si << "\n"; |
38830220 | 138 | std_file_output << si << "\n"; |
8e124873 | 139 | |
38830220 | 140 | unsigned int ui = 0xFFFFFFFF; |
c980c992 | 141 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
38830220 | 142 | textCtrl.WriteText( tmp ); |
fae05df5 | 143 | text_output << ui << "\n"; |
38830220 | 144 | std_file_output << ui << "\n"; |
8e124873 | 145 | |
38830220 | 146 | double d = 2.01234567890123456789; |
c980c992 | 147 | tmp.Printf( _T("Double: %f\n"), d ); |
38830220 | 148 | textCtrl.WriteText( tmp ); |
fae05df5 | 149 | text_output << d << "\n"; |
38830220 | 150 | std_file_output << d << "\n"; |
8e124873 VZ |
151 | |
152 | float f = (float)0.00001; | |
c980c992 | 153 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 154 | textCtrl.WriteText( tmp ); |
fae05df5 | 155 | text_output << f << "\n"; |
e57e26dd | 156 | std_file_output << f << "\n"; |
8e124873 | 157 | |
c980c992 GL |
158 | wxString str( _T("Hello!") ); |
159 | tmp.Printf( _T("String: %s\n"), str.c_str() ); | |
38830220 | 160 | textCtrl.WriteText( tmp ); |
fae05df5 | 161 | text_output << str << "\n"; |
38830220 | 162 | std_file_output << str.c_str() << "\n"; |
f6bcfd97 | 163 | |
e57e26dd | 164 | textCtrl.WriteText( "\nReading from ifstream:\n" ); |
8e124873 | 165 | |
e57e26dd RR |
166 | ifstream std_file_input( "test_std.dat" ); |
167 | ||
168 | std_file_input >> si; | |
c980c992 | 169 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd | 170 | textCtrl.WriteText( tmp ); |
8e124873 | 171 | |
e57e26dd | 172 | std_file_input >> ui; |
c980c992 | 173 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd | 174 | textCtrl.WriteText( tmp ); |
8e124873 | 175 | |
e57e26dd | 176 | std_file_input >> d; |
c980c992 | 177 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd | 178 | textCtrl.WriteText( tmp ); |
8e124873 | 179 | |
e57e26dd | 180 | std_file_input >> f; |
c980c992 | 181 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 182 | textCtrl.WriteText( tmp ); |
8e124873 | 183 | |
f6bcfd97 | 184 | // Why doesn't this work? |
69d16e3e | 185 | #if 0 |
f6bcfd97 BP |
186 | char std_buf[200]; |
187 | std_file_input >> std_buf; | |
188 | tmp.Printf( _T("String: %s\n"), std_buf ); | |
e57e26dd | 189 | textCtrl.WriteText( tmp ); |
69d16e3e | 190 | #endif |
8e124873 | 191 | |
e57e26dd | 192 | textCtrl.WriteText( "\nReading from wxFileInputStream:\n" ); |
b6bff301 | 193 | |
fae05df5 | 194 | buf_output.Sync(); |
8e124873 | 195 | |
56b79cb9 | 196 | wxFileInputStream file_input( wxString("test_wx.dat") ); |
fae05df5 | 197 | wxBufferedInputStream buf_input( file_input ); |
78e848ca | 198 | wxTextInputStream text_input( file_input ); |
8e124873 | 199 | |
fae05df5 | 200 | text_input >> si; |
c980c992 | 201 | tmp.Printf( _T("Signed int: %d\n"), si ); |
e57e26dd | 202 | textCtrl.WriteText( tmp ); |
8e124873 | 203 | |
fae05df5 | 204 | text_input >> ui; |
c980c992 | 205 | tmp.Printf( _T("Unsigned int: %u\n"), ui ); |
e57e26dd | 206 | textCtrl.WriteText( tmp ); |
8e124873 | 207 | |
fae05df5 | 208 | text_input >> d; |
c980c992 | 209 | tmp.Printf( _T("Double: %f\n"), d ); |
e57e26dd | 210 | textCtrl.WriteText( tmp ); |
8e124873 | 211 | |
fae05df5 | 212 | text_input >> f; |
c980c992 | 213 | tmp.Printf( _T("Float: %f\n"), f ); |
e57e26dd | 214 | textCtrl.WriteText( tmp ); |
8e124873 | 215 | |
fae05df5 | 216 | text_input >> str; |
c980c992 | 217 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
e57e26dd | 218 | textCtrl.WriteText( tmp ); |
8e124873 | 219 | |
53daeada | 220 | |
f6bcfd97 | 221 | |
53daeada RR |
222 | textCtrl << "\nTest for wxDataStream:\n\n"; |
223 | ||
224 | textCtrl.WriteText( "Writing to wxDataOutputStream:\n" ); | |
8e124873 | 225 | |
53daeada | 226 | file_output.SeekO( 0 ); |
fae05df5 | 227 | wxDataOutputStream data_output( buf_output ); |
53daeada | 228 | |
8e124873 | 229 | wxInt16 i16 = (short)0xFFFF; |
c980c992 | 230 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 231 | textCtrl.WriteText( tmp ); |
329e86bf | 232 | data_output.Write16( i16 ); |
8e124873 | 233 | |
329e86bf | 234 | wxUint16 ui16 = 0xFFFF; |
c980c992 | 235 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada | 236 | textCtrl.WriteText( tmp ); |
329e86bf | 237 | data_output.Write16( ui16 ); |
8e124873 | 238 | |
53daeada | 239 | d = 2.01234567890123456789; |
c980c992 | 240 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada RR |
241 | textCtrl.WriteText( tmp ); |
242 | data_output.WriteDouble( d ); | |
8e124873 | 243 | |
53daeada | 244 | str = "Hello!"; |
c980c992 | 245 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada RR |
246 | textCtrl.WriteText( tmp ); |
247 | data_output.WriteString( str ); | |
8e124873 | 248 | |
fae05df5 | 249 | buf_output.Sync(); |
8e124873 | 250 | |
53daeada | 251 | textCtrl.WriteText( "\nReading from wxDataInputStream:\n" ); |
8e124873 | 252 | |
53daeada | 253 | file_input.SeekI( 0 ); |
fae05df5 | 254 | wxDataInputStream data_input( buf_input ); |
53daeada | 255 | |
329e86bf | 256 | i16 = data_input.Read16(); |
c980c992 | 257 | tmp.Printf( _T("Signed int16: %d\n"), (int)i16 ); |
53daeada | 258 | textCtrl.WriteText( tmp ); |
8e124873 | 259 | |
329e86bf | 260 | ui16 = data_input.Read16(); |
c980c992 | 261 | tmp.Printf( _T("Unsigned int16: %u\n"), (unsigned int) ui16 ); |
53daeada RR |
262 | textCtrl.WriteText( tmp ); |
263 | ||
264 | d = data_input.ReadDouble(); | |
c980c992 | 265 | tmp.Printf( _T("Double: %f\n"), d ); |
53daeada | 266 | textCtrl.WriteText( tmp ); |
8e124873 | 267 | |
53daeada | 268 | str = data_input.ReadString(); |
c980c992 | 269 | tmp.Printf( _T("String: %s\n"), str.c_str() ); |
53daeada | 270 | textCtrl.WriteText( tmp ); |
38830220 RR |
271 | } |
272 | ||
1b055864 RR |
273 | void MyApp::DoStreamDemo2(wxCommandEvent& WXUNUSED(event)) |
274 | { | |
275 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
276 | ||
277 | textCtrl.Clear(); | |
aa51b2e1 | 278 | textCtrl << _T("\nTesting wxBufferedStream:\n\n"); |
1b055864 RR |
279 | |
280 | char ch,ch2; | |
281 | ||
282 | textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream:\n\n" ); | |
283 | ||
56b79cb9 | 284 | wxFileOutputStream file_output( wxString("test_wx.dat") ); |
1b055864 RR |
285 | wxBufferedOutputStream buf_output( file_output ); |
286 | for (ch = 0; ch < 10; ch++) | |
287 | buf_output.Write( &ch, 1 ); | |
288 | buf_output.Sync(); | |
289 | ||
56b79cb9 | 290 | wxFileInputStream file_input( wxString("test_wx.dat") ); |
1b055864 RR |
291 | for (ch2 = 0; ch2 < 10; ch2++) |
292 | { | |
293 | file_input.Read( &ch, 1 ); | |
f6bcfd97 | 294 | textCtrl.WriteText( (char)(ch + '0') ); |
1b055864 RR |
295 | } |
296 | textCtrl.WriteText( "\n\n\n" ); | |
297 | ||
298 | textCtrl.WriteText( "Writing number 0 to 9 to buffered wxFileOutputStream, then\n" ); | |
299 | textCtrl.WriteText( "seeking back to #3 and writing 3:\n\n" ); | |
300 | ||
56b79cb9 | 301 | wxFileOutputStream file_output2( wxString("test_wx2.dat") ); |
1b055864 RR |
302 | wxBufferedOutputStream buf_output2( file_output2 ); |
303 | for (ch = 0; ch < 10; ch++) | |
304 | buf_output2.Write( &ch, 1 ); | |
305 | buf_output2.SeekO( 3 ); | |
306 | ch = 3; | |
307 | buf_output2.Write( &ch, 1 ); | |
308 | buf_output2.Sync(); | |
309 | ||
56b79cb9 | 310 | wxFileInputStream file_input2( wxString("test_wx2.dat") ); |
1b055864 RR |
311 | for (ch2 = 0; ch2 < 10; ch2++) |
312 | { | |
313 | file_input2.Read( &ch, 1 ); | |
f6bcfd97 | 314 | textCtrl.WriteText( (char)(ch + '0') ); |
1b055864 RR |
315 | } |
316 | textCtrl.WriteText( "\n\n\n" ); | |
317 | ||
318 | // now append 2000 bytes to file (bigger than buffer) | |
319 | buf_output2.SeekO( 0, wxFromEnd ); | |
320 | ch = 1; | |
321 | for (int i = 0; i < 2000; i++) | |
322 | buf_output2.Write( &ch, 1 ); | |
323 | buf_output2.Sync(); | |
324 | ||
325 | textCtrl.WriteText( "Reading number 0 to 9 from buffered wxFileInputStream, then\n" ); | |
326 | textCtrl.WriteText( "seeking back to #3 and reading 3:\n\n" ); | |
327 | ||
56b79cb9 | 328 | wxFileInputStream file_input3( wxString("test_wx2.dat") ); |
1b055864 RR |
329 | wxBufferedInputStream buf_input3( file_input3 ); |
330 | for (ch2 = 0; ch2 < 10; ch2++) | |
331 | { | |
332 | buf_input3.Read( &ch, 1 ); | |
f6bcfd97 | 333 | textCtrl.WriteText( (char)(ch + '0') ); |
1b055864 RR |
334 | } |
335 | for (int j = 0; j < 2000; j++) | |
336 | buf_input3.Read( &ch, 1 ); | |
337 | textCtrl.WriteText( "\n" ); | |
338 | buf_input3.SeekI( 3 ); | |
339 | buf_input3.Read( &ch, 1 ); | |
340 | textCtrl.WriteText( (char)(ch + '0') ); | |
341 | textCtrl.WriteText( "\n\n\n" ); | |
342 | ||
343 | } | |
344 | ||
aa51b2e1 RR |
345 | void MyApp::DoStreamDemo3(wxCommandEvent& WXUNUSED(event)) |
346 | { | |
347 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
348 | ||
349 | textCtrl.Clear(); | |
350 | textCtrl << "\nTesting wxFileInputStream's and wxFFileInputStream's error handling:\n\n"; | |
351 | ||
352 | char ch,ch2; | |
353 | ||
354 | textCtrl.WriteText( "Writing number 0 to 9 to wxFileOutputStream:\n\n" ); | |
355 | ||
56b79cb9 | 356 | wxFileOutputStream file_output( wxString("test_wx.dat") ); |
aa51b2e1 RR |
357 | for (ch = 0; ch < 10; ch++) |
358 | file_output.Write( &ch, 1 ); | |
359 | ||
842d6c94 | 360 | // Testing wxFileInputStream |
aa51b2e1 RR |
361 | |
362 | textCtrl.WriteText( "Reading 0 to 10 to wxFileInputStream:\n\n" ); | |
363 | ||
56b79cb9 | 364 | wxFileInputStream file_input( wxString("test_wx.dat") ); |
aa51b2e1 RR |
365 | for (ch2 = 0; ch2 < 11; ch2++) |
366 | { | |
367 | file_input.Read( &ch, 1 ); | |
f6bcfd97 BP |
368 | textCtrl.WriteText( "Value read: " ); |
369 | textCtrl.WriteText( (char)(ch + '0') ); | |
370 | textCtrl.WriteText( "; stream.LastError() returns: " ); | |
371 | switch (file_input.LastError()) | |
372 | { | |
373 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
374 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
375 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
376 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
377 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
378 | } | |
aa51b2e1 RR |
379 | } |
380 | textCtrl.WriteText( "\n" ); | |
381 | ||
382 | textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " ); | |
383 | file_input.SeekI( 0 ); | |
384 | switch (file_input.LastError()) | |
385 | { | |
f6bcfd97 BP |
386 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; |
387 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
388 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
389 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
390 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
aa51b2e1 RR |
391 | } |
392 | textCtrl.WriteText( "\n" ); | |
393 | ||
394 | file_input.Read( &ch, 1 ); | |
395 | textCtrl.WriteText( "Value read: " ); | |
396 | textCtrl.WriteText( (char)(ch + '0') ); | |
397 | textCtrl.WriteText( "; stream.LastError() returns: " ); | |
398 | switch (file_input.LastError()) | |
399 | { | |
f6bcfd97 BP |
400 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; |
401 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
402 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
403 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
404 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
aa51b2e1 RR |
405 | } |
406 | textCtrl.WriteText( "\n\n" ); | |
407 | ||
408 | ||
842d6c94 | 409 | // Testing wxFFileInputStream |
aa51b2e1 RR |
410 | |
411 | textCtrl.WriteText( "Reading 0 to 10 to wxFFileInputStream:\n\n" ); | |
412 | ||
56b79cb9 | 413 | wxFFileInputStream ffile_input( wxString("test_wx.dat") ); |
aa51b2e1 RR |
414 | for (ch2 = 0; ch2 < 11; ch2++) |
415 | { | |
416 | ffile_input.Read( &ch, 1 ); | |
f6bcfd97 BP |
417 | textCtrl.WriteText( "Value read: " ); |
418 | textCtrl.WriteText( (char)(ch + '0') ); | |
419 | textCtrl.WriteText( "; stream.LastError() returns: " ); | |
420 | switch (ffile_input.LastError()) | |
421 | { | |
422 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
423 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
424 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
425 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
426 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
427 | } | |
aa51b2e1 RR |
428 | } |
429 | textCtrl.WriteText( "\n" ); | |
430 | ||
431 | textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " ); | |
432 | ffile_input.SeekI( 0 ); | |
433 | switch (ffile_input.LastError()) | |
434 | { | |
f6bcfd97 BP |
435 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; |
436 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
437 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
438 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
439 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
aa51b2e1 RR |
440 | } |
441 | textCtrl.WriteText( "\n" ); | |
442 | ||
443 | ffile_input.Read( &ch, 1 ); | |
444 | textCtrl.WriteText( "Value read: " ); | |
445 | textCtrl.WriteText( (char)(ch + '0') ); | |
446 | textCtrl.WriteText( "; stream.LastError() returns: " ); | |
447 | switch (ffile_input.LastError()) | |
448 | { | |
f6bcfd97 BP |
449 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; |
450 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
451 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
452 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
453 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
842d6c94 RR |
454 | } |
455 | textCtrl.WriteText( "\n\n" ); | |
456 | ||
457 | // Testing wxFFileInputStream | |
458 | ||
459 | textCtrl.WriteText( "Reading 0 to 10 to buffered wxFFileInputStream:\n\n" ); | |
460 | ||
56b79cb9 | 461 | wxFFileInputStream ffile_input2( wxString("test_wx.dat") ); |
842d6c94 RR |
462 | wxBufferedInputStream buf_input( ffile_input2 ); |
463 | for (ch2 = 0; ch2 < 11; ch2++) | |
464 | { | |
465 | buf_input.Read( &ch, 1 ); | |
f6bcfd97 BP |
466 | textCtrl.WriteText( "Value read: " ); |
467 | textCtrl.WriteText( (char)(ch + '0') ); | |
468 | textCtrl.WriteText( "; stream.LastError() returns: " ); | |
469 | switch (buf_input.LastError()) | |
470 | { | |
471 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
472 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
473 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
474 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
475 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
476 | } | |
842d6c94 RR |
477 | } |
478 | textCtrl.WriteText( "\n" ); | |
479 | ||
480 | textCtrl.WriteText( "Seeking to 0; stream.LastError() returns: " ); | |
481 | buf_input.SeekI( 0 ); | |
482 | switch (buf_input.LastError()) | |
483 | { | |
f6bcfd97 BP |
484 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; |
485 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
486 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
487 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
488 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
842d6c94 RR |
489 | } |
490 | textCtrl.WriteText( "\n" ); | |
491 | ||
492 | buf_input.Read( &ch, 1 ); | |
493 | textCtrl.WriteText( "Value read: " ); | |
494 | textCtrl.WriteText( (char)(ch + '0') ); | |
495 | textCtrl.WriteText( "; stream.LastError() returns: " ); | |
496 | switch (buf_input.LastError()) | |
497 | { | |
f6bcfd97 BP |
498 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; |
499 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; | |
500 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
501 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
502 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
aa51b2e1 | 503 | } |
2bf8e4eb RR |
504 | } |
505 | ||
506 | void MyApp::DoStreamDemo4(wxCommandEvent& WXUNUSED(event)) | |
507 | { | |
508 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
509 | ||
510 | wxString msg; | |
511 | ||
512 | textCtrl.Clear(); | |
513 | textCtrl << "\nTesting wxStreamBuffer:\n\n"; | |
514 | ||
515 | // bigger than buffer | |
516 | textCtrl.WriteText( "Writing 2000x 1 to wxFileOutputStream.\n\n" ); | |
517 | ||
56b79cb9 | 518 | wxFileOutputStream file_output( wxString("test_wx.dat") ); |
2bf8e4eb RR |
519 | for (int i = 0; i < 2000; i++) |
520 | { | |
521 | char ch = 1; | |
522 | file_output.Write( &ch, 1 ); | |
523 | } | |
524 | ||
525 | textCtrl.WriteText( "Opening with a buffered wxFileInputStream:\n\n" ); | |
526 | ||
56b79cb9 | 527 | wxFileInputStream file_input( wxString("test_wx.dat") ); |
2bf8e4eb RR |
528 | wxBufferedInputStream buf_input( file_input ); |
529 | ||
530 | textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " ); | |
531 | switch (buf_input.LastError()) | |
532 | { | |
533 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
f6bcfd97 BP |
534 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; |
535 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
536 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
537 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
2bf8e4eb RR |
538 | } |
539 | msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() ); | |
540 | textCtrl.WriteText( msg ); | |
541 | msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() ); | |
542 | textCtrl.WriteText( msg ); | |
543 | textCtrl.WriteText( "\n\n" ); | |
544 | ||
545 | ||
546 | textCtrl.WriteText( "Seeking to position 300:\n\n" ); | |
547 | ||
548 | buf_input.SeekI( 300 ); | |
549 | ||
550 | textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " ); | |
551 | switch (buf_input.LastError()) | |
552 | { | |
553 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
f6bcfd97 BP |
554 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; |
555 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
556 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
557 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
2bf8e4eb RR |
558 | } |
559 | msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() ); | |
560 | textCtrl.WriteText( msg ); | |
561 | msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() ); | |
562 | textCtrl.WriteText( msg ); | |
563 | textCtrl.WriteText( "\n\n" ); | |
564 | ||
565 | ||
566 | char buf[2000]; | |
567 | ||
568 | textCtrl.WriteText( "Reading 500 bytes:\n\n" ); | |
aa51b2e1 | 569 | |
2bf8e4eb RR |
570 | buf_input.Read( buf, 500 ); |
571 | ||
572 | textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " ); | |
573 | switch (buf_input.LastError()) | |
574 | { | |
575 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
f6bcfd97 BP |
576 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; |
577 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
578 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
579 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
2bf8e4eb RR |
580 | } |
581 | msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() ); | |
582 | textCtrl.WriteText( msg ); | |
583 | msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() ); | |
584 | textCtrl.WriteText( msg ); | |
585 | textCtrl.WriteText( "\n\n" ); | |
586 | ||
587 | textCtrl.WriteText( "Reading another 500 bytes:\n\n" ); | |
588 | ||
589 | buf_input.Read( buf, 500 ); | |
590 | ||
591 | textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " ); | |
592 | switch (buf_input.LastError()) | |
593 | { | |
594 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
f6bcfd97 BP |
595 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; |
596 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
597 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
598 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
2bf8e4eb RR |
599 | } |
600 | msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() ); | |
601 | textCtrl.WriteText( msg ); | |
602 | msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() ); | |
603 | textCtrl.WriteText( msg ); | |
604 | textCtrl.WriteText( "\n\n" ); | |
605 | ||
606 | textCtrl.WriteText( "Reading another 500 bytes:\n\n" ); | |
607 | ||
608 | buf_input.Read( buf, 500 ); | |
609 | ||
610 | textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " ); | |
611 | switch (buf_input.LastError()) | |
612 | { | |
613 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
f6bcfd97 BP |
614 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; |
615 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
616 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
617 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
2bf8e4eb RR |
618 | } |
619 | msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() ); | |
620 | textCtrl.WriteText( msg ); | |
621 | msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() ); | |
622 | textCtrl.WriteText( msg ); | |
623 | textCtrl.WriteText( "\n\n" ); | |
624 | ||
625 | textCtrl.WriteText( "Reading another 500 bytes:\n\n" ); | |
626 | ||
627 | buf_input.Read( buf, 500 ); | |
628 | ||
629 | textCtrl.WriteText( "wxBufferedInputStream.LastError() returns: " ); | |
630 | switch (buf_input.LastError()) | |
631 | { | |
632 | case wxSTREAM_NOERROR: textCtrl.WriteText( "wxSTREAM_NOERROR\n" ); break; | |
f6bcfd97 BP |
633 | case wxSTREAM_EOF: textCtrl.WriteText( "wxSTREAM_EOF\n" ); break; |
634 | case wxSTREAM_READ_ERROR: textCtrl.WriteText( "wxSTREAM_READ_ERROR\n" ); break; | |
635 | case wxSTREAM_WRITE_ERROR: textCtrl.WriteText( "wxSTREAM_WRITE_ERROR\n" ); break; | |
636 | default: textCtrl.WriteText( "Huh?\n" ); break; | |
2bf8e4eb RR |
637 | } |
638 | msg.Printf( "wxBufferedInputStream.LastRead() returns: %d\n", (int)buf_input.LastRead() ); | |
639 | textCtrl.WriteText( msg ); | |
640 | msg.Printf( "wxBufferedInputStream.TellI() returns: %d\n", (int)buf_input.TellI() ); | |
641 | textCtrl.WriteText( msg ); | |
642 | textCtrl.WriteText( "\n\n" ); | |
aa51b2e1 RR |
643 | } |
644 | ||
f6bcfd97 BP |
645 | void MyApp::DoStreamDemo5(wxCommandEvent& WXUNUSED(event)) |
646 | { | |
647 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
648 | ||
649 | textCtrl.Clear(); | |
650 | textCtrl << "\nTesting wxFileInputStream's Peek():\n\n"; | |
651 | ||
652 | char ch; | |
653 | wxString str; | |
654 | ||
655 | textCtrl.WriteText( "Writing number 0 to 9 to wxFileOutputStream:\n\n" ); | |
656 | ||
657 | wxFileOutputStream file_output( wxString("test_wx.dat") ); | |
658 | for (ch = 0; ch < 10; ch++) | |
659 | file_output.Write( &ch, 1 ); | |
660 | ||
661 | file_output.Sync(); | |
662 | ||
663 | wxFileInputStream file_input( wxString("test_wx.dat") ); | |
664 | ||
665 | ch = file_input.Peek(); | |
666 | str.Printf( "First char peeked: %d\n", (int) ch ); | |
667 | textCtrl.WriteText( str ); | |
668 | ||
669 | ch = file_input.GetC(); | |
670 | str.Printf( "First char read: %d\n", (int) ch ); | |
671 | textCtrl.WriteText( str ); | |
672 | ||
673 | ch = file_input.Peek(); | |
674 | str.Printf( "Second char peeked: %d\n", (int) ch ); | |
675 | textCtrl.WriteText( str ); | |
676 | ||
677 | ch = file_input.GetC(); | |
678 | str.Printf( "Second char read: %d\n", (int) ch ); | |
679 | textCtrl.WriteText( str ); | |
680 | ||
681 | ch = file_input.Peek(); | |
682 | str.Printf( "Third char peeked: %d\n", (int) ch ); | |
683 | textCtrl.WriteText( str ); | |
684 | ||
685 | ch = file_input.GetC(); | |
686 | str.Printf( "Third char read: %d\n", (int) ch ); | |
687 | textCtrl.WriteText( str ); | |
688 | ||
689 | ||
690 | textCtrl << "\n\n\nTesting wxMemoryInputStream's Peek():\n\n"; | |
691 | ||
692 | char buf[] = { 0,1,2,3,4,5,6,7,8,9,10 }; | |
693 | wxMemoryInputStream input( buf, 10 ); | |
694 | ||
695 | ch = input.Peek(); | |
696 | str.Printf( "First char peeked: %d\n", (int) ch ); | |
697 | textCtrl.WriteText( str ); | |
698 | ||
699 | ch = input.GetC(); | |
700 | str.Printf( "First char read: %d\n", (int) ch ); | |
701 | textCtrl.WriteText( str ); | |
702 | ||
703 | ch = input.Peek(); | |
704 | str.Printf( "Second char peeked: %d\n", (int) ch ); | |
705 | textCtrl.WriteText( str ); | |
706 | ||
707 | ch = input.GetC(); | |
708 | str.Printf( "Second char read: %d\n", (int) ch ); | |
709 | textCtrl.WriteText( str ); | |
710 | ||
711 | ch = input.Peek(); | |
712 | str.Printf( "Third char peeked: %d\n", (int) ch ); | |
713 | textCtrl.WriteText( str ); | |
714 | ||
715 | ch = input.GetC(); | |
716 | str.Printf( "Third char read: %d\n", (int) ch ); | |
717 | textCtrl.WriteText( str ); | |
718 | } | |
719 | ||
dcf924a3 RR |
720 | #if wxUSE_UNICODE |
721 | void MyApp::DoUnicodeDemo(wxCommandEvent& WXUNUSED(event)) | |
722 | { | |
723 | wxTextCtrl& textCtrl = * GetTextCtrl(); | |
8e124873 | 724 | |
dcf924a3 RR |
725 | textCtrl.Clear(); |
726 | textCtrl << "\nTest wchar_t to char (Unicode to ANSI/Multibyte) converions:"; | |
727 | ||
728 | wxString str; | |
729 |