remove TestTimer() (adds nothing to existing tests) and move wxStopWatch tests to...
[wxWidgets.git] / samples / console / console.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 04.10.99
7 // RCS-ID: $Id$
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // IMPORTANT NOTE FOR WXWIDGETS USERS:
13 // If you're a wxWidgets user and you're looking at this file to learn how to
14 // structure a wxWidgets console application, then you don't have much to learn.
15 // This application is used more for testing rather than as sample but
16 // basically the following simple block is enough for you to start your
17 // own console application:
18
19 /*
20 int main(int argc, char **argv)
21 {
22 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
23
24 wxInitializer initializer;
25 if ( !initializer )
26 {
27 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
28 return -1;
29 }
30
31 static const wxCmdLineEntryDesc cmdLineDesc[] =
32 {
33 { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
34 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
35 // ... your other command line options here...
36
37 { wxCMD_LINE_NONE }
38 };
39
40 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
41 switch ( parser.Parse() )
42 {
43 case -1:
44 wxLogMessage(wxT("Help was given, terminating."));
45 break;
46
47 case 0:
48 // everything is ok; proceed
49 break;
50
51 default:
52 wxLogMessage(wxT("Syntax error detected, aborting."));
53 break;
54 }
55
56 // do something useful here
57
58 return 0;
59 }
60 */
61
62
63 // ============================================================================
64 // declarations
65 // ============================================================================
66
67 // ----------------------------------------------------------------------------
68 // headers
69 // ----------------------------------------------------------------------------
70
71 #include "wx/defs.h"
72
73 #include <stdio.h>
74
75 #include "wx/string.h"
76 #include "wx/file.h"
77 #include "wx/filename.h"
78 #include "wx/app.h"
79 #include "wx/log.h"
80 #include "wx/apptrait.h"
81 #include "wx/platinfo.h"
82 #include "wx/wxchar.h"
83
84 // without this pragma, the stupid compiler precompiles #defines below so that
85 // changing them doesn't "take place" later!
86 #ifdef __VISUALC__
87 #pragma hdrstop
88 #endif
89
90 // ----------------------------------------------------------------------------
91 // conditional compilation
92 // ----------------------------------------------------------------------------
93
94 /*
95 A note about all these conditional compilation macros: this file is used
96 both as a test suite for various non-GUI wxWidgets classes and as a
97 scratchpad for quick tests. So there are two compilation modes: if you
98 define TEST_ALL all tests are run, otherwise you may enable the individual
99 tests individually in the "#else" branch below.
100 */
101
102 // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single
103 // test, define it to 1 to do all tests.
104 #define TEST_ALL 0
105
106
107 #if TEST_ALL
108 #define TEST_CMDLINE
109 #define TEST_DATETIME
110 #define TEST_DIR
111 #define TEST_DYNLIB
112 #define TEST_ENVIRON
113 #define TEST_FILE
114 #define TEST_FILECONF
115 #define TEST_FILENAME
116 #define TEST_FILETIME
117 #define TEST_FTP
118 #define TEST_INFO_FUNCTIONS
119 #define TEST_LOCALE
120 #define TEST_LOG
121 #define TEST_MIME
122 #define TEST_MODULE
123 #define TEST_PATHLIST
124 #define TEST_PRINTF
125 #define TEST_REGCONF
126 #define TEST_REGEX
127 #define TEST_REGISTRY
128 #define TEST_SCOPEGUARD
129 #define TEST_SNGLINST
130 // #define TEST_SOCKETS --FIXME! (RN)
131 #define TEST_STACKWALKER
132 #define TEST_STDPATHS
133 #define TEST_STREAMS
134 #else // #if TEST_ALL
135 #define TEST_DATETIME
136 #define TEST_VOLUME
137 #endif
138
139 // some tests are interactive, define this to run them
140 #ifdef TEST_INTERACTIVE
141 #undef TEST_INTERACTIVE
142
143 #define TEST_INTERACTIVE 1
144 #else
145 #define TEST_INTERACTIVE 1
146 #endif
147
148 // ============================================================================
149 // implementation
150 // ============================================================================
151
152 // ----------------------------------------------------------------------------
153 // helper functions
154 // ----------------------------------------------------------------------------
155
156 #if defined(TEST_SOCKETS)
157
158 // replace TABs with \t and CRs with \n
159 static wxString MakePrintable(const wxChar *s)
160 {
161 wxString str(s);
162 (void)str.Replace(wxT("\t"), wxT("\\t"));
163 (void)str.Replace(wxT("\n"), wxT("\\n"));
164 (void)str.Replace(wxT("\r"), wxT("\\r"));
165
166 return str;
167 }
168
169 #endif // MakePrintable() is used
170
171 // ----------------------------------------------------------------------------
172 // wxCmdLineParser
173 // ----------------------------------------------------------------------------
174
175 #ifdef TEST_CMDLINE
176
177 #include "wx/cmdline.h"
178 #include "wx/datetime.h"
179
180 #if wxUSE_CMDLINE_PARSER
181
182 static void ShowCmdLine(const wxCmdLineParser& parser)
183 {
184 wxString s = wxT("Command line parsed successfully:\nInput files: ");
185
186 size_t count = parser.GetParamCount();
187 for ( size_t param = 0; param < count; param++ )
188 {
189 s << parser.GetParam(param) << ' ';
190 }
191
192 s << '\n'
193 << wxT("Verbose:\t") << (parser.Found(wxT("v")) ? wxT("yes") : wxT("no")) << '\n'
194 << wxT("Quiet:\t") << (parser.Found(wxT("q")) ? wxT("yes") : wxT("no")) << '\n';
195
196 wxString strVal;
197 long lVal;
198 double dVal;
199 wxDateTime dt;
200 if ( parser.Found(wxT("o"), &strVal) )
201 s << wxT("Output file:\t") << strVal << '\n';
202 if ( parser.Found(wxT("i"), &strVal) )
203 s << wxT("Input dir:\t") << strVal << '\n';
204 if ( parser.Found(wxT("s"), &lVal) )
205 s << wxT("Size:\t") << lVal << '\n';
206 if ( parser.Found(wxT("f"), &dVal) )
207 s << wxT("Double:\t") << dVal << '\n';
208 if ( parser.Found(wxT("d"), &dt) )
209 s << wxT("Date:\t") << dt.FormatISODate() << '\n';
210 if ( parser.Found(wxT("project_name"), &strVal) )
211 s << wxT("Project:\t") << strVal << '\n';
212
213 wxLogMessage(s);
214 }
215
216 #endif // wxUSE_CMDLINE_PARSER
217
218 static void TestCmdLineConvert()
219 {
220 static const wxChar *cmdlines[] =
221 {
222 wxT("arg1 arg2"),
223 wxT("-a \"-bstring 1\" -c\"string 2\" \"string 3\""),
224 wxT("literal \\\" and \"\""),
225 };
226
227 for ( size_t n = 0; n < WXSIZEOF(cmdlines); n++ )
228 {
229 const wxChar *cmdline = cmdlines[n];
230 wxPrintf(wxT("Parsing: %s\n"), cmdline);
231 wxArrayString args = wxCmdLineParser::ConvertStringToArgs(cmdline);
232
233 size_t count = args.GetCount();
234 wxPrintf(wxT("\targc = %u\n"), count);
235 for ( size_t arg = 0; arg < count; arg++ )
236 {
237 wxPrintf(wxT("\targv[%u] = %s\n"), arg, args[arg].c_str());
238 }
239 }
240 }
241
242 #endif // TEST_CMDLINE
243
244 // ----------------------------------------------------------------------------
245 // wxDir
246 // ----------------------------------------------------------------------------
247
248 #ifdef TEST_DIR
249
250 #include "wx/dir.h"
251
252 #ifdef __UNIX__
253 static const wxChar *ROOTDIR = wxT("/");
254 static const wxChar *TESTDIR = wxT("/usr/local/share");
255 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
256 static const wxChar *ROOTDIR = wxT("c:\\");
257 static const wxChar *TESTDIR = wxT("d:\\");
258 #else
259 #error "don't know where the root directory is"
260 #endif
261
262 static void TestDirEnumHelper(wxDir& dir,
263 int flags = wxDIR_DEFAULT,
264 const wxString& filespec = wxEmptyString)
265 {
266 wxString filename;
267
268 if ( !dir.IsOpened() )
269 return;
270
271 bool cont = dir.GetFirst(&filename, filespec, flags);
272 while ( cont )
273 {
274 wxPrintf(wxT("\t%s\n"), filename.c_str());
275
276 cont = dir.GetNext(&filename);
277 }
278
279 wxPuts(wxEmptyString);
280 }
281
282 #if TEST_ALL
283
284 static void TestDirEnum()
285 {
286 wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
287
288 wxString cwd = wxGetCwd();
289 if ( !wxDir::Exists(cwd) )
290 {
291 wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
292 return;
293 }
294
295 wxDir dir(cwd);
296 if ( !dir.IsOpened() )
297 {
298 wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
299 return;
300 }
301
302 wxPuts(wxT("Enumerating everything in current directory:"));
303 TestDirEnumHelper(dir);
304
305 wxPuts(wxT("Enumerating really everything in current directory:"));
306 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
307
308 wxPuts(wxT("Enumerating object files in current directory:"));
309 TestDirEnumHelper(dir, wxDIR_DEFAULT, wxT("*.o*"));
310
311 wxPuts(wxT("Enumerating directories in current directory:"));
312 TestDirEnumHelper(dir, wxDIR_DIRS);
313
314 wxPuts(wxT("Enumerating files in current directory:"));
315 TestDirEnumHelper(dir, wxDIR_FILES);
316
317 wxPuts(wxT("Enumerating files including hidden in current directory:"));
318 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
319
320 dir.Open(ROOTDIR);
321
322 wxPuts(wxT("Enumerating everything in root directory:"));
323 TestDirEnumHelper(dir, wxDIR_DEFAULT);
324
325 wxPuts(wxT("Enumerating directories in root directory:"));
326 TestDirEnumHelper(dir, wxDIR_DIRS);
327
328 wxPuts(wxT("Enumerating files in root directory:"));
329 TestDirEnumHelper(dir, wxDIR_FILES);
330
331 wxPuts(wxT("Enumerating files including hidden in root directory:"));
332 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
333
334 wxPuts(wxT("Enumerating files in non existing directory:"));
335 wxDir dirNo(wxT("nosuchdir"));
336 TestDirEnumHelper(dirNo);
337 }
338
339 #endif // TEST_ALL
340
341 class DirPrintTraverser : public wxDirTraverser
342 {
343 public:
344 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
345 {
346 return wxDIR_CONTINUE;
347 }
348
349 virtual wxDirTraverseResult OnDir(const wxString& dirname)
350 {
351 wxString path, name, ext;
352 wxFileName::SplitPath(dirname, &path, &name, &ext);
353
354 if ( !ext.empty() )
355 name << wxT('.') << ext;
356
357 wxString indent;
358 for ( const wxChar *p = path.c_str(); *p; p++ )
359 {
360 if ( wxIsPathSeparator(*p) )
361 indent += wxT(" ");
362 }
363
364 wxPrintf(wxT("%s%s\n"), indent.c_str(), name.c_str());
365
366 return wxDIR_CONTINUE;
367 }
368 };
369
370 static void TestDirTraverse()
371 {
372 wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
373
374 // enum all files
375 wxArrayString files;
376 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
377 wxPrintf(wxT("There are %u files under '%s'\n"), n, TESTDIR);
378 if ( n > 1 )
379 {
380 wxPrintf(wxT("First one is '%s'\n"), files[0u].c_str());
381 wxPrintf(wxT(" last one is '%s'\n"), files[n - 1].c_str());
382 }
383
384 // enum again with custom traverser
385 wxPuts(wxT("Now enumerating directories:"));
386 wxDir dir(TESTDIR);
387 DirPrintTraverser traverser;
388 dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
389 }
390
391 #if TEST_ALL
392
393 static void TestDirExists()
394 {
395 wxPuts(wxT("*** Testing wxDir::Exists() ***"));
396
397 static const wxChar *dirnames[] =
398 {
399 wxT("."),
400 #if defined(__WXMSW__)
401 wxT("c:"),
402 wxT("c:\\"),
403 wxT("\\\\share\\file"),
404 wxT("c:\\dos"),
405 wxT("c:\\dos\\"),
406 wxT("c:\\dos\\\\"),
407 wxT("c:\\autoexec.bat"),
408 #elif defined(__UNIX__)
409 wxT("/"),
410 wxT("//"),
411 wxT("/usr/bin"),
412 wxT("/usr//bin"),
413 wxT("/usr///bin"),
414 #endif
415 };
416
417 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
418 {
419 wxPrintf(wxT("%-40s: %s\n"),
420 dirnames[n],
421 wxDir::Exists(dirnames[n]) ? wxT("exists")
422 : wxT("doesn't exist"));
423 }
424 }
425
426 #endif // TEST_ALL
427
428 #endif // TEST_DIR
429
430 // ----------------------------------------------------------------------------
431 // wxDllLoader
432 // ----------------------------------------------------------------------------
433
434 #ifdef TEST_DYNLIB
435
436 #include "wx/dynlib.h"
437
438 static void TestDllLoad()
439 {
440 #if defined(__WXMSW__)
441 static const wxChar *LIB_NAME = wxT("kernel32.dll");
442 static const wxChar *FUNC_NAME = wxT("lstrlenA");
443 #elif defined(__UNIX__)
444 // weird: using just libc.so does *not* work!
445 static const wxChar *LIB_NAME = wxT("/lib/libc.so.6");
446 static const wxChar *FUNC_NAME = wxT("strlen");
447 #else
448 #error "don't know how to test wxDllLoader on this platform"
449 #endif
450
451 wxPuts(wxT("*** testing basic wxDynamicLibrary functions ***\n"));
452
453 wxDynamicLibrary lib(LIB_NAME);
454 if ( !lib.IsLoaded() )
455 {
456 wxPrintf(wxT("ERROR: failed to load '%s'.\n"), LIB_NAME);
457 }
458 else
459 {
460 typedef int (wxSTDCALL *wxStrlenType)(const char *);
461 wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME);
462 if ( !pfnStrlen )
463 {
464 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
465 FUNC_NAME, LIB_NAME);
466 }
467 else
468 {
469 wxPrintf(wxT("Calling %s dynamically loaded from %s "),
470 FUNC_NAME, LIB_NAME);
471
472 if ( pfnStrlen("foo") != 3 )
473 {
474 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
475 }
476 else
477 {
478 wxPuts(wxT("... ok"));
479 }
480 }
481
482 #ifdef __WXMSW__
483 static const wxChar *FUNC_NAME_AW = wxT("lstrlen");
484
485 typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *);
486 wxStrlenTypeAorW
487 pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW);
488 if ( !pfnStrlenAorW )
489 {
490 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
491 FUNC_NAME_AW, LIB_NAME);
492 }
493 else
494 {
495 if ( pfnStrlenAorW(wxT("foobar")) != 6 )
496 {
497 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
498 }
499 }
500 #endif // __WXMSW__
501 }
502 }
503
504 #if defined(__WXMSW__) || defined(__UNIX__)
505
506 static void TestDllListLoaded()
507 {
508 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
509
510 puts("\nLoaded modules:");
511 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
512 const size_t count = dlls.GetCount();
513 for ( size_t n = 0; n < count; ++n )
514 {
515 const wxDynamicLibraryDetails& details = dlls[n];
516 printf("%-45s", (const char *)details.GetPath().mb_str());
517
518 void *addr wxDUMMY_INITIALIZE(NULL);
519 size_t len wxDUMMY_INITIALIZE(0);
520 if ( details.GetAddress(&addr, &len) )
521 {
522 printf(" %08lx:%08lx",
523 (unsigned long)addr, (unsigned long)((char *)addr + len));
524 }
525
526 printf(" %s\n", (const char *)details.GetVersion().mb_str());
527 }
528 }
529
530 #endif
531
532 #endif // TEST_DYNLIB
533
534 // ----------------------------------------------------------------------------
535 // wxGet/SetEnv
536 // ----------------------------------------------------------------------------
537
538 #ifdef TEST_ENVIRON
539
540 #include "wx/utils.h"
541
542 static wxString MyGetEnv(const wxString& var)
543 {
544 wxString val;
545 if ( !wxGetEnv(var, &val) )
546 val = wxT("<empty>");
547 else
548 val = wxString(wxT('\'')) + val + wxT('\'');
549
550 return val;
551 }
552
553 static void TestEnvironment()
554 {
555 const wxChar *var = wxT("wxTestVar");
556
557 wxPuts(wxT("*** testing environment access functions ***"));
558
559 wxPrintf(wxT("Initially getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
560 wxSetEnv(var, wxT("value for wxTestVar"));
561 wxPrintf(wxT("After wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
562 wxSetEnv(var, wxT("another value"));
563 wxPrintf(wxT("After 2nd wxSetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
564 wxUnsetEnv(var);
565 wxPrintf(wxT("After wxUnsetEnv: getenv(%s) = %s\n"), var, MyGetEnv(var).c_str());
566 wxPrintf(wxT("PATH = %s\n"), MyGetEnv(wxT("PATH")).c_str());
567 }
568
569 #endif // TEST_ENVIRON
570
571 // ----------------------------------------------------------------------------
572 // file
573 // ----------------------------------------------------------------------------
574
575 #ifdef TEST_FILE
576
577 #include "wx/file.h"
578 #include "wx/ffile.h"
579 #include "wx/textfile.h"
580
581 static void TestFileRead()
582 {
583 wxPuts(wxT("*** wxFile read test ***"));
584
585 wxFile file(wxT("testdata.fc"));
586 if ( file.IsOpened() )
587 {
588 wxPrintf(wxT("File length: %lu\n"), file.Length());
589
590 wxPuts(wxT("File dump:\n----------"));
591
592 static const size_t len = 1024;
593 wxChar buf[len];
594 for ( ;; )
595 {
596 size_t nRead = file.Read(buf, len);
597 if ( nRead == (size_t)wxInvalidOffset )
598 {
599 wxPrintf(wxT("Failed to read the file."));
600 break;
601 }
602
603 fwrite(buf, nRead, 1, stdout);
604
605 if ( nRead < len )
606 break;
607 }
608
609 wxPuts(wxT("----------"));
610 }
611 else
612 {
613 wxPrintf(wxT("ERROR: can't open test file.\n"));
614 }
615
616 wxPuts(wxEmptyString);
617 }
618
619 static void TestTextFileRead()
620 {
621 wxPuts(wxT("*** wxTextFile read test ***"));
622
623 wxTextFile file(wxT("testdata.fc"));
624 if ( file.Open() )
625 {
626 wxPrintf(wxT("Number of lines: %u\n"), file.GetLineCount());
627 wxPrintf(wxT("Last line: '%s'\n"), file.GetLastLine().c_str());
628
629 wxString s;
630
631 wxPuts(wxT("\nDumping the entire file:"));
632 for ( s = file.GetFirstLine(); !file.Eof(); s = file.GetNextLine() )
633 {
634 wxPrintf(wxT("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
635 }
636 wxPrintf(wxT("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
637
638 wxPuts(wxT("\nAnd now backwards:"));
639 for ( s = file.GetLastLine();
640 file.GetCurrentLine() != 0;
641 s = file.GetPrevLine() )
642 {
643 wxPrintf(wxT("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
644 }
645 wxPrintf(wxT("%6u: %s\n"), file.GetCurrentLine() + 1, s.c_str());
646 }
647 else
648 {
649 wxPrintf(wxT("ERROR: can't open '%s'\n"), file.GetName());
650 }
651
652 wxPuts(wxEmptyString);
653 }
654
655 static void TestFileCopy()
656 {
657 wxPuts(wxT("*** Testing wxCopyFile ***"));
658
659 static const wxChar *filename1 = wxT("testdata.fc");
660 static const wxChar *filename2 = wxT("test2");
661 if ( !wxCopyFile(filename1, filename2) )
662 {
663 wxPuts(wxT("ERROR: failed to copy file"));
664 }
665 else
666 {
667 wxFFile f1(filename1, wxT("rb")),
668 f2(filename2, wxT("rb"));
669
670 if ( !f1.IsOpened() || !f2.IsOpened() )
671 {
672 wxPuts(wxT("ERROR: failed to open file(s)"));
673 }
674 else
675 {
676 wxString s1, s2;
677 if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
678 {
679 wxPuts(wxT("ERROR: failed to read file(s)"));
680 }
681 else
682 {
683 if ( (s1.length() != s2.length()) ||
684 (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
685 {
686 wxPuts(wxT("ERROR: copy error!"));
687 }
688 else
689 {
690 wxPuts(wxT("File was copied ok."));
691 }
692 }
693 }
694 }
695
696 if ( !wxRemoveFile(filename2) )
697 {
698 wxPuts(wxT("ERROR: failed to remove the file"));
699 }
700
701 wxPuts(wxEmptyString);
702 }
703
704 static void TestTempFile()
705 {
706 wxPuts(wxT("*** wxTempFile test ***"));
707
708 wxTempFile tmpFile;
709 if ( tmpFile.Open(wxT("test2")) && tmpFile.Write(wxT("the answer is 42")) )
710 {
711 if ( tmpFile.Commit() )
712 wxPuts(wxT("File committed."));
713 else
714 wxPuts(wxT("ERROR: could't commit temp file."));
715
716 wxRemoveFile(wxT("test2"));
717 }
718
719 wxPuts(wxEmptyString);
720 }
721
722 #endif // TEST_FILE
723
724 // ----------------------------------------------------------------------------
725 // wxFileConfig
726 // ----------------------------------------------------------------------------
727
728 #ifdef TEST_FILECONF
729
730 #include "wx/confbase.h"
731 #include "wx/fileconf.h"
732
733 static const struct FileConfTestData
734 {
735 const wxChar *name; // value name
736 const wxChar *value; // the value from the file
737 } fcTestData[] =
738 {
739 { wxT("value1"), wxT("one") },
740 { wxT("value2"), wxT("two") },
741 { wxT("novalue"), wxT("default") },
742 };
743
744 static void TestFileConfRead()
745 {
746 wxPuts(wxT("*** testing wxFileConfig loading/reading ***"));
747
748 wxFileConfig fileconf(wxT("test"), wxEmptyString,
749 wxT("testdata.fc"), wxEmptyString,
750 wxCONFIG_USE_RELATIVE_PATH);
751
752 // test simple reading
753 wxPuts(wxT("\nReading config file:"));
754 wxString defValue(wxT("default")), value;
755 for ( size_t n = 0; n < WXSIZEOF(fcTestData); n++ )
756 {
757 const FileConfTestData& data = fcTestData[n];
758 value = fileconf.Read(data.name, defValue);
759 wxPrintf(wxT("\t%s = %s "), data.name, value.c_str());
760 if ( value == data.value )
761 {
762 wxPuts(wxT("(ok)"));
763 }
764 else
765 {
766 wxPrintf(wxT("(ERROR: should be %s)\n"), data.value);
767 }
768 }
769
770 // test enumerating the entries
771 wxPuts(wxT("\nEnumerating all root entries:"));
772 long dummy;
773 wxString name;
774 bool cont = fileconf.GetFirstEntry(name, dummy);
775 while ( cont )
776 {
777 wxPrintf(wxT("\t%s = %s\n"),
778 name.c_str(),
779 fileconf.Read(name.c_str(), wxT("ERROR")).c_str());
780
781 cont = fileconf.GetNextEntry(name, dummy);
782 }
783
784 static const wxChar *testEntry = wxT("TestEntry");
785 wxPrintf(wxT("\nTesting deletion of newly created \"Test\" entry: "));
786 fileconf.Write(testEntry, wxT("A value"));
787 fileconf.DeleteEntry(testEntry);
788 wxPrintf(fileconf.HasEntry(testEntry) ? wxT("ERROR\n") : wxT("ok\n"));
789 }
790
791 #endif // TEST_FILECONF
792
793 // ----------------------------------------------------------------------------
794 // wxFileName
795 // ----------------------------------------------------------------------------
796
797 #ifdef TEST_FILENAME
798
799 #include "wx/filename.h"
800
801 #if 0
802 static void DumpFileName(const wxChar *desc, const wxFileName& fn)
803 {
804 wxPuts(desc);
805
806 wxString full = fn.GetFullPath();
807
808 wxString vol, path, name, ext;
809 wxFileName::SplitPath(full, &vol, &path, &name, &ext);
810
811 wxPrintf(wxT("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
812 full.c_str(), vol.c_str(), path.c_str(), name.c_str(), ext.c_str());
813
814 wxFileName::SplitPath(full, &path, &name, &ext);
815 wxPrintf(wxT("or\t\t-> path '%s', name '%s', ext '%s'\n"),
816 path.c_str(), name.c_str(), ext.c_str());
817
818 wxPrintf(wxT("path is also:\t'%s'\n"), fn.GetPath().c_str());
819 wxPrintf(wxT("with volume: \t'%s'\n"),
820 fn.GetPath(wxPATH_GET_VOLUME).c_str());
821 wxPrintf(wxT("with separator:\t'%s'\n"),
822 fn.GetPath(wxPATH_GET_SEPARATOR).c_str());
823 wxPrintf(wxT("with both: \t'%s'\n"),
824 fn.GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME).c_str());
825
826 wxPuts(wxT("The directories in the path are:"));
827 wxArrayString dirs = fn.GetDirs();
828 size_t count = dirs.GetCount();
829 for ( size_t n = 0; n < count; n++ )
830 {
831 wxPrintf(wxT("\t%u: %s\n"), n, dirs[n].c_str());
832 }
833 }
834 #endif
835
836 static void TestFileNameTemp()
837 {
838 wxPuts(wxT("*** testing wxFileName temp file creation ***"));
839
840 static const wxChar *tmpprefixes[] =
841 {
842 wxT(""),
843 wxT("foo"),
844 wxT(".."),
845 wxT("../bar"),
846 #ifdef __UNIX__
847 wxT("/tmp/foo"),
848 wxT("/tmp/foo/bar"), // this one must be an error
849 #endif // __UNIX__
850 };
851
852 for ( size_t n = 0; n < WXSIZEOF(tmpprefixes); n++ )
853 {
854 wxString path = wxFileName::CreateTempFileName(tmpprefixes[n]);
855 if ( path.empty() )
856 {
857 // "error" is not in upper case because it may be ok
858 wxPrintf(wxT("Prefix '%s'\t-> error\n"), tmpprefixes[n]);
859 }
860 else
861 {
862 wxPrintf(wxT("Prefix '%s'\t-> temp file '%s'\n"),
863 tmpprefixes[n], path.c_str());
864
865 if ( !wxRemoveFile(path) )
866 {
867 wxLogWarning(wxT("Failed to remove temp file '%s'"),
868 path.c_str());
869 }
870 }
871 }
872 }
873
874 static void TestFileNameDirManip()
875 {
876 // TODO: test AppendDir(), RemoveDir(), ...
877 }
878
879 static void TestFileNameComparison()
880 {
881 // TODO!
882 }
883
884 static void TestFileNameOperations()
885 {
886 // TODO!
887 }
888
889 static void TestFileNameCwd()
890 {
891 // TODO!
892 }
893
894 #endif // TEST_FILENAME
895
896 // ----------------------------------------------------------------------------
897 // wxFileName time functions
898 // ----------------------------------------------------------------------------
899
900 #ifdef TEST_FILETIME
901
902 #include "wx/filename.h"
903 #include "wx/datetime.h"
904
905 static void TestFileGetTimes()
906 {
907 wxFileName fn(wxT("testdata.fc"));
908
909 wxDateTime dtAccess, dtMod, dtCreate;
910 if ( !fn.GetTimes(&dtAccess, &dtMod, &dtCreate) )
911 {
912 wxPrintf(wxT("ERROR: GetTimes() failed.\n"));
913 }
914 else
915 {
916 static const wxChar *fmt = wxT("%Y-%b-%d %H:%M:%S");
917
918 wxPrintf(wxT("File times for '%s':\n"), fn.GetFullPath().c_str());
919 wxPrintf(wxT("Creation: \t%s\n"), dtCreate.Format(fmt).c_str());
920 wxPrintf(wxT("Last read: \t%s\n"), dtAccess.Format(fmt).c_str());
921 wxPrintf(wxT("Last write: \t%s\n"), dtMod.Format(fmt).c_str());
922 }
923 }
924
925 #if 0
926 static void TestFileSetTimes()
927 {
928 wxFileName fn(wxT("testdata.fc"));
929
930 if ( !fn.Touch() )
931 {
932 wxPrintf(wxT("ERROR: Touch() failed.\n"));
933 }
934 }
935 #endif
936
937 #endif // TEST_FILETIME
938
939 // ----------------------------------------------------------------------------
940 // wxLocale
941 // ----------------------------------------------------------------------------
942
943 #ifdef TEST_LOCALE
944
945 #include "wx/intl.h"
946 #include "wx/utils.h" // for wxSetEnv
947
948 static wxLocale gs_localeDefault;
949 // NOTE: don't init it here as it needs a wxAppTraits object
950 // and thus must be init-ed after creation of the wxInitializer
951 // class in the main()
952
953 // find the name of the language from its value
954 static const wxChar *GetLangName(int lang)
955 {
956 static const wxChar *languageNames[] =
957 {
958 wxT("DEFAULT"),
959 wxT("UNKNOWN"),
960 wxT("ABKHAZIAN"),
961 wxT("AFAR"),
962 wxT("AFRIKAANS"),
963 wxT("ALBANIAN"),
964 wxT("AMHARIC"),
965 wxT("ARABIC"),
966 wxT("ARABIC_ALGERIA"),
967 wxT("ARABIC_BAHRAIN"),
968 wxT("ARABIC_EGYPT"),
969 wxT("ARABIC_IRAQ"),
970 wxT("ARABIC_JORDAN"),
971 wxT("ARABIC_KUWAIT"),
972 wxT("ARABIC_LEBANON"),
973 wxT("ARABIC_LIBYA"),
974 wxT("ARABIC_MOROCCO"),
975 wxT("ARABIC_OMAN"),
976 wxT("ARABIC_QATAR"),
977 wxT("ARABIC_SAUDI_ARABIA"),
978 wxT("ARABIC_SUDAN"),
979 wxT("ARABIC_SYRIA"),
980 wxT("ARABIC_TUNISIA"),
981 wxT("ARABIC_UAE"),
982 wxT("ARABIC_YEMEN"),
983 wxT("ARMENIAN"),
984 wxT("ASSAMESE"),
985 wxT("AYMARA"),
986 wxT("AZERI"),
987 wxT("AZERI_CYRILLIC"),
988 wxT("AZERI_LATIN"),
989 wxT("BASHKIR"),
990 wxT("BASQUE"),
991 wxT("BELARUSIAN"),
992 wxT("BENGALI"),
993 wxT("BHUTANI"),
994 wxT("BIHARI"),
995 wxT("BISLAMA"),
996 wxT("BRETON"),
997 wxT("BULGARIAN"),
998 wxT("BURMESE"),
999 wxT("CAMBODIAN"),
1000 wxT("CATALAN"),
1001 wxT("CHINESE"),
1002 wxT("CHINESE_SIMPLIFIED"),
1003 wxT("CHINESE_TRADITIONAL"),
1004 wxT("CHINESE_HONGKONG"),
1005 wxT("CHINESE_MACAU"),
1006 wxT("CHINESE_SINGAPORE"),
1007 wxT("CHINESE_TAIWAN"),
1008 wxT("CORSICAN"),
1009 wxT("CROATIAN"),
1010 wxT("CZECH"),
1011 wxT("DANISH"),
1012 wxT("DUTCH"),
1013 wxT("DUTCH_BELGIAN"),
1014 wxT("ENGLISH"),
1015 wxT("ENGLISH_UK"),
1016 wxT("ENGLISH_US"),
1017 wxT("ENGLISH_AUSTRALIA"),
1018 wxT("ENGLISH_BELIZE"),
1019 wxT("ENGLISH_BOTSWANA"),
1020 wxT("ENGLISH_CANADA"),
1021 wxT("ENGLISH_CARIBBEAN"),
1022 wxT("ENGLISH_DENMARK"),
1023 wxT("ENGLISH_EIRE"),
1024 wxT("ENGLISH_JAMAICA"),
1025 wxT("ENGLISH_NEW_ZEALAND"),
1026 wxT("ENGLISH_PHILIPPINES"),
1027 wxT("ENGLISH_SOUTH_AFRICA"),
1028 wxT("ENGLISH_TRINIDAD"),
1029 wxT("ENGLISH_ZIMBABWE"),
1030 wxT("ESPERANTO"),
1031 wxT("ESTONIAN"),
1032 wxT("FAEROESE"),
1033 wxT("FARSI"),
1034 wxT("FIJI"),
1035 wxT("FINNISH"),
1036 wxT("FRENCH"),
1037 wxT("FRENCH_BELGIAN"),
1038 wxT("FRENCH_CANADIAN"),
1039 wxT("FRENCH_LUXEMBOURG"),
1040 wxT("FRENCH_MONACO"),
1041 wxT("FRENCH_SWISS"),
1042 wxT("FRISIAN"),
1043 wxT("GALICIAN"),
1044 wxT("GEORGIAN"),
1045 wxT("GERMAN"),
1046 wxT("GERMAN_AUSTRIAN"),
1047 wxT("GERMAN_BELGIUM"),
1048 wxT("GERMAN_LIECHTENSTEIN"),
1049 wxT("GERMAN_LUXEMBOURG"),
1050 wxT("GERMAN_SWISS"),
1051 wxT("GREEK"),
1052 wxT("GREENLANDIC"),
1053 wxT("GUARANI"),
1054 wxT("GUJARATI"),
1055 wxT("HAUSA"),
1056 wxT("HEBREW"),
1057 wxT("HINDI"),
1058 wxT("HUNGARIAN"),
1059 wxT("ICELANDIC"),
1060 wxT("INDONESIAN"),
1061 wxT("INTERLINGUA"),
1062 wxT("INTERLINGUE"),
1063 wxT("INUKTITUT"),
1064 wxT("INUPIAK"),
1065 wxT("IRISH"),
1066 wxT("ITALIAN"),
1067 wxT("ITALIAN_SWISS"),
1068 wxT("JAPANESE"),
1069 wxT("JAVANESE"),
1070 wxT("KANNADA"),
1071 wxT("KASHMIRI"),
1072 wxT("KASHMIRI_INDIA"),
1073 wxT("KAZAKH"),
1074 wxT("KERNEWEK"),
1075 wxT("KINYARWANDA"),
1076 wxT("KIRGHIZ"),
1077 wxT("KIRUNDI"),
1078 wxT("KONKANI"),
1079 wxT("KOREAN"),
1080 wxT("KURDISH"),
1081 wxT("LAOTHIAN"),
1082 wxT("LATIN"),
1083 wxT("LATVIAN"),
1084 wxT("LINGALA"),
1085 wxT("LITHUANIAN"),
1086 wxT("MACEDONIAN"),
1087 wxT("MALAGASY"),
1088 wxT("MALAY"),
1089 wxT("MALAYALAM"),
1090 wxT("MALAY_BRUNEI_DARUSSALAM"),
1091 wxT("MALAY_MALAYSIA"),
1092 wxT("MALTESE"),
1093 wxT("MANIPURI"),
1094 wxT("MAORI"),
1095 wxT("MARATHI"),
1096 wxT("MOLDAVIAN"),
1097 wxT("MONGOLIAN"),
1098 wxT("NAURU"),
1099 wxT("NEPALI"),
1100 wxT("NEPALI_INDIA"),
1101 wxT("NORWEGIAN_BOKMAL"),
1102 wxT("NORWEGIAN_NYNORSK"),
1103 wxT("OCCITAN"),
1104 wxT("ORIYA"),
1105 wxT("OROMO"),
1106 wxT("PASHTO"),
1107 wxT("POLISH"),
1108 wxT("PORTUGUESE"),
1109 wxT("PORTUGUESE_BRAZILIAN"),
1110 wxT("PUNJABI"),
1111 wxT("QUECHUA"),
1112 wxT("RHAETO_ROMANCE"),
1113 wxT("ROMANIAN"),
1114 wxT("RUSSIAN"),
1115 wxT("RUSSIAN_UKRAINE"),
1116 wxT("SAMOAN"),
1117 wxT("SANGHO"),
1118 wxT("SANSKRIT"),
1119 wxT("SCOTS_GAELIC"),
1120 wxT("SERBIAN"),
1121 wxT("SERBIAN_CYRILLIC"),
1122 wxT("SERBIAN_LATIN"),
1123 wxT("SERBO_CROATIAN"),
1124 wxT("SESOTHO"),
1125 wxT("SETSWANA"),
1126 wxT("SHONA"),
1127 wxT("SINDHI"),
1128 wxT("SINHALESE"),
1129 wxT("SISWATI"),
1130 wxT("SLOVAK"),
1131 wxT("SLOVENIAN"),
1132 wxT("SOMALI"),
1133 wxT("SPANISH"),
1134 wxT("SPANISH_ARGENTINA"),
1135 wxT("SPANISH_BOLIVIA"),
1136 wxT("SPANISH_CHILE"),
1137 wxT("SPANISH_COLOMBIA"),
1138 wxT("SPANISH_COSTA_RICA"),
1139 wxT("SPANISH_DOMINICAN_REPUBLIC"),
1140 wxT("SPANISH_ECUADOR"),
1141 wxT("SPANISH_EL_SALVADOR"),
1142 wxT("SPANISH_GUATEMALA"),
1143 wxT("SPANISH_HONDURAS"),
1144 wxT("SPANISH_MEXICAN"),
1145 wxT("SPANISH_MODERN"),
1146 wxT("SPANISH_NICARAGUA"),
1147 wxT("SPANISH_PANAMA"),
1148 wxT("SPANISH_PARAGUAY"),
1149 wxT("SPANISH_PERU"),
1150 wxT("SPANISH_PUERTO_RICO"),
1151 wxT("SPANISH_URUGUAY"),
1152 wxT("SPANISH_US"),
1153 wxT("SPANISH_VENEZUELA"),
1154 wxT("SUNDANESE"),
1155 wxT("SWAHILI"),
1156 wxT("SWEDISH"),
1157 wxT("SWEDISH_FINLAND"),
1158 wxT("TAGALOG"),
1159 wxT("TAJIK"),
1160 wxT("TAMIL"),
1161 wxT("TATAR"),
1162 wxT("TELUGU"),
1163 wxT("THAI"),
1164 wxT("TIBETAN"),
1165 wxT("TIGRINYA"),
1166 wxT("TONGA"),
1167 wxT("TSONGA"),
1168 wxT("TURKISH"),
1169 wxT("TURKMEN"),
1170 wxT("TWI"),
1171 wxT("UIGHUR"),
1172 wxT("UKRAINIAN"),
1173 wxT("URDU"),
1174 wxT("URDU_INDIA"),
1175 wxT("URDU_PAKISTAN"),
1176 wxT("UZBEK"),
1177 wxT("UZBEK_CYRILLIC"),
1178 wxT("UZBEK_LATIN"),
1179 wxT("VIETNAMESE"),
1180 wxT("VOLAPUK"),
1181 wxT("WELSH"),
1182 wxT("WOLOF"),
1183 wxT("XHOSA"),
1184 wxT("YIDDISH"),
1185 wxT("YORUBA"),
1186 wxT("ZHUANG"),
1187 wxT("ZULU"),
1188 };
1189
1190 if ( (size_t)lang < WXSIZEOF(languageNames) )
1191 return languageNames[lang];
1192 else
1193 return wxT("INVALID");
1194 }
1195
1196 static void TestDefaultLang()
1197 {
1198 wxPuts(wxT("*** Testing wxLocale::GetSystemLanguage ***"));
1199
1200 gs_localeDefault.Init(wxLANGUAGE_ENGLISH);
1201
1202 static const wxChar *langStrings[] =
1203 {
1204 NULL, // system default
1205 wxT("C"),
1206 wxT("fr"),
1207 wxT("fr_FR"),
1208 wxT("en"),
1209 wxT("en_GB"),
1210 wxT("en_US"),
1211 wxT("de_DE.iso88591"),
1212 wxT("german"),
1213 wxT("?"), // invalid lang spec
1214 wxT("klingonese"), // I bet on some systems it does exist...
1215 };
1216
1217 wxPrintf(wxT("The default system encoding is %s (%d)\n"),
1218 wxLocale::GetSystemEncodingName().c_str(),
1219 wxLocale::GetSystemEncoding());
1220
1221 for ( size_t n = 0; n < WXSIZEOF(langStrings); n++ )
1222 {
1223 const wxChar *langStr = langStrings[n];
1224 if ( langStr )
1225 {
1226 // FIXME: this doesn't do anything at all under Windows, we need
1227 // to create a new wxLocale!
1228 wxSetEnv(wxT("LC_ALL"), langStr);
1229 }
1230
1231 int lang = gs_localeDefault.GetSystemLanguage();
1232 wxPrintf(wxT("Locale for '%s' is %s.\n"),
1233 langStr ? langStr : wxT("system default"), GetLangName(lang));
1234 }
1235 }
1236
1237 #endif // TEST_LOCALE
1238
1239 // ----------------------------------------------------------------------------
1240 // MIME types
1241 // ----------------------------------------------------------------------------
1242
1243 #ifdef TEST_MIME
1244
1245 #include "wx/mimetype.h"
1246
1247 static void TestMimeEnum()
1248 {
1249 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
1250
1251 wxArrayString mimetypes;
1252
1253 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
1254
1255 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count);
1256
1257 wxArrayString exts;
1258 wxString desc;
1259
1260 for ( size_t n = 0; n < count; n++ )
1261 {
1262 wxFileType *filetype =
1263 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
1264 if ( !filetype )
1265 {
1266 wxPrintf(wxT("nothing known about the filetype '%s'!\n"),
1267 mimetypes[n].c_str());
1268 continue;
1269 }
1270
1271 filetype->GetDescription(&desc);
1272 filetype->GetExtensions(exts);
1273
1274 filetype->GetIcon(NULL);
1275
1276 wxString extsAll;
1277 for ( size_t e = 0; e < exts.GetCount(); e++ )
1278 {
1279 if ( e > 0 )
1280 extsAll << wxT(", ");
1281 extsAll += exts[e];
1282 }
1283
1284 wxPrintf(wxT("\t%s: %s (%s)\n"),
1285 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
1286 }
1287
1288 wxPuts(wxEmptyString);
1289 }
1290
1291 static void TestMimeFilename()
1292 {
1293 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
1294
1295 static const wxChar *filenames[] =
1296 {
1297 wxT("readme.txt"),
1298 wxT("document.pdf"),
1299 wxT("image.gif"),
1300 wxT("picture.jpeg"),
1301 };
1302
1303 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
1304 {
1305 const wxString fname = filenames[n];
1306 wxString ext = fname.AfterLast(wxT('.'));
1307 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
1308 if ( !ft )
1309 {
1310 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext.c_str());
1311 }
1312 else
1313 {
1314 wxString desc;
1315 if ( !ft->GetDescription(&desc) )
1316 desc = wxT("<no description>");
1317
1318 wxString cmd;
1319 if ( !ft->GetOpenCommand(&cmd,
1320 wxFileType::MessageParameters(fname, wxEmptyString)) )
1321 cmd = wxT("<no command available>");
1322 else
1323 cmd = wxString(wxT('"')) + cmd + wxT('"');
1324
1325 wxPrintf(wxT("To open %s (%s) do %s.\n"),
1326 fname.c_str(), desc.c_str(), cmd.c_str());
1327
1328 delete ft;
1329 }
1330 }
1331
1332 wxPuts(wxEmptyString);
1333 }
1334
1335 // these tests were broken by wxMimeTypesManager changes, temporarily disabling
1336 #if 0
1337
1338 static void TestMimeOverride()
1339 {
1340 wxPuts(wxT("*** Testing wxMimeTypesManager additional files loading ***\n"));
1341
1342 static const wxChar *mailcap = wxT("/tmp/mailcap");
1343 static const wxChar *mimetypes = wxT("/tmp/mime.types");
1344
1345 if ( wxFile::Exists(mailcap) )
1346 wxPrintf(wxT("Loading mailcap from '%s': %s\n"),
1347 mailcap,
1348 wxTheMimeTypesManager->ReadMailcap(mailcap) ? wxT("ok") : wxT("ERROR"));
1349 else
1350 wxPrintf(wxT("WARN: mailcap file '%s' doesn't exist, not loaded.\n"),
1351 mailcap);
1352
1353 if ( wxFile::Exists(mimetypes) )
1354 wxPrintf(wxT("Loading mime.types from '%s': %s\n"),
1355 mimetypes,
1356 wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? wxT("ok") : wxT("ERROR"));
1357 else
1358 wxPrintf(wxT("WARN: mime.types file '%s' doesn't exist, not loaded.\n"),
1359 mimetypes);
1360
1361 wxPuts(wxEmptyString);
1362 }
1363
1364 static void TestMimeAssociate()
1365 {
1366 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
1367
1368 wxFileTypeInfo ftInfo(
1369 wxT("application/x-xyz"),
1370 wxT("xyzview '%s'"), // open cmd
1371 wxT(""), // print cmd
1372 wxT("XYZ File"), // description
1373 wxT(".xyz"), // extensions
1374 wxNullPtr // end of extensions
1375 );
1376 ftInfo.SetShortDesc(wxT("XYZFile")); // used under Win32 only
1377
1378 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
1379 if ( !ft )
1380 {
1381 wxPuts(wxT("ERROR: failed to create association!"));
1382 }
1383 else
1384 {
1385 // TODO: read it back
1386 delete ft;
1387 }
1388
1389 wxPuts(wxEmptyString);
1390 }
1391
1392 #endif // 0
1393
1394 #endif // TEST_MIME
1395
1396 // ----------------------------------------------------------------------------
1397 // module dependencies feature
1398 // ----------------------------------------------------------------------------
1399
1400 #ifdef TEST_MODULE
1401
1402 #include "wx/module.h"
1403
1404 class wxTestModule : public wxModule
1405 {
1406 protected:
1407 virtual bool OnInit() { wxPrintf(wxT("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; }
1408 virtual void OnExit() { wxPrintf(wxT("Unload module: %s\n"), GetClassInfo()->GetClassName()); }
1409 };
1410
1411 class wxTestModuleA : public wxTestModule
1412 {
1413 public:
1414 wxTestModuleA();
1415 private:
1416 DECLARE_DYNAMIC_CLASS(wxTestModuleA)
1417 };
1418
1419 class wxTestModuleB : public wxTestModule
1420 {
1421 public:
1422 wxTestModuleB();
1423 private:
1424 DECLARE_DYNAMIC_CLASS(wxTestModuleB)
1425 };
1426
1427 class wxTestModuleC : public wxTestModule
1428 {
1429 public:
1430 wxTestModuleC();
1431 private:
1432 DECLARE_DYNAMIC_CLASS(wxTestModuleC)
1433 };
1434
1435 class wxTestModuleD : public wxTestModule
1436 {
1437 public:
1438 wxTestModuleD();
1439 private:
1440 DECLARE_DYNAMIC_CLASS(wxTestModuleD)
1441 };
1442
1443 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC, wxModule)
1444 wxTestModuleC::wxTestModuleC()
1445 {
1446 AddDependency(CLASSINFO(wxTestModuleD));
1447 }
1448
1449 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA, wxModule)
1450 wxTestModuleA::wxTestModuleA()
1451 {
1452 AddDependency(CLASSINFO(wxTestModuleB));
1453 AddDependency(CLASSINFO(wxTestModuleD));
1454 }
1455
1456 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD, wxModule)
1457 wxTestModuleD::wxTestModuleD()
1458 {
1459 }
1460
1461 IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB, wxModule)
1462 wxTestModuleB::wxTestModuleB()
1463 {
1464 AddDependency(CLASSINFO(wxTestModuleD));
1465 AddDependency(CLASSINFO(wxTestModuleC));
1466 }
1467
1468 #endif // TEST_MODULE
1469
1470 // ----------------------------------------------------------------------------
1471 // misc information functions
1472 // ----------------------------------------------------------------------------
1473
1474 #ifdef TEST_INFO_FUNCTIONS
1475
1476 #include "wx/utils.h"
1477
1478 #if TEST_INTERACTIVE
1479 static void TestDiskInfo()
1480 {
1481 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
1482
1483 for ( ;; )
1484 {
1485 wxChar pathname[128];
1486 wxPrintf(wxT("\nEnter a directory name: "));
1487 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
1488 break;
1489
1490 // kill the last '\n'
1491 pathname[wxStrlen(pathname) - 1] = 0;
1492
1493 wxLongLong total, free;
1494 if ( !wxGetDiskSpace(pathname, &total, &free) )
1495 {
1496 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
1497 }
1498 else
1499 {
1500 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
1501 (total / 1024).ToString().c_str(),
1502 (free / 1024).ToString().c_str(),
1503 pathname);
1504 }
1505 }
1506 }
1507 #endif // TEST_INTERACTIVE
1508
1509 static void TestOsInfo()
1510 {
1511 wxPuts(wxT("*** Testing OS info functions ***\n"));
1512
1513 int major, minor;
1514 wxGetOsVersion(&major, &minor);
1515 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
1516 wxGetOsDescription().c_str(), major, minor);
1517
1518 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
1519
1520 wxPrintf(wxT("Host name is %s (%s).\n"),
1521 wxGetHostName().c_str(), wxGetFullHostName().c_str());
1522
1523 wxPuts(wxEmptyString);
1524 }
1525
1526 static void TestPlatformInfo()
1527 {
1528 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
1529
1530 // get this platform
1531 wxPlatformInfo plat;
1532
1533 wxPrintf(wxT("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str());
1534 wxPrintf(wxT("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str());
1535 wxPrintf(wxT("Port ID name is: %s\n"), plat.GetPortIdName().c_str());
1536 wxPrintf(wxT("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str());
1537 wxPrintf(wxT("Architecture is: %s\n"), plat.GetArchName().c_str());
1538 wxPrintf(wxT("Endianness is: %s\n"), plat.GetEndiannessName().c_str());
1539
1540 wxPuts(wxEmptyString);
1541 }
1542
1543 static void TestUserInfo()
1544 {
1545 wxPuts(wxT("*** Testing user info functions ***\n"));
1546
1547 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
1548 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
1549 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
1550 wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str());
1551
1552 wxPuts(wxEmptyString);
1553 }
1554
1555 #endif // TEST_INFO_FUNCTIONS
1556
1557 // ----------------------------------------------------------------------------
1558 // path list
1559 // ----------------------------------------------------------------------------
1560
1561 #ifdef TEST_PATHLIST
1562
1563 #ifdef __UNIX__
1564 #define CMD_IN_PATH wxT("ls")
1565 #else
1566 #define CMD_IN_PATH wxT("command.com")
1567 #endif
1568
1569 static void TestPathList()
1570 {
1571 wxPuts(wxT("*** Testing wxPathList ***\n"));
1572
1573 wxPathList pathlist;
1574 pathlist.AddEnvList(wxT("PATH"));
1575 wxString path = pathlist.FindValidPath(CMD_IN_PATH);
1576 if ( path.empty() )
1577 {
1578 wxPrintf(wxT("ERROR: command not found in the path.\n"));
1579 }
1580 else
1581 {
1582 wxPrintf(wxT("Command found in the path as '%s'.\n"), path.c_str());
1583 }
1584 }
1585
1586 #endif // TEST_PATHLIST
1587
1588 // ----------------------------------------------------------------------------
1589 // regular expressions
1590 // ----------------------------------------------------------------------------
1591
1592 #if defined TEST_REGEX && TEST_INTERACTIVE
1593
1594 #include "wx/regex.h"
1595
1596 static void TestRegExInteractive()
1597 {
1598 wxPuts(wxT("*** Testing RE interactively ***"));
1599
1600 for ( ;; )
1601 {
1602 wxChar pattern[128];
1603 wxPrintf(wxT("\nEnter a pattern: "));
1604 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
1605 break;
1606
1607 // kill the last '\n'
1608 pattern[wxStrlen(pattern) - 1] = 0;
1609
1610 wxRegEx re;
1611 if ( !re.Compile(pattern) )
1612 {
1613 continue;
1614 }
1615
1616 wxChar text[128];
1617 for ( ;; )
1618 {
1619 wxPrintf(wxT("Enter text to match: "));
1620 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
1621 break;
1622
1623 // kill the last '\n'
1624 text[wxStrlen(text) - 1] = 0;
1625
1626 if ( !re.Matches(text) )
1627 {
1628 wxPrintf(wxT("No match.\n"));
1629 }
1630 else
1631 {
1632 wxPrintf(wxT("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
1633
1634 size_t start, len;
1635 for ( size_t n = 1; ; n++ )
1636 {
1637 if ( !re.GetMatch(&start, &len, n) )
1638 {
1639 break;
1640 }
1641
1642 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
1643 n, wxString(text + start, len).c_str());
1644 }
1645 }
1646 }
1647 }
1648 }
1649
1650 #endif // TEST_REGEX
1651
1652 // ----------------------------------------------------------------------------
1653 // printf() tests
1654 // ----------------------------------------------------------------------------
1655
1656 /*
1657 NB: this stuff was taken from the glibc test suite and modified to build
1658 in wxWidgets: if I read the copyright below properly, this shouldn't
1659 be a problem
1660 */
1661
1662 #ifdef TEST_PRINTF
1663
1664 #ifdef wxTEST_PRINTF
1665 // use our functions from wxchar.cpp
1666 #undef wxPrintf
1667 #undef wxSprintf
1668
1669 // NB: do _not_ use WX_ATTRIBUTE_PRINTF here, we have some invalid formats
1670 // in the tests below
1671 int wxPrintf( const wxChar *format, ... );
1672 int wxSprintf( wxChar *str, const wxChar *format, ... );
1673 #endif
1674
1675 #include "wx/longlong.h"
1676
1677 #include <float.h>
1678
1679 static void rfg1 (void);
1680 static void rfg2 (void);
1681
1682
1683 static void
1684 fmtchk (const wxChar *fmt)
1685 {
1686 (void) wxPrintf(wxT("%s:\t`"), fmt);
1687 (void) wxPrintf(fmt, 0x12);
1688 (void) wxPrintf(wxT("'\n"));
1689 }
1690
1691 static void
1692 fmtst1chk (const wxChar *fmt)
1693 {
1694 (void) wxPrintf(wxT("%s:\t`"), fmt);
1695 (void) wxPrintf(fmt, 4, 0x12);
1696 (void) wxPrintf(wxT("'\n"));
1697 }
1698
1699 static void
1700 fmtst2chk (const wxChar *fmt)
1701 {
1702 (void) wxPrintf(wxT("%s:\t`"), fmt);
1703 (void) wxPrintf(fmt, 4, 4, 0x12);
1704 (void) wxPrintf(wxT("'\n"));
1705 }
1706
1707 /* This page is covered by the following copyright: */
1708
1709 /* (C) Copyright C E Chew
1710 *
1711 * Feel free to copy, use and distribute this software provided:
1712 *
1713 * 1. you do not pretend that you wrote it
1714 * 2. you leave this copyright notice intact.
1715 */
1716
1717 /*
1718 * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
1719 */
1720
1721 #define DEC -123
1722 #define INT 255
1723 #define UNS (~0)
1724
1725 /* Formatted Output Test
1726 *
1727 * This exercises the output formatting code.
1728 */
1729
1730 wxChar *PointerNull = NULL;
1731
1732 static void
1733 fp_test (void)
1734 {
1735 int i, j, k, l;
1736 wxChar buf[7];
1737 wxChar *prefix = buf;
1738 wxChar tp[20];
1739
1740 wxPuts(wxT("\nFormatted output test"));
1741 wxPrintf(wxT("prefix 6d 6o 6x 6X 6u\n"));
1742 wxStrcpy(prefix, wxT("%"));
1743 for (i = 0; i < 2; i++) {
1744 for (j = 0; j < 2; j++) {
1745 for (k = 0; k < 2; k++) {
1746 for (l = 0; l < 2; l++) {
1747 wxStrcpy(prefix, wxT("%"));
1748 if (i == 0) wxStrcat(prefix, wxT("-"));
1749 if (j == 0) wxStrcat(prefix, wxT("+"));
1750 if (k == 0) wxStrcat(prefix, wxT("#"));
1751 if (l == 0) wxStrcat(prefix, wxT("0"));
1752 wxPrintf(wxT("%5s |"), prefix);
1753 wxStrcpy(tp, prefix);
1754 wxStrcat(tp, wxT("6d |"));
1755 wxPrintf(tp, DEC);
1756 wxStrcpy(tp, prefix);
1757 wxStrcat(tp, wxT("6o |"));
1758 wxPrintf(tp, INT);
1759 wxStrcpy(tp, prefix);
1760 wxStrcat(tp, wxT("6x |"));
1761 wxPrintf(tp, INT);
1762 wxStrcpy(tp, prefix);
1763 wxStrcat(tp, wxT("6X |"));
1764 wxPrintf(tp, INT);
1765 wxStrcpy(tp, prefix);
1766 wxStrcat(tp, wxT("6u |"));
1767 wxPrintf(tp, UNS);
1768 wxPrintf(wxT("\n"));
1769 }
1770 }
1771 }
1772 }
1773 wxPrintf(wxT("%10s\n"), PointerNull);
1774 wxPrintf(wxT("%-10s\n"), PointerNull);
1775 }
1776
1777 static void TestPrintf()
1778 {
1779 static wxChar shortstr[] = wxT("Hi, Z.");
1780 static wxChar longstr[] = wxT("Good morning, Doctor Chandra. This is Hal. \
1781 I am ready for my first lesson today.");
1782 int result = 0;
1783 wxString test_format;
1784
1785 fmtchk(wxT("%.4x"));
1786 fmtchk(wxT("%04x"));
1787 fmtchk(wxT("%4.4x"));
1788 fmtchk(wxT("%04.4x"));
1789 fmtchk(wxT("%4.3x"));
1790 fmtchk(wxT("%04.3x"));
1791
1792 fmtst1chk(wxT("%.*x"));
1793 fmtst1chk(wxT("%0*x"));
1794 fmtst2chk(wxT("%*.*x"));
1795 fmtst2chk(wxT("%0*.*x"));
1796
1797 wxString bad_format = wxT("bad format:\t\"%b\"\n");
1798 wxPrintf(bad_format.c_str());
1799 wxPrintf(wxT("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
1800
1801 wxPrintf(wxT("decimal negative:\t\"%d\"\n"), -2345);
1802 wxPrintf(wxT("octal negative:\t\"%o\"\n"), -2345);
1803 wxPrintf(wxT("hex negative:\t\"%x\"\n"), -2345);
1804 wxPrintf(wxT("long decimal number:\t\"%ld\"\n"), -123456L);
1805 wxPrintf(wxT("long octal negative:\t\"%lo\"\n"), -2345L);
1806 wxPrintf(wxT("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
1807 wxPrintf(wxT("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
1808 test_format = wxT("left-adjusted ZLDN:\t\"%-010ld\"\n");
1809 wxPrintf(test_format.c_str(), -123456);
1810 wxPrintf(wxT("space-padded LDN:\t\"%10ld\"\n"), -123456L);
1811 wxPrintf(wxT("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
1812
1813 test_format = wxT("zero-padded string:\t\"%010s\"\n");
1814 wxPrintf(test_format.c_str(), shortstr);
1815 test_format = wxT("left-adjusted Z string:\t\"%-010s\"\n");
1816 wxPrintf(test_format.c_str(), shortstr);
1817 wxPrintf(wxT("space-padded string:\t\"%10s\"\n"), shortstr);
1818 wxPrintf(wxT("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
1819 wxPrintf(wxT("null string:\t\"%s\"\n"), PointerNull);
1820 wxPrintf(wxT("limited string:\t\"%.22s\"\n"), longstr);
1821
1822 wxPrintf(wxT("e-style >= 1:\t\"%e\"\n"), 12.34);
1823 wxPrintf(wxT("e-style >= .1:\t\"%e\"\n"), 0.1234);
1824 wxPrintf(wxT("e-style < .1:\t\"%e\"\n"), 0.001234);
1825 wxPrintf(wxT("e-style big:\t\"%.60e\"\n"), 1e20);
1826 wxPrintf(wxT("e-style == .1:\t\"%e\"\n"), 0.1);
1827 wxPrintf(wxT("f-style >= 1:\t\"%f\"\n"), 12.34);
1828 wxPrintf(wxT("f-style >= .1:\t\"%f\"\n"), 0.1234);
1829 wxPrintf(wxT("f-style < .1:\t\"%f\"\n"), 0.001234);
1830 wxPrintf(wxT("g-style >= 1:\t\"%g\"\n"), 12.34);
1831 wxPrintf(wxT("g-style >= .1:\t\"%g\"\n"), 0.1234);
1832 wxPrintf(wxT("g-style < .1:\t\"%g\"\n"), 0.001234);
1833 wxPrintf(wxT("g-style big:\t\"%.60g\"\n"), 1e20);
1834
1835 wxPrintf (wxT(" %6.5f\n"), .099999999860301614);
1836 wxPrintf (wxT(" %6.5f\n"), .1);
1837 wxPrintf (wxT("x%5.4fx\n"), .5);
1838
1839 wxPrintf (wxT("%#03x\n"), 1);
1840
1841 //wxPrintf (wxT("something really insane: %.10000f\n"), 1.0);
1842
1843 {
1844 double d = FLT_MIN;
1845 int niter = 17;
1846
1847 while (niter-- != 0)
1848 wxPrintf (wxT("%.17e\n"), d / 2);
1849 fflush (stdout);
1850 }
1851
1852 #ifndef __WATCOMC__
1853 // Open Watcom cause compiler error here
1854 // Error! E173: col(24) floating-point constant too small to represent
1855 wxPrintf (wxT("%15.5e\n"), 4.9406564584124654e-324);
1856 #endif
1857
1858 #define FORMAT wxT("|%12.4f|%12.4e|%12.4g|\n")
1859 wxPrintf (FORMAT, 0.0, 0.0, 0.0);
1860 wxPrintf (FORMAT, 1.0, 1.0, 1.0);
1861 wxPrintf (FORMAT, -1.0, -1.0, -1.0);
1862 wxPrintf (FORMAT, 100.0, 100.0, 100.0);
1863 wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
1864 wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
1865 wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
1866 wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
1867 wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
1868 #undef FORMAT
1869
1870 {
1871 wxChar buf[20];
1872 int rc = wxSnprintf (buf, WXSIZEOF(buf), wxT("%30s"), wxT("foo"));
1873
1874 wxPrintf(wxT("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
1875 rc, WXSIZEOF(buf), buf);
1876 #if 0
1877 wxChar buf2[512];
1878 wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
1879 wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
1880 #endif
1881 }
1882
1883 fp_test ();
1884
1885 wxPrintf (wxT("%e should be 1.234568e+06\n"), 1234567.8);
1886 wxPrintf (wxT("%f should be 1234567.800000\n"), 1234567.8);
1887 wxPrintf (wxT("%g should be 1.23457e+06\n"), 1234567.8);
1888 wxPrintf (wxT("%g should be 123.456\n"), 123.456);
1889 wxPrintf (wxT("%g should be 1e+06\n"), 1000000.0);
1890 wxPrintf (wxT("%g should be 10\n"), 10.0);
1891 wxPrintf (wxT("%g should be 0.02\n"), 0.02);
1892
1893 {
1894 double x=1.0;
1895 wxPrintf(wxT("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
1896 }
1897
1898 {
1899 wxChar buf[200];
1900
1901 wxSprintf(buf,wxT("%*s%*s%*s"),-1,wxT("one"),-20,wxT("two"),-30,wxT("three"));
1902
1903 result |= wxStrcmp (buf,
1904 wxT("onetwo three "));
1905
1906 wxPuts (result != 0 ? wxT("Test failed!") : wxT("Test ok."));
1907 }
1908
1909 #ifdef wxLongLong_t
1910 {
1911 wxChar buf[200];
1912
1913 wxSprintf(buf, "%07" wxLongLongFmtSpec "o", wxLL(040000000000));
1914 #if 0
1915 // for some reason below line fails under Borland
1916 wxPrintf (wxT("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
1917 #endif
1918
1919 if (wxStrcmp (buf, wxT("40000000000")) != 0)
1920 {
1921 result = 1;
1922 wxPuts (wxT("\tFAILED"));
1923 }
1924 wxUnusedVar(result);
1925 wxPuts (wxEmptyString);
1926 }
1927 #endif // wxLongLong_t
1928
1929 wxPrintf (wxT("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
1930 wxPrintf (wxT("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
1931
1932 wxPuts (wxT("--- Should be no further output. ---"));
1933 rfg1 ();
1934 rfg2 ();
1935
1936 #if 0
1937 {
1938 wxChar bytes[7];
1939 wxChar buf[20];
1940
1941 memset (bytes, '\xff', sizeof bytes);
1942 wxSprintf (buf, wxT("foo%hhn\n"), &bytes[3]);
1943 if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
1944 || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
1945 {
1946 wxPuts (wxT("%hhn overwrite more bytes"));
1947 result = 1;
1948 }
1949 if (bytes[3] != 3)
1950 {
1951 wxPuts (wxT("%hhn wrote incorrect value"));
1952 result = 1;
1953 }
1954 }
1955 #endif
1956 }
1957
1958 static void
1959 rfg1 (void)
1960 {
1961 wxChar buf[100];
1962
1963 wxSprintf (buf, wxT("%5.s"), wxT("xyz"));
1964 if (wxStrcmp (buf, wxT(" ")) != 0)
1965 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" "));
1966 wxSprintf (buf, wxT("%5.f"), 33.3);
1967 if (wxStrcmp (buf, wxT(" 33")) != 0)
1968 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 33"));
1969 wxSprintf (buf, wxT("%8.e"), 33.3e7);
1970 if (wxStrcmp (buf, wxT(" 3e+08")) != 0)
1971 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 3e+08"));
1972 wxSprintf (buf, wxT("%8.E"), 33.3e7);
1973 if (wxStrcmp (buf, wxT(" 3E+08")) != 0)
1974 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 3E+08"));
1975 wxSprintf (buf, wxT("%.g"), 33.3);
1976 if (wxStrcmp (buf, wxT("3e+01")) != 0)
1977 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3e+01"));
1978 wxSprintf (buf, wxT("%.G"), 33.3);
1979 if (wxStrcmp (buf, wxT("3E+01")) != 0)
1980 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3E+01"));
1981 }
1982
1983 static void
1984 rfg2 (void)
1985 {
1986 int prec;
1987 wxChar buf[100];
1988 wxString test_format;
1989
1990 prec = 0;
1991 wxSprintf (buf, wxT("%.*g"), prec, 3.3);
1992 if (wxStrcmp (buf, wxT("3")) != 0)
1993 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3"));
1994 prec = 0;
1995 wxSprintf (buf, wxT("%.*G"), prec, 3.3);
1996 if (wxStrcmp (buf, wxT("3")) != 0)
1997 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT("3"));
1998 prec = 0;
1999 wxSprintf (buf, wxT("%7.*G"), prec, 3.33);
2000 if (wxStrcmp (buf, wxT(" 3")) != 0)
2001 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 3"));
2002 prec = 3;
2003 test_format = wxT("%04.*o");
2004 wxSprintf (buf, test_format.c_str(), prec, 33);
2005 if (wxStrcmp (buf, wxT(" 041")) != 0)
2006 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 041"));
2007 prec = 7;
2008 test_format = wxT("%09.*u");
2009 wxSprintf (buf, test_format.c_str(), prec, 33);
2010 if (wxStrcmp (buf, wxT(" 0000033")) != 0)
2011 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 0000033"));
2012 prec = 3;
2013 test_format = wxT("%04.*x");
2014 wxSprintf (buf, test_format.c_str(), prec, 33);
2015 if (wxStrcmp (buf, wxT(" 021")) != 0)
2016 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 021"));
2017 prec = 3;
2018 test_format = wxT("%04.*X");
2019 wxSprintf (buf, test_format.c_str(), prec, 33);
2020 if (wxStrcmp (buf, wxT(" 021")) != 0)
2021 wxPrintf (wxT("got: '%s', expected: '%s'\n"), buf, wxT(" 021"));
2022 }
2023
2024 #endif // TEST_PRINTF
2025
2026 // ----------------------------------------------------------------------------
2027 // registry and related stuff
2028 // ----------------------------------------------------------------------------
2029
2030 // this is for MSW only
2031 #ifndef __WXMSW__
2032 #undef TEST_REGCONF
2033 #undef TEST_REGISTRY
2034 #endif
2035
2036 #ifdef TEST_REGCONF
2037
2038 #include "wx/confbase.h"
2039 #include "wx/msw/regconf.h"
2040
2041 #if 0
2042 static void TestRegConfWrite()
2043 {
2044 wxConfig *config = new wxConfig(wxT("myapp"));
2045 config->SetPath(wxT("/group1"));
2046 config->Write(wxT("entry1"), wxT("foo"));
2047 config->SetPath(wxT("/group2"));
2048 config->Write(wxT("entry1"), wxT("bar"));
2049 }
2050 #endif
2051
2052 static void TestRegConfRead()
2053 {
2054 wxRegConfig *config = new wxRegConfig(wxT("myapp"));
2055
2056 wxString str;
2057 long dummy;
2058 config->SetPath(wxT("/"));
2059 wxPuts(wxT("Enumerating / subgroups:"));
2060 bool bCont = config->GetFirstGroup(str, dummy);
2061 while(bCont)
2062 {
2063 wxPuts(str);
2064 bCont = config->GetNextGroup(str, dummy);
2065 }
2066 }
2067
2068 #endif // TEST_REGCONF
2069
2070 #ifdef TEST_REGISTRY
2071
2072 #include "wx/msw/registry.h"
2073
2074 // I chose this one because I liked its name, but it probably only exists under
2075 // NT
2076 static const wxChar *TESTKEY =
2077 wxT("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
2078
2079 static void TestRegistryRead()
2080 {
2081 wxPuts(wxT("*** testing registry reading ***"));
2082
2083 wxRegKey key(TESTKEY);
2084 wxPrintf(wxT("The test key name is '%s'.\n"), key.GetName().c_str());
2085 if ( !key.Open() )
2086 {
2087 wxPuts(wxT("ERROR: test key can't be opened, aborting test."));
2088
2089 return;
2090 }
2091
2092 size_t nSubKeys, nValues;
2093 if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
2094 {
2095 wxPrintf(wxT("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
2096 }
2097
2098 wxPrintf(wxT("Enumerating values:\n"));
2099
2100 long dummy;
2101 wxString value;
2102 bool cont = key.GetFirstValue(value, dummy);
2103 while ( cont )
2104 {
2105 wxPrintf(wxT("Value '%s': type "), value.c_str());
2106 switch ( key.GetValueType(value) )
2107 {
2108 case wxRegKey::Type_None: wxPrintf(wxT("ERROR (none)")); break;
2109 case wxRegKey::Type_String: wxPrintf(wxT("SZ")); break;
2110 case wxRegKey::Type_Expand_String: wxPrintf(wxT("EXPAND_SZ")); break;
2111 case wxRegKey::Type_Binary: wxPrintf(wxT("BINARY")); break;
2112 case wxRegKey::Type_Dword: wxPrintf(wxT("DWORD")); break;
2113 case wxRegKey::Type_Multi_String: wxPrintf(wxT("MULTI_SZ")); break;
2114 default: wxPrintf(wxT("other (unknown)")); break;
2115 }
2116
2117 wxPrintf(wxT(", value = "));
2118 if ( key.IsNumericValue(value) )
2119 {
2120 long val;
2121 key.QueryValue(value, &val);
2122 wxPrintf(wxT("%ld"), val);
2123 }
2124 else // string
2125 {
2126 wxString val;
2127 key.QueryValue(value, val);
2128 wxPrintf(wxT("'%s'"), val.c_str());
2129
2130 key.QueryRawValue(value, val);
2131 wxPrintf(wxT(" (raw value '%s')"), val.c_str());
2132 }
2133
2134 wxPutchar('\n');
2135
2136 cont = key.GetNextValue(value, dummy);
2137 }
2138 }
2139
2140 static void TestRegistryAssociation()
2141 {
2142 /*
2143 The second call to deleteself genertaes an error message, with a
2144 messagebox saying .flo is crucial to system operation, while the .ddf
2145 call also fails, but with no error message
2146 */
2147
2148 wxRegKey key;
2149
2150 key.SetName(wxT("HKEY_CLASSES_ROOT\\.ddf") );
2151 key.Create();
2152 key = wxT("ddxf_auto_file") ;
2153 key.SetName(wxT("HKEY_CLASSES_ROOT\\.flo") );
2154 key.Create();
2155 key = wxT("ddxf_auto_file") ;
2156 key.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2157 key.Create();
2158 key = wxT("program,0") ;
2159 key.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2160 key.Create();
2161 key = wxT("program \"%1\"") ;
2162
2163 key.SetName(wxT("HKEY_CLASSES_ROOT\\.ddf") );
2164 key.DeleteSelf();
2165 key.SetName(wxT("HKEY_CLASSES_ROOT\\.flo") );
2166 key.DeleteSelf();
2167 key.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
2168 key.DeleteSelf();
2169 key.SetName(wxT("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
2170 key.DeleteSelf();
2171 }
2172
2173 #endif // TEST_REGISTRY
2174
2175 // ----------------------------------------------------------------------------
2176 // scope guard
2177 // ----------------------------------------------------------------------------
2178
2179 #ifdef TEST_SCOPEGUARD
2180
2181 #include "wx/scopeguard.h"
2182
2183 static void function0() { puts("function0()"); }
2184 static void function1(int n) { printf("function1(%d)\n", n); }
2185 static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
2186
2187 struct Object
2188 {
2189 void method0() { printf("method0()\n"); }
2190 void method1(int n) { printf("method1(%d)\n", n); }
2191 void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
2192 };
2193
2194 static void TestScopeGuard()
2195 {
2196 wxON_BLOCK_EXIT0(function0);
2197 wxON_BLOCK_EXIT1(function1, 17);
2198 wxON_BLOCK_EXIT2(function2, 3.14, 'p');
2199
2200 Object obj;
2201 wxON_BLOCK_EXIT_OBJ0(obj, Object::method0);
2202 wxON_BLOCK_EXIT_OBJ1(obj, Object::method1, 7);
2203 wxON_BLOCK_EXIT_OBJ2(obj, Object::method2, 2.71, 'e');
2204
2205 wxScopeGuard dismissed = wxMakeGuard(function0);
2206 dismissed.Dismiss();
2207 }
2208
2209 #endif
2210
2211 // ----------------------------------------------------------------------------
2212 // sockets
2213 // ----------------------------------------------------------------------------
2214
2215 #ifdef TEST_SOCKETS
2216
2217 #include "wx/socket.h"
2218 #include "wx/protocol/protocol.h"
2219 #include "wx/protocol/http.h"
2220
2221 static void TestSocketServer()
2222 {
2223 wxPuts(wxT("*** Testing wxSocketServer ***\n"));
2224
2225 static const int PORT = 3000;
2226
2227 wxIPV4address addr;
2228 addr.Service(PORT);
2229
2230 wxSocketServer *server = new wxSocketServer(addr);
2231 if ( !server->Ok() )
2232 {
2233 wxPuts(wxT("ERROR: failed to bind"));
2234
2235 return;
2236 }
2237
2238 bool quit = false;
2239 while ( !quit )
2240 {
2241 wxPrintf(wxT("Server: waiting for connection on port %d...\n"), PORT);
2242
2243 wxSocketBase *socket = server->Accept();
2244 if ( !socket )
2245 {
2246 wxPuts(wxT("ERROR: wxSocketServer::Accept() failed."));
2247 break;
2248 }
2249
2250 wxPuts(wxT("Server: got a client."));
2251
2252 server->SetTimeout(60); // 1 min
2253
2254 bool close = false;
2255 while ( !close && socket->IsConnected() )
2256 {
2257 wxString s;
2258 wxChar ch = wxT('\0');
2259 for ( ;; )
2260 {
2261 if ( socket->Read(&ch, sizeof(ch)).Error() )
2262 {
2263 // don't log error if the client just close the connection
2264 if ( socket->IsConnected() )
2265 {
2266 wxPuts(wxT("ERROR: in wxSocket::Read."));
2267 }
2268
2269 break;
2270 }
2271
2272 if ( ch == '\r' )
2273 continue;
2274
2275 if ( ch == '\n' )
2276 break;
2277
2278 s += ch;
2279 }
2280
2281 if ( ch != '\n' )
2282 {
2283 break;
2284 }
2285
2286 wxPrintf(wxT("Server: got '%s'.\n"), s.c_str());
2287 if ( s == wxT("close") )
2288 {
2289 wxPuts(wxT("Closing connection"));
2290
2291 close = true;
2292 }
2293 else if ( s == wxT("quit") )
2294 {
2295 close =
2296 quit = true;
2297
2298 wxPuts(wxT("Shutting down the server"));
2299 }
2300 else // not a special command
2301 {
2302 socket->Write(s.MakeUpper().c_str(), s.length());
2303 socket->Write("\r\n", 2);
2304 wxPrintf(wxT("Server: wrote '%s'.\n"), s.c_str());
2305 }
2306 }
2307
2308 if ( !close )
2309 {
2310 wxPuts(wxT("Server: lost a client unexpectedly."));
2311 }
2312
2313 socket->Destroy();
2314 }
2315
2316 // same as "delete server" but is consistent with GUI programs
2317 server->Destroy();
2318 }
2319
2320 static void TestSocketClient()
2321 {
2322 wxPuts(wxT("*** Testing wxSocketClient ***\n"));
2323
2324 static const wxChar *hostname = wxT("www.wxwidgets.org");
2325
2326 wxIPV4address addr;
2327 addr.Hostname(hostname);
2328 addr.Service(80);
2329
2330 wxPrintf(wxT("--- Attempting to connect to %s:80...\n"), hostname);
2331
2332 wxSocketClient client;
2333 if ( !client.Connect(addr) )
2334 {
2335 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
2336 }
2337 else
2338 {
2339 wxPrintf(wxT("--- Connected to %s:%u...\n"),
2340 addr.Hostname().c_str(), addr.Service());
2341
2342 wxChar buf[8192];
2343
2344 // could use simply "GET" here I suppose
2345 wxString cmdGet =
2346 wxString::Format(wxT("GET http://%s/\r\n"), hostname);
2347 client.Write(cmdGet, cmdGet.length());
2348 wxPrintf(wxT("--- Sent command '%s' to the server\n"),
2349 MakePrintable(cmdGet).c_str());
2350 client.Read(buf, WXSIZEOF(buf));
2351 wxPrintf(wxT("--- Server replied:\n%s"), buf);
2352 }
2353 }
2354
2355 #endif // TEST_SOCKETS
2356
2357 // ----------------------------------------------------------------------------
2358 // FTP
2359 // ----------------------------------------------------------------------------
2360
2361 #ifdef TEST_FTP
2362
2363 #include "wx/protocol/ftp.h"
2364 #include "wx/protocol/log.h"
2365
2366 #define FTP_ANONYMOUS
2367
2368 static wxFTP *ftp;
2369
2370 #ifdef FTP_ANONYMOUS
2371 static const wxChar *directory = wxT("/pub");
2372 static const wxChar *filename = wxT("welcome.msg");
2373 #else
2374 static const wxChar *directory = wxT("/etc");
2375 static const wxChar *filename = wxT("issue");
2376 #endif
2377
2378 static bool TestFtpConnect()
2379 {
2380 wxPuts(wxT("*** Testing FTP connect ***"));
2381
2382 #ifdef FTP_ANONYMOUS
2383 static const wxChar *hostname = wxT("ftp.wxwidgets.org");
2384
2385 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
2386 #else // !FTP_ANONYMOUS
2387 static const wxChar *hostname = "localhost";
2388
2389 wxChar user[256];
2390 wxFgets(user, WXSIZEOF(user), stdin);
2391 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
2392 ftp->SetUser(user);
2393
2394 wxChar password[256];
2395 wxPrintf(wxT("Password for %s: "), password);
2396 wxFgets(password, WXSIZEOF(password), stdin);
2397 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
2398 ftp->SetPassword(password);
2399
2400 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
2401 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
2402
2403 if ( !ftp->Connect(hostname) )
2404 {
2405 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
2406
2407 return false;
2408 }
2409 else
2410 {
2411 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
2412 hostname, ftp->Pwd().c_str());
2413 ftp->Close();
2414 }
2415
2416 return true;
2417 }
2418
2419 static void TestFtpList()
2420 {
2421 wxPuts(wxT("*** Testing wxFTP file listing ***\n"));
2422
2423 // test CWD
2424 if ( !ftp->ChDir(directory) )
2425 {
2426 wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory);
2427 }
2428
2429 wxPrintf(wxT("Current directory is '%s'\n"), ftp->Pwd().c_str());
2430
2431 // test NLIST and LIST
2432 wxArrayString files;
2433 if ( !ftp->GetFilesList(files) )
2434 {
2435 wxPuts(wxT("ERROR: failed to get NLIST of files"));
2436 }
2437 else
2438 {
2439 wxPrintf(wxT("Brief list of files under '%s':\n"), ftp->Pwd().c_str());
2440 size_t count = files.GetCount();
2441 for ( size_t n = 0; n < count; n++ )
2442 {
2443 wxPrintf(wxT("\t%s\n"), files[n].c_str());
2444 }
2445 wxPuts(wxT("End of the file list"));
2446 }
2447
2448 if ( !ftp->GetDirList(files) )
2449 {
2450 wxPuts(wxT("ERROR: failed to get LIST of files"));
2451 }
2452 else
2453 {
2454 wxPrintf(wxT("Detailed list of files under '%s':\n"), ftp->Pwd().c_str());
2455 size_t count = files.GetCount();
2456 for ( size_t n = 0; n < count; n++ )
2457 {
2458 wxPrintf(wxT("\t%s\n"), files[n].c_str());
2459 }
2460 wxPuts(wxT("End of the file list"));
2461 }
2462
2463 if ( !ftp->ChDir(wxT("..")) )
2464 {
2465 wxPuts(wxT("ERROR: failed to cd to .."));
2466 }
2467
2468 wxPrintf(wxT("Current directory is '%s'\n"), ftp->Pwd().c_str());
2469 }
2470
2471 static void TestFtpDownload()
2472 {
2473 wxPuts(wxT("*** Testing wxFTP download ***\n"));
2474
2475 // test RETR
2476 wxInputStream *in = ftp->GetInputStream(filename);
2477 if ( !in )
2478 {
2479 wxPrintf(wxT("ERROR: couldn't get input stream for %s\n"), filename);
2480 }
2481 else
2482 {
2483 size_t size = in->GetSize();
2484 wxPrintf(wxT("Reading file %s (%u bytes)..."), filename, size);
2485 fflush(stdout);
2486
2487 wxChar *data = new wxChar[size];
2488 if ( !in->Read(data, size) )
2489 {
2490 wxPuts(wxT("ERROR: read error"));
2491 }
2492 else
2493 {
2494 wxPrintf(wxT("\nContents of %s:\n%s\n"), filename, data);
2495 }
2496
2497 delete [] data;
2498 delete in;
2499 }
2500 }
2501
2502 static void TestFtpFileSize()
2503 {
2504 wxPuts(wxT("*** Testing FTP SIZE command ***"));
2505
2506 if ( !ftp->ChDir(directory) )
2507 {
2508 wxPrintf(wxT("ERROR: failed to cd to %s\n"), directory);
2509 }
2510
2511 wxPrintf(wxT("Current directory is '%s'\n"), ftp->Pwd().c_str());
2512
2513 if ( ftp->FileExists(filename) )
2514 {
2515 int size = ftp->GetFileSize(filename);
2516 if ( size == -1 )
2517 wxPrintf(wxT("ERROR: couldn't get size of '%s'\n"), filename);
2518 else
2519 wxPrintf(wxT("Size of '%s' is %d bytes.\n"), filename, size);
2520 }
2521 else
2522 {
2523 wxPrintf(wxT("ERROR: '%s' doesn't exist\n"), filename);
2524 }
2525 }
2526
2527 static void TestFtpMisc()
2528 {
2529 wxPuts(wxT("*** Testing miscellaneous wxFTP functions ***"));
2530
2531 if ( ftp->SendCommand(wxT("STAT")) != '2' )
2532 {
2533 wxPuts(wxT("ERROR: STAT failed"));
2534 }
2535 else
2536 {
2537 wxPrintf(wxT("STAT returned:\n\n%s\n"), ftp->GetLastResult().c_str());
2538 }
2539
2540 if ( ftp->SendCommand(wxT("HELP SITE")) != '2' )
2541 {
2542 wxPuts(wxT("ERROR: HELP SITE failed"));
2543 }
2544 else
2545 {
2546 wxPrintf(wxT("The list of site-specific commands:\n\n%s\n"),
2547 ftp->GetLastResult().c_str());
2548 }
2549 }
2550
2551 #if TEST_INTERACTIVE
2552
2553 static void TestFtpInteractive()
2554 {
2555 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
2556
2557 wxChar buf[128];
2558
2559 for ( ;; )
2560 {
2561 wxPrintf(wxT("Enter FTP command: "));
2562 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
2563 break;
2564
2565 // kill the last '\n'
2566 buf[wxStrlen(buf) - 1] = 0;
2567
2568 // special handling of LIST and NLST as they require data connection
2569 wxString start(buf, 4);
2570 start.MakeUpper();
2571 if ( start == wxT("LIST") || start == wxT("NLST") )
2572 {
2573 wxString wildcard;
2574 if ( wxStrlen(buf) > 4 )
2575 wildcard = buf + 5;
2576
2577 wxArrayString files;
2578 if ( !ftp->GetList(files, wildcard, start == wxT("LIST")) )
2579 {
2580 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start.c_str());
2581 }
2582 else
2583 {
2584 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
2585 start.c_str(), wildcard.c_str(), ftp->Pwd().c_str());
2586 size_t count = files.GetCount();
2587 for ( size_t n = 0; n < count; n++ )
2588 {
2589 wxPrintf(wxT("\t%s\n"), files[n].c_str());
2590 }
2591 wxPuts(wxT("--- End of the file list"));
2592 }
2593 }
2594 else // !list
2595 {
2596 wxChar ch = ftp->SendCommand(buf);
2597 wxPrintf(wxT("Command %s"), ch ? wxT("succeeded") : wxT("failed"));
2598 if ( ch )
2599 {
2600 wxPrintf(wxT(" (return code %c)"), ch);
2601 }
2602
2603 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp->GetLastResult().c_str());
2604 }
2605 }
2606
2607 wxPuts(wxT("\n*** done ***"));
2608 }
2609
2610 #endif // TEST_INTERACTIVE
2611
2612 static void TestFtpUpload()
2613 {
2614 wxPuts(wxT("*** Testing wxFTP uploading ***\n"));
2615
2616 // upload a file
2617 static const wxChar *file1 = wxT("test1");
2618 static const wxChar *file2 = wxT("test2");
2619 wxOutputStream *out = ftp->GetOutputStream(file1);
2620 if ( out )
2621 {
2622 wxPrintf(wxT("--- Uploading to %s ---\n"), file1);
2623 out->Write("First hello", 11);
2624 delete out;
2625 }
2626
2627 // send a command to check the remote file
2628 if ( ftp->SendCommand(wxString(wxT("STAT ")) + file1) != '2' )
2629 {
2630 wxPrintf(wxT("ERROR: STAT %s failed\n"), file1);
2631 }
2632 else
2633 {
2634 wxPrintf(wxT("STAT %s returned:\n\n%s\n"),
2635 file1, ftp->GetLastResult().c_str());
2636 }
2637
2638 out = ftp->GetOutputStream(file2);
2639 if ( out )
2640 {
2641 wxPrintf(wxT("--- Uploading to %s ---\n"), file1);
2642 out->Write("Second hello", 12);
2643 delete out;
2644 }
2645 }
2646
2647 #endif // TEST_FTP
2648
2649 // ----------------------------------------------------------------------------
2650 // stack backtrace
2651 // ----------------------------------------------------------------------------
2652
2653 #ifdef TEST_STACKWALKER
2654
2655 #if wxUSE_STACKWALKER
2656
2657 #include "wx/stackwalk.h"
2658
2659 class StackDump : public wxStackWalker
2660 {
2661 public:
2662 StackDump(const char *argv0)
2663 : wxStackWalker(argv0)
2664 {
2665 }
2666
2667 virtual void Walk(size_t skip = 1, size_t maxdepth = wxSTACKWALKER_MAX_DEPTH)
2668 {
2669 wxPuts(wxT("Stack dump:"));
2670
2671 wxStackWalker::Walk(skip, maxdepth);
2672 }
2673
2674 protected:
2675 virtual void OnStackFrame(const wxStackFrame& frame)
2676 {
2677 printf("[%2d] ", (int) frame.GetLevel());
2678
2679 wxString name = frame.GetName();
2680 if ( !name.empty() )
2681 {
2682 printf("%-20.40s", (const char*)name.mb_str());
2683 }
2684 else
2685 {
2686 printf("0x%08lx", (unsigned long)frame.GetAddress());
2687 }
2688
2689 if ( frame.HasSourceLocation() )
2690 {
2691 printf("\t%s:%d",
2692 (const char*)frame.GetFileName().mb_str(),
2693 (int)frame.GetLine());
2694 }
2695
2696 puts("");
2697
2698 wxString type, val;
2699 for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ )
2700 {
2701 printf("\t%s %s = %s\n", (const char*)type.mb_str(),
2702 (const char*)name.mb_str(),
2703 (const char*)val.mb_str());
2704 }
2705 }
2706 };
2707
2708 static void TestStackWalk(const char *argv0)
2709 {
2710 wxPuts(wxT("*** Testing wxStackWalker ***\n"));
2711
2712 StackDump dump(argv0);
2713 dump.Walk();
2714 }
2715
2716 #endif // wxUSE_STACKWALKER
2717
2718 #endif // TEST_STACKWALKER
2719
2720 // ----------------------------------------------------------------------------
2721 // standard paths
2722 // ----------------------------------------------------------------------------
2723
2724 #ifdef TEST_STDPATHS
2725
2726 #include "wx/stdpaths.h"
2727 #include "wx/wxchar.h" // wxPrintf
2728
2729 static void TestStandardPaths()
2730 {
2731 wxPuts(wxT("*** Testing wxStandardPaths ***\n"));
2732
2733 wxTheApp->SetAppName(wxT("console"));
2734
2735 wxStandardPathsBase& stdp = wxStandardPaths::Get();
2736 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
2737 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
2738 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
2739 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
2740 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
2741 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
2742 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
2743 wxPrintf(wxT("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
2744 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
2745 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
2746 wxPrintf(wxT("Localized res. dir:\t%s\n"),
2747 stdp.GetLocalizedResourcesDir(wxT("fr")).c_str());
2748 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
2749 stdp.GetLocalizedResourcesDir
2750 (
2751 wxT("fr"),
2752 wxStandardPaths::ResourceCat_Messages
2753 ).c_str());
2754 }
2755
2756 #endif // TEST_STDPATHS
2757
2758 // ----------------------------------------------------------------------------
2759 // streams
2760 // ----------------------------------------------------------------------------
2761
2762 #ifdef TEST_STREAMS
2763
2764 #include "wx/wfstream.h"
2765 #include "wx/mstream.h"
2766
2767 static void TestFileStream()
2768 {
2769 wxPuts(wxT("*** Testing wxFileInputStream ***"));
2770
2771 static const wxString filename = wxT("testdata.fs");
2772 {
2773 wxFileOutputStream fsOut(filename);
2774 fsOut.Write("foo", 3);
2775 }
2776
2777 {
2778 wxFileInputStream fsIn(filename);
2779 wxPrintf(wxT("File stream size: %u\n"), fsIn.GetSize());
2780 int c;
2781 while ( (c=fsIn.GetC()) != wxEOF )
2782 {
2783 wxPutchar(c);
2784 }
2785 }
2786
2787 if ( !wxRemoveFile(filename) )
2788 {
2789 wxPrintf(wxT("ERROR: failed to remove the file '%s'.\n"), filename.c_str());
2790 }
2791
2792 wxPuts(wxT("\n*** wxFileInputStream test done ***"));
2793 }
2794
2795 static void TestMemoryStream()
2796 {
2797 wxPuts(wxT("*** Testing wxMemoryOutputStream ***"));
2798
2799 wxMemoryOutputStream memOutStream;
2800 wxPrintf(wxT("Initially out stream offset: %lu\n"),
2801 (unsigned long)memOutStream.TellO());
2802
2803 for ( const wxChar *p = wxT("Hello, stream!"); *p; p++ )
2804 {
2805 memOutStream.PutC(*p);
2806 }
2807
2808 wxPrintf(wxT("Final out stream offset: %lu\n"),
2809 (unsigned long)memOutStream.TellO());
2810
2811 wxPuts(wxT("*** Testing wxMemoryInputStream ***"));
2812
2813 wxChar buf[1024];
2814 size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
2815
2816 wxMemoryInputStream memInpStream(buf, len);
2817 wxPrintf(wxT("Memory stream size: %u\n"), memInpStream.GetSize());
2818 int c;
2819 while ( (c=memInpStream.GetC()) != wxEOF )
2820 {
2821 wxPutchar(c);
2822 }
2823
2824 wxPuts(wxT("\n*** wxMemoryInputStream test done ***"));
2825 }
2826
2827 #endif // TEST_STREAMS
2828
2829 // ----------------------------------------------------------------------------
2830 // wxVolume tests
2831 // ----------------------------------------------------------------------------
2832
2833 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
2834 #undef TEST_VOLUME
2835 #endif
2836
2837 #ifdef TEST_VOLUME
2838
2839 #include "wx/volume.h"
2840
2841 static const wxChar *volumeKinds[] =
2842 {
2843 wxT("floppy"),
2844 wxT("hard disk"),
2845 wxT("CD-ROM"),
2846 wxT("DVD-ROM"),
2847 wxT("network volume"),
2848 wxT("other volume"),
2849 };
2850
2851 static void TestFSVolume()
2852 {
2853 wxPuts(wxT("*** Testing wxFSVolume class ***"));
2854
2855 wxArrayString volumes = wxFSVolume::GetVolumes();
2856 size_t count = volumes.GetCount();
2857
2858 if ( !count )
2859 {
2860 wxPuts(wxT("ERROR: no mounted volumes?"));
2861 return;
2862 }
2863
2864 wxPrintf(wxT("%u mounted volumes found:\n"), count);
2865
2866 for ( size_t n = 0; n < count; n++ )
2867 {
2868 wxFSVolume vol(volumes[n]);
2869 if ( !vol.IsOk() )
2870 {
2871 wxPuts(wxT("ERROR: couldn't create volume"));
2872 continue;
2873 }
2874
2875 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
2876 n + 1,
2877 vol.GetDisplayName().c_str(),
2878 vol.GetName().c_str(),
2879 volumeKinds[vol.GetKind()],
2880 vol.IsWritable() ? wxT("rw") : wxT("ro"),
2881 vol.GetFlags() & wxFS_VOL_REMOVABLE ? wxT("removable")
2882 : wxT("fixed"));
2883 }
2884 }
2885
2886 #endif // TEST_VOLUME
2887
2888 // ----------------------------------------------------------------------------
2889 // date time
2890 // ----------------------------------------------------------------------------
2891
2892 #ifdef TEST_DATETIME
2893
2894 #include "wx/math.h"
2895 #include "wx/datetime.h"
2896
2897 #if TEST_INTERACTIVE
2898
2899 static void TestDateTimeInteractive()
2900 {
2901 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
2902
2903 wxChar buf[128];
2904
2905 for ( ;; )
2906 {
2907 wxPrintf(wxT("Enter a date: "));
2908 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
2909 break;
2910
2911 // kill the last '\n'
2912 buf[wxStrlen(buf) - 1] = 0;
2913
2914 wxDateTime dt;
2915 const wxChar *p = dt.ParseDate(buf);
2916 if ( !p )
2917 {
2918 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf);
2919
2920 continue;
2921 }
2922 else if ( *p )
2923 {
2924 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p - buf);
2925 }
2926
2927 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
2928 dt.Format(wxT("%b %d, %Y")).c_str(),
2929 dt.GetDayOfYear(),
2930 dt.GetWeekOfMonth(wxDateTime::Monday_First),
2931 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
2932 dt.GetWeekOfYear(wxDateTime::Monday_First));
2933 }
2934
2935 wxPuts(wxT("\n*** done ***"));
2936 }
2937
2938 #endif // TEST_INTERACTIVE
2939 #endif // TEST_DATETIME
2940
2941 // ----------------------------------------------------------------------------
2942 // entry point
2943 // ----------------------------------------------------------------------------
2944
2945 #ifdef TEST_SNGLINST
2946 #include "wx/snglinst.h"
2947 #endif // TEST_SNGLINST
2948
2949 int main(int argc, char **argv)
2950 {
2951 #if wxUSE_UNICODE
2952 wxChar **wxArgv = new wxChar *[argc + 1];
2953
2954 {
2955 int n;
2956
2957 for (n = 0; n < argc; n++ )
2958 {
2959 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]);
2960 wxArgv[n] = wxStrdup(warg);
2961 }
2962
2963 wxArgv[n] = NULL;
2964 }
2965 #else // !wxUSE_UNICODE
2966 #define wxArgv argv
2967 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2968
2969 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
2970
2971 wxInitializer initializer;
2972 if ( !initializer )
2973 {
2974 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
2975
2976 return -1;
2977 }
2978
2979 #ifdef TEST_SNGLINST
2980 wxSingleInstanceChecker checker;
2981 if ( checker.Create(wxT(".wxconsole.lock")) )
2982 {
2983 if ( checker.IsAnotherRunning() )
2984 {
2985 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
2986
2987 return 1;
2988 }
2989
2990 // wait some time to give time to launch another instance
2991 wxPrintf(wxT("Press \"Enter\" to continue..."));
2992 wxFgetc(stdin);
2993 }
2994 else // failed to create
2995 {
2996 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
2997 }
2998 #endif // TEST_SNGLINST
2999
3000 #ifdef TEST_CMDLINE
3001 TestCmdLineConvert();
3002
3003 #if wxUSE_CMDLINE_PARSER
3004 static const wxCmdLineEntryDesc cmdLineDesc[] =
3005 {
3006 { wxCMD_LINE_SWITCH, "h", "help", "show this help message",
3007 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
3008 { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" },
3009 { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" },
3010
3011 { wxCMD_LINE_OPTION, "o", "output", "output file" },
3012 { wxCMD_LINE_OPTION, "i", "input", "input dir" },
3013 { wxCMD_LINE_OPTION, "s", "size", "output block size",
3014 wxCMD_LINE_VAL_NUMBER },
3015 { wxCMD_LINE_OPTION, "d", "date", "output file date",
3016 wxCMD_LINE_VAL_DATE },
3017 { wxCMD_LINE_OPTION, "f", "double", "output double",
3018 wxCMD_LINE_VAL_DOUBLE },
3019
3020 { wxCMD_LINE_PARAM, NULL, NULL, "input file",
3021 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE },
3022
3023 { wxCMD_LINE_NONE }
3024 };
3025
3026 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
3027
3028 parser.AddOption(wxT("project_name"), wxT(""), wxT("full path to project file"),
3029 wxCMD_LINE_VAL_STRING,
3030 wxCMD_LINE_OPTION_MANDATORY | wxCMD_LINE_NEEDS_SEPARATOR);
3031
3032 switch ( parser.Parse() )
3033 {
3034 case -1:
3035 wxLogMessage(wxT("Help was given, terminating."));
3036 break;
3037
3038 case 0:
3039 ShowCmdLine(parser);
3040 break;
3041
3042 default:
3043 wxLogMessage(wxT("Syntax error detected, aborting."));
3044 break;
3045 }
3046 #endif // wxUSE_CMDLINE_PARSER
3047
3048 #endif // TEST_CMDLINE
3049
3050 #ifdef TEST_DIR
3051 #if TEST_ALL
3052 TestDirExists();
3053 TestDirEnum();
3054 #endif
3055 TestDirTraverse();
3056 #endif // TEST_DIR
3057
3058 #ifdef TEST_DYNLIB
3059 TestDllLoad();
3060 TestDllListLoaded();
3061 #endif // TEST_DYNLIB
3062
3063 #ifdef TEST_ENVIRON
3064 TestEnvironment();
3065 #endif // TEST_ENVIRON
3066
3067 #ifdef TEST_FILECONF
3068 TestFileConfRead();
3069 #endif // TEST_FILECONF
3070
3071 #ifdef TEST_LOCALE
3072 TestDefaultLang();
3073 #endif // TEST_LOCALE
3074
3075 #ifdef TEST_LOG
3076 wxPuts(wxT("*** Testing wxLog ***"));
3077
3078 wxString s;
3079 for ( size_t n = 0; n < 8000; n++ )
3080 {
3081 s << (wxChar)(wxT('A') + (n % 26));
3082 }
3083
3084 wxLogWarning(wxT("The length of the string is %lu"),
3085 (unsigned long)s.length());
3086
3087 wxString msg;
3088 msg.Printf(wxT("A very very long message: '%s', the end!\n"), s.c_str());
3089
3090 // this one shouldn't be truncated
3091 wxPrintf(msg);
3092
3093 // but this one will because log functions use fixed size buffer
3094 // (note that it doesn't need '\n' at the end neither - will be added
3095 // by wxLog anyhow)
3096 wxLogMessage(wxT("A very very long message 2: '%s', the end!"), s.c_str());
3097 #endif // TEST_LOG
3098
3099 #ifdef TEST_FILE
3100 TestFileRead();
3101 TestTextFileRead();
3102 TestFileCopy();
3103 TestTempFile();
3104 #endif // TEST_FILE
3105
3106 #ifdef TEST_FILENAME
3107 TestFileNameTemp();
3108 TestFileNameCwd();
3109 TestFileNameDirManip();
3110 TestFileNameComparison();
3111 TestFileNameOperations();
3112 #endif // TEST_FILENAME
3113
3114 #ifdef TEST_FILETIME
3115 TestFileGetTimes();
3116 #if 0
3117 TestFileSetTimes();
3118 #endif
3119 #endif // TEST_FILETIME
3120
3121 #ifdef TEST_FTP
3122 wxLog::AddTraceMask(FTP_TRACE_MASK);
3123
3124 // wxFTP cannot be a static variable as its ctor needs to access
3125 // wxWidgets internals after it has been initialized
3126 ftp = new wxFTP;
3127 ftp->SetLog(new wxProtocolLog(FTP_TRACE_MASK));
3128
3129 if ( TestFtpConnect() )
3130 {
3131 #if TEST_ALL
3132 TestFtpList();
3133 TestFtpDownload();
3134 TestFtpMisc();
3135 TestFtpFileSize();
3136 TestFtpUpload();
3137 #endif // TEST_ALL
3138
3139 #if TEST_INTERACTIVE
3140 //TestFtpInteractive();
3141 #endif
3142 }
3143 //else: connecting to the FTP server failed
3144
3145 delete ftp;
3146 #endif // TEST_FTP
3147
3148 #ifdef TEST_MIME
3149 //wxLog::AddTraceMask(wxT("mime"));
3150 TestMimeEnum();
3151 #if 0
3152 TestMimeOverride();
3153 TestMimeAssociate();
3154 #endif
3155 TestMimeFilename();
3156 #endif // TEST_MIME
3157
3158 #ifdef TEST_INFO_FUNCTIONS
3159 TestOsInfo();
3160 TestPlatformInfo();
3161 TestUserInfo();
3162
3163 #if TEST_INTERACTIVE
3164 TestDiskInfo();
3165 #endif
3166 #endif // TEST_INFO_FUNCTIONS
3167
3168 #ifdef TEST_PATHLIST
3169 TestPathList();
3170 #endif // TEST_PATHLIST
3171
3172 #ifdef TEST_PRINTF
3173 TestPrintf();
3174 #endif // TEST_PRINTF
3175
3176 #ifdef TEST_REGCONF
3177 #if 0
3178 TestRegConfWrite();
3179 #endif
3180 TestRegConfRead();
3181 #endif // TEST_REGCONF
3182
3183 #if defined TEST_REGEX && TEST_INTERACTIVE
3184 TestRegExInteractive();
3185 #endif // defined TEST_REGEX && TEST_INTERACTIVE
3186
3187 #ifdef TEST_REGISTRY
3188 TestRegistryRead();
3189 TestRegistryAssociation();
3190 #endif // TEST_REGISTRY
3191
3192 #ifdef TEST_SOCKETS
3193 TestSocketServer();
3194 TestSocketClient();
3195 #endif // TEST_SOCKETS
3196
3197 #ifdef TEST_STREAMS
3198 #if TEST_ALL
3199 TestFileStream();
3200 #endif
3201 TestMemoryStream();
3202 #endif // TEST_STREAMS
3203
3204 #ifdef TEST_DATETIME
3205 #if TEST_INTERACTIVE
3206 TestDateTimeInteractive();
3207 #endif
3208 #endif // TEST_DATETIME
3209
3210 #ifdef TEST_SCOPEGUARD
3211 TestScopeGuard();
3212 #endif
3213
3214 #ifdef TEST_STACKWALKER
3215 #if wxUSE_STACKWALKER
3216 TestStackWalk(argv[0]);
3217 #endif
3218 #endif // TEST_STACKWALKER
3219
3220 #ifdef TEST_STDPATHS
3221 TestStandardPaths();
3222 #endif
3223
3224 #ifdef TEST_USLEEP
3225 wxPuts(wxT("Sleeping for 3 seconds... z-z-z-z-z..."));
3226 wxUsleep(3000);
3227 #endif // TEST_USLEEP
3228
3229 #ifdef TEST_VOLUME
3230 TestFSVolume();
3231 #endif // TEST_VOLUME
3232
3233 #if wxUSE_UNICODE
3234 {
3235 for ( int n = 0; n < argc; n++ )
3236 free(wxArgv[n]);
3237
3238 delete [] wxArgv;
3239 }
3240 #endif // wxUSE_UNICODE
3241
3242 wxUnusedVar(argc);
3243 wxUnusedVar(argv);
3244 return 0;
3245 }