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