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