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