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