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