moved non-interactive tests for wxDynamicLibrary, wxGet/SetEnv, wxTempFile, wxCopyFil...
[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 #else // #if TEST_ALL
110 #define TEST_DATETIME
111 #define TEST_VOLUME
112 #define TEST_STDPATHS
113 #define TEST_STACKWALKER
114 #define TEST_FTP
115 #define TEST_SNGLINST
116 #define TEST_REGEX
117 #define TEST_INFO_FUNCTIONS
118 #define TEST_MIME
119 #define TEST_DYNLIB
120 #endif
121
122 // some tests are interactive, define this to run them
123 #ifdef TEST_INTERACTIVE
124 #undef TEST_INTERACTIVE
125
126 #define TEST_INTERACTIVE 1
127 #else
128 #define TEST_INTERACTIVE 1
129 #endif
130
131 // ============================================================================
132 // implementation
133 // ============================================================================
134
135 // ----------------------------------------------------------------------------
136 // wxDir
137 // ----------------------------------------------------------------------------
138
139 #ifdef TEST_DIR
140
141 #include "wx/dir.h"
142
143 #ifdef __UNIX__
144 static const wxChar *ROOTDIR = wxT("/");
145 static const wxChar *TESTDIR = wxT("/usr/local/share");
146 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
147 static const wxChar *ROOTDIR = wxT("c:\\");
148 static const wxChar *TESTDIR = wxT("d:\\");
149 #else
150 #error "don't know where the root directory is"
151 #endif
152
153 static void TestDirEnumHelper(wxDir& dir,
154 int flags = wxDIR_DEFAULT,
155 const wxString& filespec = wxEmptyString)
156 {
157 wxString filename;
158
159 if ( !dir.IsOpened() )
160 return;
161
162 bool cont = dir.GetFirst(&filename, filespec, flags);
163 while ( cont )
164 {
165 wxPrintf(wxT("\t%s\n"), filename.c_str());
166
167 cont = dir.GetNext(&filename);
168 }
169
170 wxPuts(wxEmptyString);
171 }
172
173 #if TEST_ALL
174
175 static void TestDirEnum()
176 {
177 wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
178
179 wxString cwd = wxGetCwd();
180 if ( !wxDir::Exists(cwd) )
181 {
182 wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd.c_str());
183 return;
184 }
185
186 wxDir dir(cwd);
187 if ( !dir.IsOpened() )
188 {
189 wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd.c_str());
190 return;
191 }
192
193 wxPuts(wxT("Enumerating everything in current directory:"));
194 TestDirEnumHelper(dir);
195
196 wxPuts(wxT("Enumerating really everything in current directory:"));
197 TestDirEnumHelper(dir, wxDIR_DEFAULT | wxDIR_DOTDOT);
198
199 wxPuts(wxT("Enumerating object files in current directory:"));
200 TestDirEnumHelper(dir, wxDIR_DEFAULT, wxT("*.o*"));
201
202 wxPuts(wxT("Enumerating directories in current directory:"));
203 TestDirEnumHelper(dir, wxDIR_DIRS);
204
205 wxPuts(wxT("Enumerating files in current directory:"));
206 TestDirEnumHelper(dir, wxDIR_FILES);
207
208 wxPuts(wxT("Enumerating files including hidden in current directory:"));
209 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
210
211 dir.Open(ROOTDIR);
212
213 wxPuts(wxT("Enumerating everything in root directory:"));
214 TestDirEnumHelper(dir, wxDIR_DEFAULT);
215
216 wxPuts(wxT("Enumerating directories in root directory:"));
217 TestDirEnumHelper(dir, wxDIR_DIRS);
218
219 wxPuts(wxT("Enumerating files in root directory:"));
220 TestDirEnumHelper(dir, wxDIR_FILES);
221
222 wxPuts(wxT("Enumerating files including hidden in root directory:"));
223 TestDirEnumHelper(dir, wxDIR_FILES | wxDIR_HIDDEN);
224
225 wxPuts(wxT("Enumerating files in non existing directory:"));
226 wxDir dirNo(wxT("nosuchdir"));
227 TestDirEnumHelper(dirNo);
228 }
229
230 #endif // TEST_ALL
231
232 class DirPrintTraverser : public wxDirTraverser
233 {
234 public:
235 virtual wxDirTraverseResult OnFile(const wxString& WXUNUSED(filename))
236 {
237 return wxDIR_CONTINUE;
238 }
239
240 virtual wxDirTraverseResult OnDir(const wxString& dirname)
241 {
242 wxString path, name, ext;
243 wxFileName::SplitPath(dirname, &path, &name, &ext);
244
245 if ( !ext.empty() )
246 name << wxT('.') << ext;
247
248 wxString indent;
249 for ( const wxChar *p = path.c_str(); *p; p++ )
250 {
251 if ( wxIsPathSeparator(*p) )
252 indent += wxT(" ");
253 }
254
255 wxPrintf(wxT("%s%s\n"), indent.c_str(), name.c_str());
256
257 return wxDIR_CONTINUE;
258 }
259 };
260
261 static void TestDirTraverse()
262 {
263 wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
264
265 // enum all files
266 wxArrayString files;
267 size_t n = wxDir::GetAllFiles(TESTDIR, &files);
268 wxPrintf(wxT("There are %u files under '%s'\n"), n, TESTDIR);
269 if ( n > 1 )
270 {
271 wxPrintf(wxT("First one is '%s'\n"), files[0u].c_str());
272 wxPrintf(wxT(" last one is '%s'\n"), files[n - 1].c_str());
273 }
274
275 // enum again with custom traverser
276 wxPuts(wxT("Now enumerating directories:"));
277 wxDir dir(TESTDIR);
278 DirPrintTraverser traverser;
279 dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN);
280 }
281
282 #if TEST_ALL
283
284 static void TestDirExists()
285 {
286 wxPuts(wxT("*** Testing wxDir::Exists() ***"));
287
288 static const wxChar *dirnames[] =
289 {
290 wxT("."),
291 #if defined(__WXMSW__)
292 wxT("c:"),
293 wxT("c:\\"),
294 wxT("\\\\share\\file"),
295 wxT("c:\\dos"),
296 wxT("c:\\dos\\"),
297 wxT("c:\\dos\\\\"),
298 wxT("c:\\autoexec.bat"),
299 #elif defined(__UNIX__)
300 wxT("/"),
301 wxT("//"),
302 wxT("/usr/bin"),
303 wxT("/usr//bin"),
304 wxT("/usr///bin"),
305 #endif
306 };
307
308 for ( size_t n = 0; n < WXSIZEOF(dirnames); n++ )
309 {
310 wxPrintf(wxT("%-40s: %s\n"),
311 dirnames[n],
312 wxDir::Exists(dirnames[n]) ? wxT("exists")
313 : wxT("doesn't exist"));
314 }
315 }
316
317 #endif // TEST_ALL
318
319 #endif // TEST_DIR
320
321 // ----------------------------------------------------------------------------
322 // wxDllLoader
323 // ----------------------------------------------------------------------------
324
325 #ifdef TEST_DYNLIB
326
327 #include "wx/dynlib.h"
328
329 #if defined(__WXMSW__) || defined(__UNIX__)
330
331 static void TestDllListLoaded()
332 {
333 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
334
335 wxPuts("Loaded modules:");
336 wxDynamicLibraryDetailsArray dlls = wxDynamicLibrary::ListLoaded();
337 const size_t count = dlls.GetCount();
338 for ( size_t n = 0; n < count; ++n )
339 {
340 const wxDynamicLibraryDetails& details = dlls[n];
341 printf("%-45s", (const char *)details.GetPath().mb_str());
342
343 void *addr wxDUMMY_INITIALIZE(NULL);
344 size_t len wxDUMMY_INITIALIZE(0);
345 if ( details.GetAddress(&addr, &len) )
346 {
347 printf(" %08lx:%08lx",
348 (unsigned long)addr, (unsigned long)((char *)addr + len));
349 }
350
351 printf(" %s\n", (const char *)details.GetVersion().mb_str());
352 }
353
354 wxPuts(wxEmptyString);
355 }
356
357 #endif
358
359 #endif // TEST_DYNLIB
360
361 // ----------------------------------------------------------------------------
362 // MIME types
363 // ----------------------------------------------------------------------------
364
365 #ifdef TEST_MIME
366
367 #include "wx/mimetype.h"
368
369 static void TestMimeEnum()
370 {
371 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
372
373 wxArrayString mimetypes;
374
375 size_t count = wxTheMimeTypesManager->EnumAllFileTypes(mimetypes);
376
377 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count);
378
379 wxArrayString exts;
380 wxString desc;
381
382 for ( size_t n = 0; n < count; n++ )
383 {
384 wxFileType *filetype =
385 wxTheMimeTypesManager->GetFileTypeFromMimeType(mimetypes[n]);
386 if ( !filetype )
387 {
388 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
389 mimetypes[n].c_str());
390 continue;
391 }
392
393 filetype->GetDescription(&desc);
394 filetype->GetExtensions(exts);
395
396 filetype->GetIcon(NULL);
397
398 wxString extsAll;
399 for ( size_t e = 0; e < exts.GetCount(); e++ )
400 {
401 if ( e > 0 )
402 extsAll << wxT(", ");
403 extsAll += exts[e];
404 }
405
406 wxPrintf(wxT(" %s: %s (%s)\n"),
407 mimetypes[n].c_str(), desc.c_str(), extsAll.c_str());
408 }
409
410 wxPuts(wxEmptyString);
411 }
412
413 static void TestMimeFilename()
414 {
415 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
416
417 static const wxChar *filenames[] =
418 {
419 wxT("readme.txt"),
420 wxT("document.pdf"),
421 wxT("image.gif"),
422 wxT("picture.jpeg"),
423 };
424
425 for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
426 {
427 const wxString fname = filenames[n];
428 wxString ext = fname.AfterLast(wxT('.'));
429 wxFileType *ft = wxTheMimeTypesManager->GetFileTypeFromExtension(ext);
430 if ( !ft )
431 {
432 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext.c_str());
433 }
434 else
435 {
436 wxString desc;
437 if ( !ft->GetDescription(&desc) )
438 desc = wxT("<no description>");
439
440 wxString cmd;
441 if ( !ft->GetOpenCommand(&cmd,
442 wxFileType::MessageParameters(fname, wxEmptyString)) )
443 cmd = wxT("<no command available>");
444 else
445 cmd = wxString(wxT('"')) + cmd + wxT('"');
446
447 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
448 fname.c_str(), desc.c_str(), cmd.c_str());
449
450 delete ft;
451 }
452 }
453
454 wxPuts(wxEmptyString);
455 }
456
457 static void TestMimeAssociate()
458 {
459 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
460
461 wxFileTypeInfo ftInfo(
462 wxT("application/x-xyz"),
463 wxT("xyzview '%s'"), // open cmd
464 wxT(""), // print cmd
465 wxT("XYZ File"), // description
466 wxT(".xyz"), // extensions
467 wxNullPtr // end of extensions
468 );
469 ftInfo.SetShortDesc(wxT("XYZFile")); // used under Win32 only
470
471 wxFileType *ft = wxTheMimeTypesManager->Associate(ftInfo);
472 if ( !ft )
473 {
474 wxPuts(wxT("ERROR: failed to create association!"));
475 }
476 else
477 {
478 // TODO: read it back
479 delete ft;
480 }
481
482 wxPuts(wxEmptyString);
483 }
484
485 #endif // TEST_MIME
486
487
488 // ----------------------------------------------------------------------------
489 // misc information functions
490 // ----------------------------------------------------------------------------
491
492 #ifdef TEST_INFO_FUNCTIONS
493
494 #include "wx/utils.h"
495
496 #if TEST_INTERACTIVE
497 static void TestDiskInfo()
498 {
499 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
500
501 for ( ;; )
502 {
503 wxChar pathname[128];
504 wxPrintf(wxT("\nEnter a directory name (or 'quit' to escape): "));
505 if ( !wxFgets(pathname, WXSIZEOF(pathname), stdin) )
506 break;
507
508 // kill the last '\n'
509 pathname[wxStrlen(pathname) - 1] = 0;
510
511 if (wxStrcmp(pathname, "quit") == 0)
512 break;
513
514 wxLongLong total, free;
515 if ( !wxGetDiskSpace(pathname, &total, &free) )
516 {
517 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
518 }
519 else
520 {
521 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
522 (total / 1024).ToString().c_str(),
523 (free / 1024).ToString().c_str(),
524 pathname);
525 }
526 }
527 }
528 #endif // TEST_INTERACTIVE
529
530 static void TestOsInfo()
531 {
532 wxPuts(wxT("*** Testing OS info functions ***\n"));
533
534 int major, minor;
535 wxGetOsVersion(&major, &minor);
536 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
537 wxGetOsDescription().c_str(), major, minor);
538
539 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
540
541 wxPrintf(wxT("Host name is %s (%s).\n"),
542 wxGetHostName().c_str(), wxGetFullHostName().c_str());
543
544 wxPuts(wxEmptyString);
545 }
546
547 static void TestPlatformInfo()
548 {
549 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
550
551 // get this platform
552 wxPlatformInfo plat;
553
554 wxPrintf(wxT("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str());
555 wxPrintf(wxT("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str());
556 wxPrintf(wxT("Port ID name is: %s\n"), plat.GetPortIdName().c_str());
557 wxPrintf(wxT("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str());
558 wxPrintf(wxT("Architecture is: %s\n"), plat.GetArchName().c_str());
559 wxPrintf(wxT("Endianness is: %s\n"), plat.GetEndiannessName().c_str());
560
561 wxPuts(wxEmptyString);
562 }
563
564 static void TestUserInfo()
565 {
566 wxPuts(wxT("*** Testing user info functions ***\n"));
567
568 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
569 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
570 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
571 wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str());
572
573 wxPuts(wxEmptyString);
574 }
575
576 #endif // TEST_INFO_FUNCTIONS
577
578 // ----------------------------------------------------------------------------
579 // regular expressions
580 // ----------------------------------------------------------------------------
581
582 #if defined TEST_REGEX && TEST_INTERACTIVE
583
584 #include "wx/regex.h"
585
586 static void TestRegExInteractive()
587 {
588 wxPuts(wxT("*** Testing RE interactively ***"));
589
590 for ( ;; )
591 {
592 wxChar pattern[128];
593 wxPrintf(wxT("\nEnter a pattern (or 'quit' to escape): "));
594 if ( !wxFgets(pattern, WXSIZEOF(pattern), stdin) )
595 break;
596
597 // kill the last '\n'
598 pattern[wxStrlen(pattern) - 1] = 0;
599
600 if (wxStrcmp(pattern, "quit") == 0)
601 break;
602
603 wxRegEx re;
604 if ( !re.Compile(pattern) )
605 {
606 continue;
607 }
608
609 wxChar text[128];
610 for ( ;; )
611 {
612 wxPrintf(wxT("Enter text to match: "));
613 if ( !wxFgets(text, WXSIZEOF(text), stdin) )
614 break;
615
616 // kill the last '\n'
617 text[wxStrlen(text) - 1] = 0;
618
619 if ( !re.Matches(text) )
620 {
621 wxPrintf(wxT("No match.\n"));
622 }
623 else
624 {
625 wxPrintf(wxT("Pattern matches at '%s'\n"), re.GetMatch(text).c_str());
626
627 size_t start, len;
628 for ( size_t n = 1; ; n++ )
629 {
630 if ( !re.GetMatch(&start, &len, n) )
631 {
632 break;
633 }
634
635 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
636 n, wxString(text + start, len).c_str());
637 }
638 }
639 }
640 }
641 }
642
643 #endif // TEST_REGEX
644
645 // ----------------------------------------------------------------------------
646 // FTP
647 // ----------------------------------------------------------------------------
648
649 #ifdef TEST_FTP
650
651 #include "wx/protocol/ftp.h"
652 #include "wx/protocol/log.h"
653
654 #define FTP_ANONYMOUS
655
656 static wxFTP *ftp;
657
658 #ifdef FTP_ANONYMOUS
659 static const wxChar *hostname = wxT("ftp.wxwidgets.org");
660 #else
661 static const wxChar *hostname = "localhost";
662 #endif
663
664 static bool TestFtpConnect()
665 {
666 wxPuts(wxT("*** Testing FTP connect ***"));
667
668 #ifdef FTP_ANONYMOUS
669 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
670 #else // !FTP_ANONYMOUS
671 wxChar user[256];
672 wxFgets(user, WXSIZEOF(user), stdin);
673 user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
674 ftp->SetUser(user);
675
676 wxChar password[256];
677 wxPrintf(wxT("Password for %s: "), password);
678 wxFgets(password, WXSIZEOF(password), stdin);
679 password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
680 ftp->SetPassword(password);
681
682 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
683 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
684
685 if ( !ftp->Connect(hostname) )
686 {
687 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname);
688
689 return false;
690 }
691 else
692 {
693 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
694 hostname, ftp->Pwd().c_str());
695 ftp->Close();
696 }
697
698 return true;
699 }
700
701 #if TEST_INTERACTIVE
702 static void TestFtpInteractive()
703 {
704 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
705
706 wxChar buf[128];
707
708 for ( ;; )
709 {
710 wxPrintf(wxT("Enter FTP command (or 'quit' to escape): "));
711 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
712 break;
713
714 // kill the last '\n'
715 buf[wxStrlen(buf) - 1] = 0;
716
717 // special handling of LIST and NLST as they require data connection
718 wxString start(buf, 4);
719 start.MakeUpper();
720 if ( start == wxT("LIST") || start == wxT("NLST") )
721 {
722 wxString wildcard;
723 if ( wxStrlen(buf) > 4 )
724 wildcard = buf + 5;
725
726 wxArrayString files;
727 if ( !ftp->GetList(files, wildcard, start == wxT("LIST")) )
728 {
729 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start.c_str());
730 }
731 else
732 {
733 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
734 start.c_str(), wildcard.c_str(), ftp->Pwd().c_str());
735 size_t count = files.GetCount();
736 for ( size_t n = 0; n < count; n++ )
737 {
738 wxPrintf(wxT("\t%s\n"), files[n].c_str());
739 }
740 wxPuts(wxT("--- End of the file list"));
741 }
742 }
743 else if ( start == wxT("QUIT") )
744 {
745 break; // get out of here!
746 }
747 else // !list
748 {
749 wxChar ch = ftp->SendCommand(buf);
750 wxPrintf(wxT("Command %s"), ch ? wxT("succeeded") : wxT("failed"));
751 if ( ch )
752 {
753 wxPrintf(wxT(" (return code %c)"), ch);
754 }
755
756 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp->GetLastResult().c_str());
757 }
758 }
759
760 wxPuts(wxT("\n"));
761 }
762 #endif // TEST_INTERACTIVE
763 #endif // TEST_FTP
764
765 // ----------------------------------------------------------------------------
766 // stack backtrace
767 // ----------------------------------------------------------------------------
768
769 #ifdef TEST_STACKWALKER
770
771 #if wxUSE_STACKWALKER
772
773 #include "wx/stackwalk.h"
774
775 class StackDump : public wxStackWalker
776 {
777 public:
778 StackDump(const char *argv0)
779 : wxStackWalker(argv0)
780 {
781 }
782
783 virtual void Walk(size_t skip = 1, size_t maxdepth = wxSTACKWALKER_MAX_DEPTH)
784 {
785 wxPuts(wxT("Stack dump:"));
786
787 wxStackWalker::Walk(skip, maxdepth);
788 }
789
790 protected:
791 virtual void OnStackFrame(const wxStackFrame& frame)
792 {
793 printf("[%2d] ", (int) frame.GetLevel());
794
795 wxString name = frame.GetName();
796 if ( !name.empty() )
797 {
798 printf("%-20.40s", (const char*)name.mb_str());
799 }
800 else
801 {
802 printf("0x%08lx", (unsigned long)frame.GetAddress());
803 }
804
805 if ( frame.HasSourceLocation() )
806 {
807 printf("\t%s:%d",
808 (const char*)frame.GetFileName().mb_str(),
809 (int)frame.GetLine());
810 }
811
812 puts("");
813
814 wxString type, val;
815 for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ )
816 {
817 printf("\t%s %s = %s\n", (const char*)type.mb_str(),
818 (const char*)name.mb_str(),
819 (const char*)val.mb_str());
820 }
821 }
822 };
823
824 static void TestStackWalk(const char *argv0)
825 {
826 wxPuts(wxT("*** Testing wxStackWalker ***"));
827
828 StackDump dump(argv0);
829 dump.Walk();
830
831 wxPuts("\n");
832 }
833
834 #endif // wxUSE_STACKWALKER
835
836 #endif // TEST_STACKWALKER
837
838 // ----------------------------------------------------------------------------
839 // standard paths
840 // ----------------------------------------------------------------------------
841
842 #ifdef TEST_STDPATHS
843
844 #include "wx/stdpaths.h"
845 #include "wx/wxchar.h" // wxPrintf
846
847 static void TestStandardPaths()
848 {
849 wxPuts(wxT("*** Testing wxStandardPaths ***"));
850
851 wxTheApp->SetAppName(wxT("console"));
852
853 wxStandardPathsBase& stdp = wxStandardPaths::Get();
854 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
855 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
856 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
857 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str());
858 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str());
859 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str());
860 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str());
861 wxPrintf(wxT("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str());
862 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
863 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str());
864 wxPrintf(wxT("Localized res. dir:\t%s\n"),
865 stdp.GetLocalizedResourcesDir(wxT("fr")).c_str());
866 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
867 stdp.GetLocalizedResourcesDir
868 (
869 wxT("fr"),
870 wxStandardPaths::ResourceCat_Messages
871 ).c_str());
872
873 wxPuts("\n");
874 }
875
876 #endif // TEST_STDPATHS
877
878 // ----------------------------------------------------------------------------
879 // wxVolume tests
880 // ----------------------------------------------------------------------------
881
882 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
883 #undef TEST_VOLUME
884 #endif
885
886 #ifdef TEST_VOLUME
887
888 #include "wx/volume.h"
889
890 static const wxChar *volumeKinds[] =
891 {
892 wxT("floppy"),
893 wxT("hard disk"),
894 wxT("CD-ROM"),
895 wxT("DVD-ROM"),
896 wxT("network volume"),
897 wxT("other volume"),
898 };
899
900 static void TestFSVolume()
901 {
902 wxPuts(wxT("*** Testing wxFSVolume class ***"));
903
904 wxArrayString volumes = wxFSVolume::GetVolumes();
905 size_t count = volumes.GetCount();
906
907 if ( !count )
908 {
909 wxPuts(wxT("ERROR: no mounted volumes?"));
910 return;
911 }
912
913 wxPrintf(wxT("%u mounted volumes found:\n"), count);
914
915 for ( size_t n = 0; n < count; n++ )
916 {
917 wxFSVolume vol(volumes[n]);
918 if ( !vol.IsOk() )
919 {
920 wxPuts(wxT("ERROR: couldn't create volume"));
921 continue;
922 }
923
924 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
925 n + 1,
926 vol.GetDisplayName().c_str(),
927 vol.GetName().c_str(),
928 volumeKinds[vol.GetKind()],
929 vol.IsWritable() ? wxT("rw") : wxT("ro"),
930 vol.GetFlags() & wxFS_VOL_REMOVABLE ? wxT("removable")
931 : wxT("fixed"));
932 }
933
934 wxPuts("\n");
935 }
936
937 #endif // TEST_VOLUME
938
939 // ----------------------------------------------------------------------------
940 // date time
941 // ----------------------------------------------------------------------------
942
943 #ifdef TEST_DATETIME
944
945 #include "wx/math.h"
946 #include "wx/datetime.h"
947
948 #if TEST_INTERACTIVE
949
950 static void TestDateTimeInteractive()
951 {
952 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
953
954 wxChar buf[128];
955
956 for ( ;; )
957 {
958 wxPrintf(wxT("Enter a date (or 'quit' to escape): "));
959 if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
960 break;
961
962 // kill the last '\n'
963 buf[wxStrlen(buf) - 1] = 0;
964
965 if ( wxString(buf).CmpNoCase("quit") == 0 )
966 break;
967
968 wxDateTime dt;
969 const wxChar *p = dt.ParseDate(buf);
970 if ( !p )
971 {
972 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf);
973
974 continue;
975 }
976 else if ( *p )
977 {
978 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p - buf);
979 }
980
981 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
982 dt.Format(wxT("%b %d, %Y")).c_str(),
983 dt.GetDayOfYear(),
984 dt.GetWeekOfMonth(wxDateTime::Monday_First),
985 dt.GetWeekOfMonth(wxDateTime::Sunday_First),
986 dt.GetWeekOfYear(wxDateTime::Monday_First));
987 }
988
989 wxPuts("\n");
990 }
991
992 #endif // TEST_INTERACTIVE
993 #endif // TEST_DATETIME
994
995 // ----------------------------------------------------------------------------
996 // single instance
997 // ----------------------------------------------------------------------------
998
999 #ifdef TEST_SNGLINST
1000
1001 #include "wx/snglinst.h"
1002
1003 static bool TestSingleIstance()
1004 {
1005 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
1006
1007 wxSingleInstanceChecker checker;
1008 if ( checker.Create(wxT(".wxconsole.lock")) )
1009 {
1010 if ( checker.IsAnotherRunning() )
1011 {
1012 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
1013
1014 return false;
1015 }
1016
1017 // wait some time to give time to launch another instance
1018 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
1019 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
1020 wxFgetc(stdin);
1021 }
1022 else // failed to create
1023 {
1024 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
1025 }
1026
1027 wxPuts("\n");
1028
1029 return true;
1030 }
1031 #endif // TEST_SNGLINST
1032
1033
1034 // ----------------------------------------------------------------------------
1035 // entry point
1036 // ----------------------------------------------------------------------------
1037
1038 int main(int argc, char **argv)
1039 {
1040 #if wxUSE_UNICODE
1041 wxChar **wxArgv = new wxChar *[argc + 1];
1042
1043 {
1044 int n;
1045
1046 for (n = 0; n < argc; n++ )
1047 {
1048 wxMB2WXbuf warg = wxConvertMB2WX(argv[n]);
1049 wxArgv[n] = wxStrdup(warg);
1050 }
1051
1052 wxArgv[n] = NULL;
1053 }
1054 #else // !wxUSE_UNICODE
1055 #define wxArgv argv
1056 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1057
1058 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
1059
1060 wxInitializer initializer;
1061 if ( !initializer )
1062 {
1063 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
1064
1065 return -1;
1066 }
1067
1068 #ifdef TEST_SNGLINST
1069 if (!TestSingleIstance())
1070 return 1;
1071 #endif // TEST_SNGLINST
1072
1073 #ifdef TEST_DIR
1074 #if TEST_ALL
1075 TestDirExists();
1076 TestDirEnum();
1077 #endif
1078 TestDirTraverse();
1079 #endif // TEST_DIR
1080
1081 #ifdef TEST_DYNLIB
1082 TestDllListLoaded();
1083 #endif // TEST_DYNLIB
1084
1085 #ifdef TEST_FTP
1086 wxLog::AddTraceMask(FTP_TRACE_MASK);
1087
1088 // wxFTP cannot be a static variable as its ctor needs to access
1089 // wxWidgets internals after it has been initialized
1090 ftp = new wxFTP;
1091 ftp->SetLog(new wxProtocolLog(FTP_TRACE_MASK));
1092 if ( TestFtpConnect() )
1093 TestFtpInteractive();
1094 //else: connecting to the FTP server failed
1095
1096 delete ftp;
1097 #endif // TEST_FTP
1098
1099 #ifdef TEST_MIME
1100 TestMimeEnum();
1101 TestMimeAssociate();
1102 TestMimeFilename();
1103 #endif // TEST_MIME
1104
1105 #ifdef TEST_INFO_FUNCTIONS
1106 TestOsInfo();
1107 TestPlatformInfo();
1108 TestUserInfo();
1109
1110 #if TEST_INTERACTIVE
1111 TestDiskInfo();
1112 #endif
1113 #endif // TEST_INFO_FUNCTIONS
1114
1115 #ifdef TEST_PRINTF
1116 TestPrintf();
1117 #endif // TEST_PRINTF
1118
1119 #if defined TEST_REGEX && TEST_INTERACTIVE
1120 TestRegExInteractive();
1121 #endif // defined TEST_REGEX && TEST_INTERACTIVE
1122
1123 #ifdef TEST_DATETIME
1124 #if TEST_INTERACTIVE
1125 TestDateTimeInteractive();
1126 #endif
1127 #endif // TEST_DATETIME
1128
1129 #ifdef TEST_STACKWALKER
1130 #if wxUSE_STACKWALKER
1131 TestStackWalk(argv[0]);
1132 #endif
1133 #endif // TEST_STACKWALKER
1134
1135 #ifdef TEST_STDPATHS
1136 TestStandardPaths();
1137 #endif
1138
1139 #ifdef TEST_VOLUME
1140 TestFSVolume();
1141 #endif // TEST_VOLUME
1142
1143 #if wxUSE_UNICODE
1144 {
1145 for ( int n = 0; n < argc; n++ )
1146 free(wxArgv[n]);
1147
1148 delete [] wxArgv;
1149 }
1150 #endif // wxUSE_UNICODE
1151
1152 wxUnusedVar(argc);
1153 wxUnusedVar(argv);
1154 return 0;
1155 }