1 /////////////////////////////////////////////////////////////////////////////
2 // Name: samples/console/console.cpp
3 // Purpose: A sample console (as opposed to GUI) program using wxWidgets
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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:
20 int main(int argc, char **argv)
22 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program");
24 wxInitializer initializer;
27 fprintf(stderr, "Failed to initialize the wxWidgets library, aborting.");
31 static const wxCmdLineEntryDesc cmdLineDesc[] =
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...
40 wxCmdLineParser parser(cmdLineDesc, argc, wxArgv);
41 switch ( parser.Parse() )
44 wxLogMessage(wxT("Help was given, terminating."));
48 // everything is ok; proceed
52 wxLogMessage(wxT("Syntax error detected, aborting."));
56 // do something useful here
63 // ============================================================================
65 // ============================================================================
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
75 #include "wx/string.h"
77 #include "wx/filename.h"
80 #include "wx/apptrait.h"
81 #include "wx/platinfo.h"
82 #include "wx/wxchar.h"
84 // without this pragma, the stupid compiler precompiles #defines below so that
85 // changing them doesn't "take place" later!
90 // ----------------------------------------------------------------------------
91 // conditional compilation
92 // ----------------------------------------------------------------------------
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.
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.
112 #define TEST_FILENAME
113 #define TEST_FILETIME
114 #else // #if TEST_ALL
115 #define TEST_DATETIME
117 #define TEST_STDPATHS
118 #define TEST_STACKWALKER
120 #define TEST_SNGLINST
122 #define TEST_INFO_FUNCTIONS
126 // some tests are interactive, define this to run them
127 #ifdef TEST_INTERACTIVE
128 #undef TEST_INTERACTIVE
130 #define TEST_INTERACTIVE 1
132 #define TEST_INTERACTIVE 1
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
148 static const wxChar
*ROOTDIR
= wxT("/");
149 static const wxChar
*TESTDIR
= wxT("/usr/local/share");
150 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
151 static const wxChar
*ROOTDIR
= wxT("c:\\");
152 static const wxChar
*TESTDIR
= wxT("d:\\");
154 #error "don't know where the root directory is"
157 static void TestDirEnumHelper(wxDir
& dir
,
158 int flags
= wxDIR_DEFAULT
,
159 const wxString
& filespec
= wxEmptyString
)
163 if ( !dir
.IsOpened() )
166 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
169 wxPrintf(wxT("\t%s\n"), filename
.c_str());
171 cont
= dir
.GetNext(&filename
);
174 wxPuts(wxEmptyString
);
179 static void TestDirEnum()
181 wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
183 wxString cwd
= wxGetCwd();
184 if ( !wxDir
::Exists(cwd
) )
186 wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
191 if ( !dir
.IsOpened() )
193 wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
197 wxPuts(wxT("Enumerating everything in current directory:"));
198 TestDirEnumHelper(dir
);
200 wxPuts(wxT("Enumerating really everything in current directory:"));
201 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
203 wxPuts(wxT("Enumerating object files in current directory:"));
204 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, wxT("*.o*"));
206 wxPuts(wxT("Enumerating directories in current directory:"));
207 TestDirEnumHelper(dir
, wxDIR_DIRS
);
209 wxPuts(wxT("Enumerating files in current directory:"));
210 TestDirEnumHelper(dir
, wxDIR_FILES
);
212 wxPuts(wxT("Enumerating files including hidden in current directory:"));
213 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
217 wxPuts(wxT("Enumerating everything in root directory:"));
218 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
220 wxPuts(wxT("Enumerating directories in root directory:"));
221 TestDirEnumHelper(dir
, wxDIR_DIRS
);
223 wxPuts(wxT("Enumerating files in root directory:"));
224 TestDirEnumHelper(dir
, wxDIR_FILES
);
226 wxPuts(wxT("Enumerating files including hidden in root directory:"));
227 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
229 wxPuts(wxT("Enumerating files in non existing directory:"));
230 wxDir
dirNo(wxT("nosuchdir"));
231 TestDirEnumHelper(dirNo
);
236 class DirPrintTraverser
: public wxDirTraverser
239 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
241 return wxDIR_CONTINUE
;
244 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
246 wxString path
, name
, ext
;
247 wxFileName
::SplitPath(dirname
, &path
, &name
, &ext
);
250 name
<< wxT('.') << ext
;
253 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
255 if ( wxIsPathSeparator(*p
) )
259 wxPrintf(wxT("%s%s\n"), indent
.c_str(), name
.c_str());
261 return wxDIR_CONTINUE
;
265 static void TestDirTraverse()
267 wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
271 size_t n
= wxDir
::GetAllFiles(TESTDIR
, &files
);
272 wxPrintf(wxT("There are %u files under '%s'\n"), n
, TESTDIR
);
275 wxPrintf(wxT("First one is '%s'\n"), files
[0u].c_str());
276 wxPrintf(wxT(" last one is '%s'\n"), files
[n
- 1].c_str());
279 // enum again with custom traverser
280 wxPuts(wxT("Now enumerating directories:"));
282 DirPrintTraverser traverser
;
283 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
288 static void TestDirExists()
290 wxPuts(wxT("*** Testing wxDir::Exists() ***"));
292 static const wxChar
*dirnames
[] =
295 #if defined(__WXMSW__)
298 wxT("\\\\share\\file"),
302 wxT("c:\\autoexec.bat"),
303 #elif defined(__UNIX__)
312 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
314 wxPrintf(wxT("%-40s: %s\n"),
316 wxDir
::Exists(dirnames
[n
]) ?
wxT("exists")
317 : wxT("doesn't exist"));
325 // ----------------------------------------------------------------------------
327 // ----------------------------------------------------------------------------
331 #include "wx/dynlib.h"
333 static void TestDllLoad()
335 #if defined(__WXMSW__)
336 static const wxChar
*LIB_NAME
= wxT("kernel32.dll");
337 static const wxChar
*FUNC_NAME
= wxT("lstrlenA");
338 #elif defined(__UNIX__)
339 // weird: using just libc.so does *not* work!
340 static const wxChar
*LIB_NAME
= wxT("/lib/libc.so.6");
341 static const wxChar
*FUNC_NAME
= wxT("strlen");
343 #error "don't know how to test wxDllLoader on this platform"
346 wxPuts(wxT("*** testing basic wxDynamicLibrary functions ***\n"));
348 wxDynamicLibrary
lib(LIB_NAME
);
349 if ( !lib
.IsLoaded() )
351 wxPrintf(wxT("ERROR: failed to load '%s'.\n"), LIB_NAME
);
355 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
356 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
359 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
360 FUNC_NAME
, LIB_NAME
);
364 wxPrintf(wxT("Calling %s dynamically loaded from %s "),
365 FUNC_NAME
, LIB_NAME
);
367 if ( pfnStrlen("foo") != 3 )
369 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
373 wxPuts(wxT("... ok"));
378 static const wxChar
*FUNC_NAME_AW
= wxT("lstrlen");
380 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
382 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
383 if ( !pfnStrlenAorW
)
385 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
386 FUNC_NAME_AW
, LIB_NAME
);
390 if ( pfnStrlenAorW(wxT("foobar")) != 6 )
392 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
399 #if defined(__WXMSW__) || defined(__UNIX__)
401 static void TestDllListLoaded()
403 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
405 puts("\nLoaded modules:");
406 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary
::ListLoaded();
407 const size_t count
= dlls
.GetCount();
408 for ( size_t n
= 0; n
< count
; ++n
)
410 const wxDynamicLibraryDetails
& details
= dlls
[n
];
411 printf("%-45s", (const char *)details
.GetPath().mb_str());
413 void *addr
wxDUMMY_INITIALIZE(NULL
);
414 size_t len
wxDUMMY_INITIALIZE(0);
415 if ( details
.GetAddress(&addr
, &len
) )
417 printf(" %08lx:%08lx",
418 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
421 printf(" %s\n", (const char *)details
.GetVersion().mb_str());
427 #endif // TEST_DYNLIB
429 // ----------------------------------------------------------------------------
431 // ----------------------------------------------------------------------------
435 #include "wx/utils.h"
437 static wxString
MyGetEnv(const wxString
& var
)
440 if ( !wxGetEnv(var
, &val
) )
441 val
= wxT("<empty>");
443 val
= wxString(wxT('\'')) + val
+ wxT('\'');
448 static void TestEnvironment()
450 const wxChar
*var
= wxT("wxTestVar");
452 wxPuts(wxT("*** testing environment access functions ***"));
454 wxPrintf(wxT("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
455 wxSetEnv(var
, wxT("value for wxTestVar"));
456 wxPrintf(wxT("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
457 wxSetEnv(var
, wxT("another value"));
458 wxPrintf(wxT("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
460 wxPrintf(wxT("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
461 wxPrintf(wxT("PATH = %s\n"), MyGetEnv(wxT("PATH")).c_str());
464 #endif // TEST_ENVIRON
466 // ----------------------------------------------------------------------------
468 // ----------------------------------------------------------------------------
473 #include "wx/ffile.h"
474 #include "wx/textfile.h"
476 static void TestFileRead()
478 wxPuts(wxT("*** wxFile read test ***"));
480 wxFile
file(wxT("testdata.fc"));
481 if ( file
.IsOpened() )
483 wxPrintf(wxT("File length: %lu\n"), file
.Length());
485 wxPuts(wxT("File dump:\n----------"));
487 static const size_t len
= 1024;
491 size_t nRead
= file
.Read(buf
, len
);
492 if ( nRead
== (size_t)wxInvalidOffset
)
494 wxPrintf(wxT("Failed to read the file."));
498 fwrite(buf
, nRead
, 1, stdout
);
504 wxPuts(wxT("----------"));
508 wxPrintf(wxT("ERROR: can't open test file.\n"));
511 wxPuts(wxEmptyString
);
514 static void TestTextFileRead()
516 wxPuts(wxT("*** wxTextFile read test ***"));
518 wxTextFile
file(wxT("testdata.fc"));
521 wxPrintf(wxT("Number of lines: %u\n"), file
.GetLineCount());
522 wxPrintf(wxT("Last line: '%s'\n"), file
.GetLastLine().c_str());
526 wxPuts(wxT("\nDumping the entire file:"));
527 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
529 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
531 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
533 wxPuts(wxT("\nAnd now backwards:"));
534 for ( s
= file
.GetLastLine();
535 file
.GetCurrentLine() != 0;
536 s
= file
.GetPrevLine() )
538 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
540 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
544 wxPrintf(wxT("ERROR: can't open '%s'\n"), file
.GetName());
547 wxPuts(wxEmptyString
);
550 static void TestFileCopy()
552 wxPuts(wxT("*** Testing wxCopyFile ***"));
554 static const wxChar
*filename1
= wxT("testdata.fc");
555 static const wxChar
*filename2
= wxT("test2");
556 if ( !wxCopyFile(filename1
, filename2
) )
558 wxPuts(wxT("ERROR: failed to copy file"));
562 wxFFile
f1(filename1
, wxT("rb")),
563 f2(filename2
, wxT("rb"));
565 if ( !f1
.IsOpened() || !f2
.IsOpened() )
567 wxPuts(wxT("ERROR: failed to open file(s)"));
572 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
574 wxPuts(wxT("ERROR: failed to read file(s)"));
578 if ( (s1
.length() != s2
.length()) ||
579 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
581 wxPuts(wxT("ERROR: copy error!"));
585 wxPuts(wxT("File was copied ok."));
591 if ( !wxRemoveFile(filename2
) )
593 wxPuts(wxT("ERROR: failed to remove the file"));
596 wxPuts(wxEmptyString
);
599 static void TestTempFile()
601 wxPuts(wxT("*** wxTempFile test ***"));
604 if ( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) )
606 if ( tmpFile
.Commit() )
607 wxPuts(wxT("File committed."));
609 wxPuts(wxT("ERROR: could't commit temp file."));
611 wxRemoveFile(wxT("test2"));
614 wxPuts(wxEmptyString
);
619 // ----------------------------------------------------------------------------
621 // ----------------------------------------------------------------------------
625 #include "wx/filename.h"
628 static void DumpFileName(const wxChar
*desc
, const wxFileName
& fn
)
632 wxString full
= fn
.GetFullPath();
634 wxString vol
, path
, name
, ext
;
635 wxFileName
::SplitPath(full
, &vol
, &path
, &name
, &ext
);
637 wxPrintf(wxT("'%s'-> vol '%s', path '%s', name '%s', ext '%s'\n"),
638 full
.c_str(), vol
.c_str(), path
.c_str(), name
.c_str(), ext
.c_str());
640 wxFileName
::SplitPath(full
, &path
, &name
, &ext
);
641 wxPrintf(wxT("or\t\t-> path '%s', name '%s', ext '%s'\n"),
642 path
.c_str(), name
.c_str(), ext
.c_str());
644 wxPrintf(wxT("path is also:\t'%s'\n"), fn
.GetPath().c_str());
645 wxPrintf(wxT("with volume: \t'%s'\n"),
646 fn
.GetPath(wxPATH_GET_VOLUME
).c_str());
647 wxPrintf(wxT("with separator:\t'%s'\n"),
648 fn
.GetPath(wxPATH_GET_SEPARATOR
).c_str());
649 wxPrintf(wxT("with both: \t'%s'\n"),
650 fn
.GetPath(wxPATH_GET_SEPARATOR
| wxPATH_GET_VOLUME
).c_str());
652 wxPuts(wxT("The directories in the path are:"));
653 wxArrayString dirs
= fn
.GetDirs();
654 size_t count
= dirs
.GetCount();
655 for ( size_t n
= 0; n
< count
; n
++ )
657 wxPrintf(wxT("\t%u: %s\n"), n
, dirs
[n
].c_str());
662 static void TestFileNameTemp()
664 wxPuts(wxT("*** testing wxFileName temp file creation ***"));
666 static const wxChar
*tmpprefixes
[] =
674 wxT("/tmp/foo/bar"), // this one must be an error
678 for ( size_t n
= 0; n
< WXSIZEOF(tmpprefixes
); n
++ )
680 wxString path
= wxFileName
::CreateTempFileName(tmpprefixes
[n
]);
683 // "error" is not in upper case because it may be ok
684 wxPrintf(wxT("Prefix '%s'\t-> error\n"), tmpprefixes
[n
]);
688 wxPrintf(wxT("Prefix '%s'\t-> temp file '%s'\n"),
689 tmpprefixes
[n
], path
.c_str());
691 if ( !wxRemoveFile(path
) )
693 wxLogWarning(wxT("Failed to remove temp file '%s'"),
700 static void TestFileNameDirManip()
702 // TODO: test AppendDir(), RemoveDir(), ...
705 static void TestFileNameComparison()
710 static void TestFileNameOperations()
715 static void TestFileNameCwd()
720 #endif // TEST_FILENAME
722 // ----------------------------------------------------------------------------
723 // wxFileName time functions
724 // ----------------------------------------------------------------------------
728 #include "wx/filename.h"
729 #include "wx/datetime.h"
731 static void TestFileGetTimes()
733 wxFileName
fn(wxT("testdata.fc"));
735 wxDateTime dtAccess
, dtMod
, dtCreate
;
736 if ( !fn
.GetTimes(&dtAccess
, &dtMod
, &dtCreate
) )
738 wxPrintf(wxT("ERROR: GetTimes() failed.\n"));
742 static const wxChar
*fmt
= wxT("%Y-%b-%d %H:%M:%S");
744 wxPrintf(wxT("File times for '%s':\n"), fn
.GetFullPath().c_str());
745 wxPrintf(wxT("Creation: \t%s\n"), dtCreate
.Format(fmt
).c_str());
746 wxPrintf(wxT("Last read: \t%s\n"), dtAccess
.Format(fmt
).c_str());
747 wxPrintf(wxT("Last write: \t%s\n"), dtMod
.Format(fmt
).c_str());
752 static void TestFileSetTimes()
754 wxFileName
fn(wxT("testdata.fc"));
758 wxPrintf(wxT("ERROR: Touch() failed.\n"));
763 #endif // TEST_FILETIME
765 // ----------------------------------------------------------------------------
767 // ----------------------------------------------------------------------------
771 #include "wx/mimetype.h"
773 static void TestMimeEnum()
775 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
777 wxArrayString mimetypes
;
779 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
781 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count
);
786 for ( size_t n
= 0; n
< count
; n
++ )
788 wxFileType
*filetype
=
789 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
792 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
793 mimetypes
[n
].c_str());
797 filetype
->GetDescription(&desc
);
798 filetype
->GetExtensions(exts
);
800 filetype
->GetIcon(NULL
);
803 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
806 extsAll
<< wxT(", ");
810 wxPrintf(wxT(" %s: %s (%s)\n"),
811 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
814 wxPuts(wxEmptyString
);
817 static void TestMimeFilename()
819 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
821 static const wxChar
*filenames
[] =
829 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
831 const wxString fname
= filenames
[n
];
832 wxString ext
= fname
.AfterLast(wxT('.'));
833 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
836 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
841 if ( !ft
->GetDescription(&desc
) )
842 desc
= wxT("<no description>");
845 if ( !ft
->GetOpenCommand(&cmd
,
846 wxFileType
::MessageParameters(fname
, wxEmptyString
)) )
847 cmd
= wxT("<no command available>");
849 cmd
= wxString(wxT('"')) + cmd
+ wxT('"');
851 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
852 fname
.c_str(), desc
.c_str(), cmd
.c_str());
858 wxPuts(wxEmptyString
);
861 static void TestMimeAssociate()
863 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
865 wxFileTypeInfo
ftInfo(
866 wxT("application/x-xyz"),
867 wxT("xyzview '%s'"), // open cmd
868 wxT(""), // print cmd
869 wxT("XYZ File"), // description
870 wxT(".xyz"), // extensions
871 wxNullPtr
// end of extensions
873 ftInfo
.SetShortDesc(wxT("XYZFile")); // used under Win32 only
875 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
878 wxPuts(wxT("ERROR: failed to create association!"));
882 // TODO: read it back
886 wxPuts(wxEmptyString
);
892 // ----------------------------------------------------------------------------
893 // misc information functions
894 // ----------------------------------------------------------------------------
896 #ifdef TEST_INFO_FUNCTIONS
898 #include "wx/utils.h"
901 static void TestDiskInfo()
903 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
907 wxChar pathname
[128];
908 wxPrintf(wxT("\nEnter a directory name (or 'quit' to escape): "));
909 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
912 // kill the last '\n'
913 pathname
[wxStrlen(pathname
) - 1] = 0;
915 if (wxStrcmp(pathname
, "quit") == 0)
918 wxLongLong total
, free
;
919 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
921 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
925 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
926 (total
/ 1024).ToString().c_str(),
927 (free
/ 1024).ToString().c_str(),
932 #endif // TEST_INTERACTIVE
934 static void TestOsInfo()
936 wxPuts(wxT("*** Testing OS info functions ***\n"));
939 wxGetOsVersion(&major
, &minor
);
940 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
941 wxGetOsDescription().c_str(), major
, minor
);
943 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
945 wxPrintf(wxT("Host name is %s (%s).\n"),
946 wxGetHostName().c_str(), wxGetFullHostName().c_str());
948 wxPuts(wxEmptyString
);
951 static void TestPlatformInfo()
953 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
958 wxPrintf(wxT("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
959 wxPrintf(wxT("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
960 wxPrintf(wxT("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
961 wxPrintf(wxT("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
962 wxPrintf(wxT("Architecture is: %s\n"), plat
.GetArchName().c_str());
963 wxPrintf(wxT("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
965 wxPuts(wxEmptyString
);
968 static void TestUserInfo()
970 wxPuts(wxT("*** Testing user info functions ***\n"));
972 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
973 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
974 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
975 wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str());
977 wxPuts(wxEmptyString
);
980 #endif // TEST_INFO_FUNCTIONS
982 // ----------------------------------------------------------------------------
983 // regular expressions
984 // ----------------------------------------------------------------------------
986 #if defined TEST_REGEX && TEST_INTERACTIVE
988 #include "wx/regex.h"
990 static void TestRegExInteractive()
992 wxPuts(wxT("*** Testing RE interactively ***"));
997 wxPrintf(wxT("\nEnter a pattern (or 'quit' to escape): "));
998 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
1001 // kill the last '\n'
1002 pattern
[wxStrlen(pattern
) - 1] = 0;
1004 if (wxStrcmp(pattern
, "quit") == 0)
1008 if ( !re
.Compile(pattern
) )
1016 wxPrintf(wxT("Enter text to match: "));
1017 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
1020 // kill the last '\n'
1021 text
[wxStrlen(text
) - 1] = 0;
1023 if ( !re
.Matches(text
) )
1025 wxPrintf(wxT("No match.\n"));
1029 wxPrintf(wxT("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
1032 for ( size_t n
= 1; ; n
++ )
1034 if ( !re
.GetMatch(&start
, &len
, n
) )
1039 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
1040 n
, wxString(text
+ start
, len
).c_str());
1047 #endif // TEST_REGEX
1049 // ----------------------------------------------------------------------------
1051 // ----------------------------------------------------------------------------
1055 #include "wx/protocol/ftp.h"
1056 #include "wx/protocol/log.h"
1058 #define FTP_ANONYMOUS
1062 #ifdef FTP_ANONYMOUS
1063 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
1064 static const wxChar
*directory
= wxT("/pub");
1065 static const wxChar
*filename
= wxT("welcome.msg");
1067 static const wxChar
*hostname
= "localhost";
1068 static const wxChar
*directory
= wxT("/etc");
1069 static const wxChar
*filename
= wxT("issue");
1072 static bool TestFtpConnect()
1074 wxPuts(wxT("*** Testing FTP connect ***"));
1076 #ifdef FTP_ANONYMOUS
1077 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
1078 #else // !FTP_ANONYMOUS
1080 wxFgets(user
, WXSIZEOF(user
), stdin
);
1081 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
1084 wxChar password
[256];
1085 wxPrintf(wxT("Password for %s: "), password
);
1086 wxFgets(password
, WXSIZEOF(password
), stdin
);
1087 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
1088 ftp
->SetPassword(password
);
1090 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
1091 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
1093 if ( !ftp
->Connect(hostname
) )
1095 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
1101 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
1102 hostname
, ftp
->Pwd().c_str());
1109 #if TEST_INTERACTIVE
1110 static void TestFtpInteractive()
1112 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
1118 wxPrintf(wxT("Enter FTP command (or 'quit' to escape): "));
1119 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
1122 // kill the last '\n'
1123 buf
[wxStrlen(buf
) - 1] = 0;
1125 // special handling of LIST and NLST as they require data connection
1126 wxString
start(buf
, 4);
1128 if ( start
== wxT("LIST") || start
== wxT("NLST") )
1131 if ( wxStrlen(buf
) > 4 )
1134 wxArrayString files
;
1135 if ( !ftp
->GetList(files
, wildcard
, start
== wxT("LIST")) )
1137 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
1141 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
1142 start
.c_str(), wildcard
.c_str(), ftp
->Pwd().c_str());
1143 size_t count
= files
.GetCount();
1144 for ( size_t n
= 0; n
< count
; n
++ )
1146 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
1148 wxPuts(wxT("--- End of the file list"));
1151 else if ( start
== wxT("QUIT") )
1153 break; // get out of here!
1157 wxChar ch
= ftp
->SendCommand(buf
);
1158 wxPrintf(wxT("Command %s"), ch ?
wxT("succeeded") : wxT("failed"));
1161 wxPrintf(wxT(" (return code %c)"), ch
);
1164 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
->GetLastResult().c_str());
1170 #endif // TEST_INTERACTIVE
1173 // ----------------------------------------------------------------------------
1175 // ----------------------------------------------------------------------------
1177 #ifdef TEST_STACKWALKER
1179 #if wxUSE_STACKWALKER
1181 #include "wx/stackwalk.h"
1183 class StackDump
: public wxStackWalker
1186 StackDump(const char *argv0
)
1187 : wxStackWalker(argv0
)
1191 virtual void Walk(size_t skip
= 1, size_t maxdepth
= wxSTACKWALKER_MAX_DEPTH
)
1193 wxPuts(wxT("Stack dump:"));
1195 wxStackWalker
::Walk(skip
, maxdepth
);
1199 virtual void OnStackFrame(const wxStackFrame
& frame
)
1201 printf("[%2d] ", (int) frame
.GetLevel());
1203 wxString name
= frame
.GetName();
1204 if ( !name
.empty() )
1206 printf("%-20.40s", (const char*)name
.mb_str());
1210 printf("0x%08lx", (unsigned long)frame
.GetAddress());
1213 if ( frame
.HasSourceLocation() )
1216 (const char*)frame
.GetFileName().mb_str(),
1217 (int)frame
.GetLine());
1223 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
1225 printf("\t%s %s = %s\n", (const char*)type
.mb_str(),
1226 (const char*)name
.mb_str(),
1227 (const char*)val
.mb_str());
1232 static void TestStackWalk(const char *argv0
)
1234 wxPuts(wxT("*** Testing wxStackWalker ***"));
1236 StackDump
dump(argv0
);
1242 #endif // wxUSE_STACKWALKER
1244 #endif // TEST_STACKWALKER
1246 // ----------------------------------------------------------------------------
1248 // ----------------------------------------------------------------------------
1250 #ifdef TEST_STDPATHS
1252 #include "wx/stdpaths.h"
1253 #include "wx/wxchar.h" // wxPrintf
1255 static void TestStandardPaths()
1257 wxPuts(wxT("*** Testing wxStandardPaths ***"));
1259 wxTheApp
->SetAppName(wxT("console"));
1261 wxStandardPathsBase
& stdp
= wxStandardPaths
::Get();
1262 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
1263 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
1264 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
1265 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
1266 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
1267 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
1268 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
1269 wxPrintf(wxT("Executable path:\t%s\n"), stdp
.GetExecutablePath().c_str());
1270 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
1271 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
1272 wxPrintf(wxT("Localized res. dir:\t%s\n"),
1273 stdp
.GetLocalizedResourcesDir(wxT("fr")).c_str());
1274 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
1275 stdp
.GetLocalizedResourcesDir
1278 wxStandardPaths
::ResourceCat_Messages
1284 #endif // TEST_STDPATHS
1286 // ----------------------------------------------------------------------------
1288 // ----------------------------------------------------------------------------
1290 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
1296 #include "wx/volume.h"
1298 static const wxChar
*volumeKinds
[] =
1304 wxT("network volume"),
1305 wxT("other volume"),
1308 static void TestFSVolume()
1310 wxPuts(wxT("*** Testing wxFSVolume class ***"));
1312 wxArrayString volumes
= wxFSVolume
::GetVolumes();
1313 size_t count
= volumes
.GetCount();
1317 wxPuts(wxT("ERROR: no mounted volumes?"));
1321 wxPrintf(wxT("%u mounted volumes found:\n"), count
);
1323 for ( size_t n
= 0; n
< count
; n
++ )
1325 wxFSVolume
vol(volumes
[n
]);
1328 wxPuts(wxT("ERROR: couldn't create volume"));
1332 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
1334 vol
.GetDisplayName().c_str(),
1335 vol
.GetName().c_str(),
1336 volumeKinds
[vol
.GetKind()],
1337 vol
.IsWritable() ?
wxT("rw") : wxT("ro"),
1338 vol
.GetFlags() & wxFS_VOL_REMOVABLE ?
wxT("removable")
1345 #endif // TEST_VOLUME
1347 // ----------------------------------------------------------------------------
1349 // ----------------------------------------------------------------------------
1351 #ifdef TEST_DATETIME
1353 #include "wx/math.h"
1354 #include "wx/datetime.h"
1356 #if TEST_INTERACTIVE
1358 static void TestDateTimeInteractive()
1360 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
1366 wxPrintf(wxT("Enter a date (or 'quit' to escape): "));
1367 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
1370 // kill the last '\n'
1371 buf
[wxStrlen(buf
) - 1] = 0;
1373 if ( wxString(buf
).CmpNoCase("quit") == 0 )
1377 const wxChar
*p
= dt
.ParseDate(buf
);
1380 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
1386 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
1389 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
1390 dt
.Format(wxT("%b %d, %Y")).c_str(),
1392 dt
.GetWeekOfMonth(wxDateTime
::Monday_First
),
1393 dt
.GetWeekOfMonth(wxDateTime
::Sunday_First
),
1394 dt
.GetWeekOfYear(wxDateTime
::Monday_First
));
1400 #endif // TEST_INTERACTIVE
1401 #endif // TEST_DATETIME
1403 // ----------------------------------------------------------------------------
1405 // ----------------------------------------------------------------------------
1407 #ifdef TEST_SNGLINST
1409 #include "wx/snglinst.h"
1411 static bool TestSingleIstance()
1413 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
1415 wxSingleInstanceChecker checker
;
1416 if ( checker
.Create(wxT(".wxconsole.lock")) )
1418 if ( checker
.IsAnotherRunning() )
1420 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
1425 // wait some time to give time to launch another instance
1426 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
1427 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
1430 else // failed to create
1432 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
1439 #endif // TEST_SNGLINST
1442 // ----------------------------------------------------------------------------
1444 // ----------------------------------------------------------------------------
1446 int main(int argc
, char **argv
)
1449 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
1454 for (n
= 0; n
< argc
; n
++ )
1456 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
1457 wxArgv
[n
] = wxStrdup(warg
);
1462 #else // !wxUSE_UNICODE
1464 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1466 wxApp
::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
1468 wxInitializer initializer
;
1471 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
1476 #ifdef TEST_SNGLINST
1477 if (!TestSingleIstance())
1479 #endif // TEST_SNGLINST
1491 TestDllListLoaded();
1492 #endif // TEST_DYNLIB
1496 #endif // TEST_ENVIRON
1505 #ifdef TEST_FILENAME
1508 TestFileNameDirManip();
1509 TestFileNameComparison();
1510 TestFileNameOperations();
1511 #endif // TEST_FILENAME
1513 #ifdef TEST_FILETIME
1518 #endif // TEST_FILETIME
1521 wxLog
::AddTraceMask(FTP_TRACE_MASK
);
1523 // wxFTP cannot be a static variable as its ctor needs to access
1524 // wxWidgets internals after it has been initialized
1526 ftp
->SetLog(new wxProtocolLog(FTP_TRACE_MASK
));
1527 if ( TestFtpConnect() )
1528 TestFtpInteractive();
1529 //else: connecting to the FTP server failed
1535 //wxLog::AddTraceMask(wxT("mime"));
1537 TestMimeAssociate();
1541 #ifdef TEST_INFO_FUNCTIONS
1546 #if TEST_INTERACTIVE
1549 #endif // TEST_INFO_FUNCTIONS
1553 #endif // TEST_PRINTF
1555 #if defined TEST_REGEX && TEST_INTERACTIVE
1556 TestRegExInteractive();
1557 #endif // defined TEST_REGEX && TEST_INTERACTIVE
1559 #ifdef TEST_DATETIME
1560 #if TEST_INTERACTIVE
1561 TestDateTimeInteractive();
1563 #endif // TEST_DATETIME
1565 #ifdef TEST_STACKWALKER
1566 #if wxUSE_STACKWALKER
1567 TestStackWalk(argv
[0]);
1569 #endif // TEST_STACKWALKER
1571 #ifdef TEST_STDPATHS
1572 TestStandardPaths();
1576 wxPuts(wxT("Sleeping for 3 seconds... z-z-z-z-z..."));
1578 #endif // TEST_USLEEP
1582 #endif // TEST_VOLUME
1586 for ( int n
= 0; n
< argc
; n
++ )
1591 #endif // wxUSE_UNICODE