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 #else // #if TEST_ALL
113 #define TEST_DATETIME
115 #define TEST_STDPATHS
116 #define TEST_STACKWALKER
118 #define TEST_SNGLINST
120 #define TEST_INFO_FUNCTIONS
124 // some tests are interactive, define this to run them
125 #ifdef TEST_INTERACTIVE
126 #undef TEST_INTERACTIVE
128 #define TEST_INTERACTIVE 1
130 #define TEST_INTERACTIVE 1
133 // ============================================================================
135 // ============================================================================
137 // ----------------------------------------------------------------------------
139 // ----------------------------------------------------------------------------
146 static const wxChar
*ROOTDIR
= wxT("/");
147 static const wxChar
*TESTDIR
= wxT("/usr/local/share");
148 #elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__)
149 static const wxChar
*ROOTDIR
= wxT("c:\\");
150 static const wxChar
*TESTDIR
= wxT("d:\\");
152 #error "don't know where the root directory is"
155 static void TestDirEnumHelper(wxDir
& dir
,
156 int flags
= wxDIR_DEFAULT
,
157 const wxString
& filespec
= wxEmptyString
)
161 if ( !dir
.IsOpened() )
164 bool cont
= dir
.GetFirst(&filename
, filespec
, flags
);
167 wxPrintf(wxT("\t%s\n"), filename
.c_str());
169 cont
= dir
.GetNext(&filename
);
172 wxPuts(wxEmptyString
);
177 static void TestDirEnum()
179 wxPuts(wxT("*** Testing wxDir::GetFirst/GetNext ***"));
181 wxString cwd
= wxGetCwd();
182 if ( !wxDir::Exists(cwd
) )
184 wxPrintf(wxT("ERROR: current directory '%s' doesn't exist?\n"), cwd
.c_str());
189 if ( !dir
.IsOpened() )
191 wxPrintf(wxT("ERROR: failed to open current directory '%s'.\n"), cwd
.c_str());
195 wxPuts(wxT("Enumerating everything in current directory:"));
196 TestDirEnumHelper(dir
);
198 wxPuts(wxT("Enumerating really everything in current directory:"));
199 TestDirEnumHelper(dir
, wxDIR_DEFAULT
| wxDIR_DOTDOT
);
201 wxPuts(wxT("Enumerating object files in current directory:"));
202 TestDirEnumHelper(dir
, wxDIR_DEFAULT
, wxT("*.o*"));
204 wxPuts(wxT("Enumerating directories in current directory:"));
205 TestDirEnumHelper(dir
, wxDIR_DIRS
);
207 wxPuts(wxT("Enumerating files in current directory:"));
208 TestDirEnumHelper(dir
, wxDIR_FILES
);
210 wxPuts(wxT("Enumerating files including hidden in current directory:"));
211 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
215 wxPuts(wxT("Enumerating everything in root directory:"));
216 TestDirEnumHelper(dir
, wxDIR_DEFAULT
);
218 wxPuts(wxT("Enumerating directories in root directory:"));
219 TestDirEnumHelper(dir
, wxDIR_DIRS
);
221 wxPuts(wxT("Enumerating files in root directory:"));
222 TestDirEnumHelper(dir
, wxDIR_FILES
);
224 wxPuts(wxT("Enumerating files including hidden in root directory:"));
225 TestDirEnumHelper(dir
, wxDIR_FILES
| wxDIR_HIDDEN
);
227 wxPuts(wxT("Enumerating files in non existing directory:"));
228 wxDir
dirNo(wxT("nosuchdir"));
229 TestDirEnumHelper(dirNo
);
234 class DirPrintTraverser
: public wxDirTraverser
237 virtual wxDirTraverseResult
OnFile(const wxString
& WXUNUSED(filename
))
239 return wxDIR_CONTINUE
;
242 virtual wxDirTraverseResult
OnDir(const wxString
& dirname
)
244 wxString path
, name
, ext
;
245 wxFileName::SplitPath(dirname
, &path
, &name
, &ext
);
248 name
<< wxT('.') << ext
;
251 for ( const wxChar
*p
= path
.c_str(); *p
; p
++ )
253 if ( wxIsPathSeparator(*p
) )
257 wxPrintf(wxT("%s%s\n"), indent
.c_str(), name
.c_str());
259 return wxDIR_CONTINUE
;
263 static void TestDirTraverse()
265 wxPuts(wxT("*** Testing wxDir::Traverse() ***"));
269 size_t n
= wxDir::GetAllFiles(TESTDIR
, &files
);
270 wxPrintf(wxT("There are %u files under '%s'\n"), n
, TESTDIR
);
273 wxPrintf(wxT("First one is '%s'\n"), files
[0u].c_str());
274 wxPrintf(wxT(" last one is '%s'\n"), files
[n
- 1].c_str());
277 // enum again with custom traverser
278 wxPuts(wxT("Now enumerating directories:"));
280 DirPrintTraverser traverser
;
281 dir
.Traverse(traverser
, wxEmptyString
, wxDIR_DIRS
| wxDIR_HIDDEN
);
286 static void TestDirExists()
288 wxPuts(wxT("*** Testing wxDir::Exists() ***"));
290 static const wxChar
*dirnames
[] =
293 #if defined(__WXMSW__)
296 wxT("\\\\share\\file"),
300 wxT("c:\\autoexec.bat"),
301 #elif defined(__UNIX__)
310 for ( size_t n
= 0; n
< WXSIZEOF(dirnames
); n
++ )
312 wxPrintf(wxT("%-40s: %s\n"),
314 wxDir::Exists(dirnames
[n
]) ? wxT("exists")
315 : wxT("doesn't exist"));
323 // ----------------------------------------------------------------------------
325 // ----------------------------------------------------------------------------
329 #include "wx/dynlib.h"
331 static void TestDllLoad()
333 #if defined(__WXMSW__)
334 static const wxChar
*LIB_NAME
= wxT("kernel32.dll");
335 static const wxChar
*FUNC_NAME
= wxT("lstrlenA");
336 #elif defined(__UNIX__)
337 // weird: using just libc.so does *not* work!
338 static const wxChar
*LIB_NAME
= wxT("/lib/libc.so.6");
339 static const wxChar
*FUNC_NAME
= wxT("strlen");
341 #error "don't know how to test wxDllLoader on this platform"
344 wxPuts(wxT("*** testing basic wxDynamicLibrary functions ***\n"));
346 wxDynamicLibrary
lib(LIB_NAME
);
347 if ( !lib
.IsLoaded() )
349 wxPrintf(wxT("ERROR: failed to load '%s'.\n"), LIB_NAME
);
353 typedef int (wxSTDCALL
*wxStrlenType
)(const char *);
354 wxStrlenType pfnStrlen
= (wxStrlenType
)lib
.GetSymbol(FUNC_NAME
);
357 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
358 FUNC_NAME
, LIB_NAME
);
362 wxPrintf(wxT("Calling %s dynamically loaded from %s "),
363 FUNC_NAME
, LIB_NAME
);
365 if ( pfnStrlen("foo") != 3 )
367 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
371 wxPuts(wxT("... ok"));
376 static const wxChar
*FUNC_NAME_AW
= wxT("lstrlen");
378 typedef int (wxSTDCALL
*wxStrlenTypeAorW
)(const wxChar
*);
380 pfnStrlenAorW
= (wxStrlenTypeAorW
)lib
.GetSymbolAorW(FUNC_NAME_AW
);
381 if ( !pfnStrlenAorW
)
383 wxPrintf(wxT("ERROR: function '%s' wasn't found in '%s'.\n"),
384 FUNC_NAME_AW
, LIB_NAME
);
388 if ( pfnStrlenAorW(wxT("foobar")) != 6 )
390 wxPrintf(wxT("ERROR: loaded function is not wxStrlen()!\n"));
397 #if defined(__WXMSW__) || defined(__UNIX__)
399 static void TestDllListLoaded()
401 wxPuts(wxT("*** testing wxDynamicLibrary::ListLoaded() ***\n"));
403 puts("\nLoaded modules:");
404 wxDynamicLibraryDetailsArray dlls
= wxDynamicLibrary::ListLoaded();
405 const size_t count
= dlls
.GetCount();
406 for ( size_t n
= 0; n
< count
; ++n
)
408 const wxDynamicLibraryDetails
& details
= dlls
[n
];
409 printf("%-45s", (const char *)details
.GetPath().mb_str());
411 void *addr
wxDUMMY_INITIALIZE(NULL
);
412 size_t len
wxDUMMY_INITIALIZE(0);
413 if ( details
.GetAddress(&addr
, &len
) )
415 printf(" %08lx:%08lx",
416 (unsigned long)addr
, (unsigned long)((char *)addr
+ len
));
419 printf(" %s\n", (const char *)details
.GetVersion().mb_str());
425 #endif // TEST_DYNLIB
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
433 #include "wx/utils.h"
435 static wxString
MyGetEnv(const wxString
& var
)
438 if ( !wxGetEnv(var
, &val
) )
439 val
= wxT("<empty>");
441 val
= wxString(wxT('\'')) + val
+ wxT('\'');
446 static void TestEnvironment()
448 const wxChar
*var
= wxT("wxTestVar");
450 wxPuts(wxT("*** testing environment access functions ***"));
452 wxPrintf(wxT("Initially getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
453 wxSetEnv(var
, wxT("value for wxTestVar"));
454 wxPrintf(wxT("After wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
455 wxSetEnv(var
, wxT("another value"));
456 wxPrintf(wxT("After 2nd wxSetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
458 wxPrintf(wxT("After wxUnsetEnv: getenv(%s) = %s\n"), var
, MyGetEnv(var
).c_str());
459 wxPrintf(wxT("PATH = %s\n"), MyGetEnv(wxT("PATH")).c_str());
462 #endif // TEST_ENVIRON
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
471 #include "wx/ffile.h"
472 #include "wx/textfile.h"
474 static void TestFileRead()
476 wxPuts(wxT("*** wxFile read test ***"));
478 wxFile
file(wxT("makefile.vc"));
479 if ( file
.IsOpened() )
481 wxPrintf(wxT("File length: %lu\n"), file
.Length());
483 wxPuts(wxT("File dump:\n----------"));
485 static const size_t len
= 1024;
489 size_t nRead
= file
.Read(buf
, len
);
490 if ( nRead
== (size_t)wxInvalidOffset
)
492 wxPrintf(wxT("Failed to read the file."));
496 fwrite(buf
, nRead
, 1, stdout
);
502 wxPuts(wxT("----------"));
506 wxPrintf(wxT("ERROR: can't open test file.\n"));
509 wxPuts(wxEmptyString
);
512 static void TestTextFileRead()
514 wxPuts(wxT("*** wxTextFile read test ***"));
516 wxTextFile
file(wxT("makefile.vc"));
519 wxPrintf(wxT("Number of lines: %u\n"), file
.GetLineCount());
520 wxPrintf(wxT("Last line: '%s'\n"), file
.GetLastLine().c_str());
524 wxPuts(wxT("\nDumping the entire file:"));
525 for ( s
= file
.GetFirstLine(); !file
.Eof(); s
= file
.GetNextLine() )
527 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
529 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
531 wxPuts(wxT("\nAnd now backwards:"));
532 for ( s
= file
.GetLastLine();
533 file
.GetCurrentLine() != 0;
534 s
= file
.GetPrevLine() )
536 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
538 wxPrintf(wxT("%6u: %s\n"), file
.GetCurrentLine() + 1, s
.c_str());
542 wxPrintf(wxT("ERROR: can't open '%s'\n"), file
.GetName());
545 wxPuts(wxEmptyString
);
548 static void TestFileCopy()
550 wxPuts(wxT("*** Testing wxCopyFile ***"));
552 static const wxChar
*filename1
= wxT("makefile.vc");
553 static const wxChar
*filename2
= wxT("test2");
554 if ( !wxCopyFile(filename1
, filename2
) )
556 wxPuts(wxT("ERROR: failed to copy file"));
560 wxFFile
f1(filename1
, wxT("rb")),
561 f2(filename2
, wxT("rb"));
563 if ( !f1
.IsOpened() || !f2
.IsOpened() )
565 wxPuts(wxT("ERROR: failed to open file(s)"));
570 if ( !f1
.ReadAll(&s1
) || !f2
.ReadAll(&s2
) )
572 wxPuts(wxT("ERROR: failed to read file(s)"));
576 if ( (s1
.length() != s2
.length()) ||
577 (memcmp(s1
.c_str(), s2
.c_str(), s1
.length()) != 0) )
579 wxPuts(wxT("ERROR: copy error!"));
583 wxPuts(wxT("File was copied ok."));
589 if ( !wxRemoveFile(filename2
) )
591 wxPuts(wxT("ERROR: failed to remove the file"));
594 wxPuts(wxEmptyString
);
597 static void TestTempFile()
599 wxPuts(wxT("*** wxTempFile test ***"));
602 if ( tmpFile
.Open(wxT("test2")) && tmpFile
.Write(wxT("the answer is 42")) )
604 if ( tmpFile
.Commit() )
605 wxPuts(wxT("File committed."));
607 wxPuts(wxT("ERROR: could't commit temp file."));
609 wxRemoveFile(wxT("test2"));
612 wxPuts(wxEmptyString
);
617 // ----------------------------------------------------------------------------
619 // ----------------------------------------------------------------------------
623 #include "wx/mimetype.h"
625 static void TestMimeEnum()
627 wxPuts(wxT("*** Testing wxMimeTypesManager::EnumAllFileTypes() ***\n"));
629 wxArrayString mimetypes
;
631 size_t count
= wxTheMimeTypesManager
->EnumAllFileTypes(mimetypes
);
633 wxPrintf(wxT("*** All %u known filetypes: ***\n"), count
);
638 for ( size_t n
= 0; n
< count
; n
++ )
640 wxFileType
*filetype
=
641 wxTheMimeTypesManager
->GetFileTypeFromMimeType(mimetypes
[n
]);
644 wxPrintf(wxT(" nothing known about the filetype '%s'!\n"),
645 mimetypes
[n
].c_str());
649 filetype
->GetDescription(&desc
);
650 filetype
->GetExtensions(exts
);
652 filetype
->GetIcon(NULL
);
655 for ( size_t e
= 0; e
< exts
.GetCount(); e
++ )
658 extsAll
<< wxT(", ");
662 wxPrintf(wxT(" %s: %s (%s)\n"),
663 mimetypes
[n
].c_str(), desc
.c_str(), extsAll
.c_str());
666 wxPuts(wxEmptyString
);
669 static void TestMimeFilename()
671 wxPuts(wxT("*** Testing MIME type from filename query ***\n"));
673 static const wxChar
*filenames
[] =
681 for ( size_t n
= 0; n
< WXSIZEOF(filenames
); n
++ )
683 const wxString fname
= filenames
[n
];
684 wxString ext
= fname
.AfterLast(wxT('.'));
685 wxFileType
*ft
= wxTheMimeTypesManager
->GetFileTypeFromExtension(ext
);
688 wxPrintf(wxT("WARNING: extension '%s' is unknown.\n"), ext
.c_str());
693 if ( !ft
->GetDescription(&desc
) )
694 desc
= wxT("<no description>");
697 if ( !ft
->GetOpenCommand(&cmd
,
698 wxFileType::MessageParameters(fname
, wxEmptyString
)) )
699 cmd
= wxT("<no command available>");
701 cmd
= wxString(wxT('"')) + cmd
+ wxT('"');
703 wxPrintf(wxT("To open %s (%s) run:\n %s\n"),
704 fname
.c_str(), desc
.c_str(), cmd
.c_str());
710 wxPuts(wxEmptyString
);
713 static void TestMimeAssociate()
715 wxPuts(wxT("*** Testing creation of filetype association ***\n"));
717 wxFileTypeInfo
ftInfo(
718 wxT("application/x-xyz"),
719 wxT("xyzview '%s'"), // open cmd
720 wxT(""), // print cmd
721 wxT("XYZ File"), // description
722 wxT(".xyz"), // extensions
723 wxNullPtr
// end of extensions
725 ftInfo
.SetShortDesc(wxT("XYZFile")); // used under Win32 only
727 wxFileType
*ft
= wxTheMimeTypesManager
->Associate(ftInfo
);
730 wxPuts(wxT("ERROR: failed to create association!"));
734 // TODO: read it back
738 wxPuts(wxEmptyString
);
744 // ----------------------------------------------------------------------------
745 // misc information functions
746 // ----------------------------------------------------------------------------
748 #ifdef TEST_INFO_FUNCTIONS
750 #include "wx/utils.h"
753 static void TestDiskInfo()
755 wxPuts(wxT("*** Testing wxGetDiskSpace() ***"));
759 wxChar pathname
[128];
760 wxPrintf(wxT("\nEnter a directory name (or 'quit' to escape): "));
761 if ( !wxFgets(pathname
, WXSIZEOF(pathname
), stdin
) )
764 // kill the last '\n'
765 pathname
[wxStrlen(pathname
) - 1] = 0;
767 if (wxStrcmp(pathname
, "quit") == 0)
770 wxLongLong total
, free
;
771 if ( !wxGetDiskSpace(pathname
, &total
, &free
) )
773 wxPuts(wxT("ERROR: wxGetDiskSpace failed."));
777 wxPrintf(wxT("%sKb total, %sKb free on '%s'.\n"),
778 (total
/ 1024).ToString().c_str(),
779 (free
/ 1024).ToString().c_str(),
784 #endif // TEST_INTERACTIVE
786 static void TestOsInfo()
788 wxPuts(wxT("*** Testing OS info functions ***\n"));
791 wxGetOsVersion(&major
, &minor
);
792 wxPrintf(wxT("Running under: %s, version %d.%d\n"),
793 wxGetOsDescription().c_str(), major
, minor
);
795 wxPrintf(wxT("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong());
797 wxPrintf(wxT("Host name is %s (%s).\n"),
798 wxGetHostName().c_str(), wxGetFullHostName().c_str());
800 wxPuts(wxEmptyString
);
803 static void TestPlatformInfo()
805 wxPuts(wxT("*** Testing wxPlatformInfo functions ***\n"));
810 wxPrintf(wxT("Operating system family name is: %s\n"), plat
.GetOperatingSystemFamilyName().c_str());
811 wxPrintf(wxT("Operating system name is: %s\n"), plat
.GetOperatingSystemIdName().c_str());
812 wxPrintf(wxT("Port ID name is: %s\n"), plat
.GetPortIdName().c_str());
813 wxPrintf(wxT("Port ID short name is: %s\n"), plat
.GetPortIdShortName().c_str());
814 wxPrintf(wxT("Architecture is: %s\n"), plat
.GetArchName().c_str());
815 wxPrintf(wxT("Endianness is: %s\n"), plat
.GetEndiannessName().c_str());
817 wxPuts(wxEmptyString
);
820 static void TestUserInfo()
822 wxPuts(wxT("*** Testing user info functions ***\n"));
824 wxPrintf(wxT("User id is:\t%s\n"), wxGetUserId().c_str());
825 wxPrintf(wxT("User name is:\t%s\n"), wxGetUserName().c_str());
826 wxPrintf(wxT("Home dir is:\t%s\n"), wxGetHomeDir().c_str());
827 wxPrintf(wxT("Email address:\t%s\n"), wxGetEmailAddress().c_str());
829 wxPuts(wxEmptyString
);
832 #endif // TEST_INFO_FUNCTIONS
834 // ----------------------------------------------------------------------------
835 // regular expressions
836 // ----------------------------------------------------------------------------
838 #if defined TEST_REGEX && TEST_INTERACTIVE
840 #include "wx/regex.h"
842 static void TestRegExInteractive()
844 wxPuts(wxT("*** Testing RE interactively ***"));
849 wxPrintf(wxT("\nEnter a pattern (or 'quit' to escape): "));
850 if ( !wxFgets(pattern
, WXSIZEOF(pattern
), stdin
) )
853 // kill the last '\n'
854 pattern
[wxStrlen(pattern
) - 1] = 0;
856 if (wxStrcmp(pattern
, "quit") == 0)
860 if ( !re
.Compile(pattern
) )
868 wxPrintf(wxT("Enter text to match: "));
869 if ( !wxFgets(text
, WXSIZEOF(text
), stdin
) )
872 // kill the last '\n'
873 text
[wxStrlen(text
) - 1] = 0;
875 if ( !re
.Matches(text
) )
877 wxPrintf(wxT("No match.\n"));
881 wxPrintf(wxT("Pattern matches at '%s'\n"), re
.GetMatch(text
).c_str());
884 for ( size_t n
= 1; ; n
++ )
886 if ( !re
.GetMatch(&start
, &len
, n
) )
891 wxPrintf(wxT("Subexpr %u matched '%s'\n"),
892 n
, wxString(text
+ start
, len
).c_str());
901 // ----------------------------------------------------------------------------
903 // ----------------------------------------------------------------------------
907 #include "wx/protocol/ftp.h"
908 #include "wx/protocol/log.h"
910 #define FTP_ANONYMOUS
915 static const wxChar
*hostname
= wxT("ftp.wxwidgets.org");
917 static const wxChar
*hostname
= "localhost";
920 static bool TestFtpConnect()
922 wxPuts(wxT("*** Testing FTP connect ***"));
925 wxPrintf(wxT("--- Attempting to connect to %s:21 anonymously...\n"), hostname
);
926 #else // !FTP_ANONYMOUS
928 wxFgets(user
, WXSIZEOF(user
), stdin
);
929 user
[wxStrlen(user
) - 1] = '\0'; // chop off '\n'
932 wxChar password
[256];
933 wxPrintf(wxT("Password for %s: "), password
);
934 wxFgets(password
, WXSIZEOF(password
), stdin
);
935 password
[wxStrlen(password
) - 1] = '\0'; // chop off '\n'
936 ftp
->SetPassword(password
);
938 wxPrintf(wxT("--- Attempting to connect to %s:21 as %s...\n"), hostname
, user
);
939 #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
941 if ( !ftp
->Connect(hostname
) )
943 wxPrintf(wxT("ERROR: failed to connect to %s\n"), hostname
);
949 wxPrintf(wxT("--- Connected to %s, current directory is '%s'\n"),
950 hostname
, ftp
->Pwd().c_str());
958 static void TestFtpInteractive()
960 wxPuts(wxT("\n*** Interactive wxFTP test ***"));
966 wxPrintf(wxT("Enter FTP command (or 'quit' to escape): "));
967 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
970 // kill the last '\n'
971 buf
[wxStrlen(buf
) - 1] = 0;
973 // special handling of LIST and NLST as they require data connection
974 wxString
start(buf
, 4);
976 if ( start
== wxT("LIST") || start
== wxT("NLST") )
979 if ( wxStrlen(buf
) > 4 )
983 if ( !ftp
->GetList(files
, wildcard
, start
== wxT("LIST")) )
985 wxPrintf(wxT("ERROR: failed to get %s of files\n"), start
.c_str());
989 wxPrintf(wxT("--- %s of '%s' under '%s':\n"),
990 start
.c_str(), wildcard
.c_str(), ftp
->Pwd().c_str());
991 size_t count
= files
.GetCount();
992 for ( size_t n
= 0; n
< count
; n
++ )
994 wxPrintf(wxT("\t%s\n"), files
[n
].c_str());
996 wxPuts(wxT("--- End of the file list"));
999 else if ( start
== wxT("QUIT") )
1001 break; // get out of here!
1005 wxChar ch
= ftp
->SendCommand(buf
);
1006 wxPrintf(wxT("Command %s"), ch
? wxT("succeeded") : wxT("failed"));
1009 wxPrintf(wxT(" (return code %c)"), ch
);
1012 wxPrintf(wxT(", server reply:\n%s\n\n"), ftp
->GetLastResult().c_str());
1018 #endif // TEST_INTERACTIVE
1021 // ----------------------------------------------------------------------------
1023 // ----------------------------------------------------------------------------
1025 #ifdef TEST_STACKWALKER
1027 #if wxUSE_STACKWALKER
1029 #include "wx/stackwalk.h"
1031 class StackDump
: public wxStackWalker
1034 StackDump(const char *argv0
)
1035 : wxStackWalker(argv0
)
1039 virtual void Walk(size_t skip
= 1, size_t maxdepth
= wxSTACKWALKER_MAX_DEPTH
)
1041 wxPuts(wxT("Stack dump:"));
1043 wxStackWalker::Walk(skip
, maxdepth
);
1047 virtual void OnStackFrame(const wxStackFrame
& frame
)
1049 printf("[%2d] ", (int) frame
.GetLevel());
1051 wxString name
= frame
.GetName();
1052 if ( !name
.empty() )
1054 printf("%-20.40s", (const char*)name
.mb_str());
1058 printf("0x%08lx", (unsigned long)frame
.GetAddress());
1061 if ( frame
.HasSourceLocation() )
1064 (const char*)frame
.GetFileName().mb_str(),
1065 (int)frame
.GetLine());
1071 for ( size_t n
= 0; frame
.GetParam(n
, &type
, &name
, &val
); n
++ )
1073 printf("\t%s %s = %s\n", (const char*)type
.mb_str(),
1074 (const char*)name
.mb_str(),
1075 (const char*)val
.mb_str());
1080 static void TestStackWalk(const char *argv0
)
1082 wxPuts(wxT("*** Testing wxStackWalker ***"));
1084 StackDump
dump(argv0
);
1090 #endif // wxUSE_STACKWALKER
1092 #endif // TEST_STACKWALKER
1094 // ----------------------------------------------------------------------------
1096 // ----------------------------------------------------------------------------
1098 #ifdef TEST_STDPATHS
1100 #include "wx/stdpaths.h"
1101 #include "wx/wxchar.h" // wxPrintf
1103 static void TestStandardPaths()
1105 wxPuts(wxT("*** Testing wxStandardPaths ***"));
1107 wxTheApp
->SetAppName(wxT("console"));
1109 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
1110 wxPrintf(wxT("Config dir (sys):\t%s\n"), stdp
.GetConfigDir().c_str());
1111 wxPrintf(wxT("Config dir (user):\t%s\n"), stdp
.GetUserConfigDir().c_str());
1112 wxPrintf(wxT("Data dir (sys):\t\t%s\n"), stdp
.GetDataDir().c_str());
1113 wxPrintf(wxT("Data dir (sys local):\t%s\n"), stdp
.GetLocalDataDir().c_str());
1114 wxPrintf(wxT("Data dir (user):\t%s\n"), stdp
.GetUserDataDir().c_str());
1115 wxPrintf(wxT("Data dir (user local):\t%s\n"), stdp
.GetUserLocalDataDir().c_str());
1116 wxPrintf(wxT("Documents dir:\t\t%s\n"), stdp
.GetDocumentsDir().c_str());
1117 wxPrintf(wxT("Executable path:\t%s\n"), stdp
.GetExecutablePath().c_str());
1118 wxPrintf(wxT("Plugins dir:\t\t%s\n"), stdp
.GetPluginsDir().c_str());
1119 wxPrintf(wxT("Resources dir:\t\t%s\n"), stdp
.GetResourcesDir().c_str());
1120 wxPrintf(wxT("Localized res. dir:\t%s\n"),
1121 stdp
.GetLocalizedResourcesDir(wxT("fr")).c_str());
1122 wxPrintf(wxT("Message catalogs dir:\t%s\n"),
1123 stdp
.GetLocalizedResourcesDir
1126 wxStandardPaths::ResourceCat_Messages
1132 #endif // TEST_STDPATHS
1134 // ----------------------------------------------------------------------------
1136 // ----------------------------------------------------------------------------
1138 #if !defined(__WIN32__) || !wxUSE_FSVOLUME
1144 #include "wx/volume.h"
1146 static const wxChar
*volumeKinds
[] =
1152 wxT("network volume"),
1153 wxT("other volume"),
1156 static void TestFSVolume()
1158 wxPuts(wxT("*** Testing wxFSVolume class ***"));
1160 wxArrayString volumes
= wxFSVolume::GetVolumes();
1161 size_t count
= volumes
.GetCount();
1165 wxPuts(wxT("ERROR: no mounted volumes?"));
1169 wxPrintf(wxT("%u mounted volumes found:\n"), count
);
1171 for ( size_t n
= 0; n
< count
; n
++ )
1173 wxFSVolume
vol(volumes
[n
]);
1176 wxPuts(wxT("ERROR: couldn't create volume"));
1180 wxPrintf(wxT("%u: %s (%s), %s, %s, %s\n"),
1182 vol
.GetDisplayName().c_str(),
1183 vol
.GetName().c_str(),
1184 volumeKinds
[vol
.GetKind()],
1185 vol
.IsWritable() ? wxT("rw") : wxT("ro"),
1186 vol
.GetFlags() & wxFS_VOL_REMOVABLE
? wxT("removable")
1193 #endif // TEST_VOLUME
1195 // ----------------------------------------------------------------------------
1197 // ----------------------------------------------------------------------------
1199 #ifdef TEST_DATETIME
1201 #include "wx/math.h"
1202 #include "wx/datetime.h"
1204 #if TEST_INTERACTIVE
1206 static void TestDateTimeInteractive()
1208 wxPuts(wxT("\n*** interactive wxDateTime tests ***"));
1214 wxPrintf(wxT("Enter a date (or 'quit' to escape): "));
1215 if ( !wxFgets(buf
, WXSIZEOF(buf
), stdin
) )
1218 // kill the last '\n'
1219 buf
[wxStrlen(buf
) - 1] = 0;
1221 if ( wxString(buf
).CmpNoCase("quit") == 0 )
1225 const wxChar
*p
= dt
.ParseDate(buf
);
1228 wxPrintf(wxT("ERROR: failed to parse the date '%s'.\n"), buf
);
1234 wxPrintf(wxT("WARNING: parsed only first %u characters.\n"), p
- buf
);
1237 wxPrintf(wxT("%s: day %u, week of month %u/%u, week of year %u\n"),
1238 dt
.Format(wxT("%b %d, %Y")).c_str(),
1240 dt
.GetWeekOfMonth(wxDateTime::Monday_First
),
1241 dt
.GetWeekOfMonth(wxDateTime::Sunday_First
),
1242 dt
.GetWeekOfYear(wxDateTime::Monday_First
));
1248 #endif // TEST_INTERACTIVE
1249 #endif // TEST_DATETIME
1251 // ----------------------------------------------------------------------------
1253 // ----------------------------------------------------------------------------
1255 #ifdef TEST_SNGLINST
1257 #include "wx/snglinst.h"
1259 static bool TestSingleIstance()
1261 wxPuts(wxT("\n*** Testing wxSingleInstanceChecker ***"));
1263 wxSingleInstanceChecker checker
;
1264 if ( checker
.Create(wxT(".wxconsole.lock")) )
1266 if ( checker
.IsAnotherRunning() )
1268 wxPrintf(wxT("Another instance of the program is running, exiting.\n"));
1273 // wait some time to give time to launch another instance
1274 wxPuts(wxT("If you try to run another instance of this program now, it won't start."));
1275 wxPrintf(wxT("Press \"Enter\" to exit wxSingleInstanceChecker test and proceed..."));
1278 else // failed to create
1280 wxPrintf(wxT("Failed to init wxSingleInstanceChecker.\n"));
1287 #endif // TEST_SNGLINST
1290 // ----------------------------------------------------------------------------
1292 // ----------------------------------------------------------------------------
1294 int main(int argc
, char **argv
)
1297 wxChar
**wxArgv
= new wxChar
*[argc
+ 1];
1302 for (n
= 0; n
< argc
; n
++ )
1304 wxMB2WXbuf warg
= wxConvertMB2WX(argv
[n
]);
1305 wxArgv
[n
] = wxStrdup(warg
);
1310 #else // !wxUSE_UNICODE
1312 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1314 wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE
, "program");
1316 wxInitializer initializer
;
1319 fprintf(stderr
, "Failed to initialize the wxWidgets library, aborting.");
1324 #ifdef TEST_SNGLINST
1325 if (!TestSingleIstance())
1327 #endif // TEST_SNGLINST
1339 TestDllListLoaded();
1340 #endif // TEST_DYNLIB
1344 #endif // TEST_ENVIRON
1354 wxLog::AddTraceMask(FTP_TRACE_MASK
);
1356 // wxFTP cannot be a static variable as its ctor needs to access
1357 // wxWidgets internals after it has been initialized
1359 ftp
->SetLog(new wxProtocolLog(FTP_TRACE_MASK
));
1360 if ( TestFtpConnect() )
1361 TestFtpInteractive();
1362 //else: connecting to the FTP server failed
1369 TestMimeAssociate();
1373 #ifdef TEST_INFO_FUNCTIONS
1378 #if TEST_INTERACTIVE
1381 #endif // TEST_INFO_FUNCTIONS
1385 #endif // TEST_PRINTF
1387 #if defined TEST_REGEX && TEST_INTERACTIVE
1388 TestRegExInteractive();
1389 #endif // defined TEST_REGEX && TEST_INTERACTIVE
1391 #ifdef TEST_DATETIME
1392 #if TEST_INTERACTIVE
1393 TestDateTimeInteractive();
1395 #endif // TEST_DATETIME
1397 #ifdef TEST_STACKWALKER
1398 #if wxUSE_STACKWALKER
1399 TestStackWalk(argv
[0]);
1401 #endif // TEST_STACKWALKER
1403 #ifdef TEST_STDPATHS
1404 TestStandardPaths();
1409 #endif // TEST_VOLUME
1413 for ( int n
= 0; n
< argc
; n
++ )
1418 #endif // wxUSE_UNICODE