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