X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/b1859b1a1b35194abb9d55a35d8bad202db39776..e4de825ee6f3f991e2aa86159d6e3457afb6a8e0:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 1744e90776..8a9a0705c6 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -9,6 +9,57 @@ // Licence: wxWindows license ///////////////////////////////////////////////////////////////////////////// +// IMPORTANT NOTE FOR WXWIDGETS USERS: +// If you're a wxWidgets user and you're looking at this file to learn how to +// structure a wxWidgets console application, then you don't have much to learn. +// This application is used more for testing rather than as sample but +// basically the following simple block is enough for you to start your +// own console application: + +/* + int main(int argc, char **argv) + { + wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program"); + + wxInitializer initializer; + if ( !initializer ) + { + fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); + return -1; + } + + static const wxCmdLineEntryDesc cmdLineDesc[] = + { + { wxCMD_LINE_SWITCH, "h", "help", "show this help message", + wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, + // ... your other command line options here... + + { wxCMD_LINE_NONE } + }; + + wxCmdLineParser parser(cmdLineDesc, argc, wxArgv); + switch ( parser.Parse() ) + { + case -1: + wxLogMessage(_T("Help was given, terminating.")); + break; + + case 0: + // everything is ok; proceed + break; + + default: + wxLogMessage(_T("Syntax error detected, aborting.")); + break; + } + + // do something useful here + + return 0; + } +*/ + + // ============================================================================ // declarations // ============================================================================ @@ -23,6 +74,7 @@ #include "wx/string.h" #include "wx/file.h" +#include "wx/filename.h" #include "wx/app.h" #include "wx/log.h" #include "wx/apptrait.h" @@ -49,7 +101,7 @@ // what to test (in alphabetic order)? Define TEST_ALL to 0 to do a single // test, define it to 1 to do all tests. -#define TEST_ALL 0 +#define TEST_ALL 1 #if TEST_ALL @@ -63,7 +115,7 @@ #define TEST_FILECONF #define TEST_FILENAME #define TEST_FILETIME - // #define TEST_FTP --FIXME! (RN) + #define TEST_FTP #define TEST_INFO_FUNCTIONS #define TEST_LOCALE #define TEST_LOG @@ -88,7 +140,7 @@ #define TEST_WCHAR #define TEST_ZIP #else // #if TEST_ALL - #define TEST_CMDLINE + #define TEST_FTP #endif // some tests are interactive, define this to run them @@ -304,7 +356,7 @@ public: virtual wxDirTraverseResult OnDir(const wxString& dirname) { wxString path, name, ext; - wxSplitPath(dirname, &path, &name, &ext); + wxFileName::SplitPath(dirname, &path, &name, &ext); if ( !ext.empty() ) name << _T('.') << ext; @@ -468,7 +520,7 @@ static void TestDllListLoaded() for ( size_t n = 0; n < count; ++n ) { const wxDynamicLibraryDetails& details = dlls[n]; - printf("%-45s", details.GetPath().mb_str()); + printf("%-45s", (const char *)details.GetPath().mb_str()); void *addr; size_t len; @@ -478,7 +530,7 @@ static void TestDllListLoaded() (unsigned long)addr, (unsigned long)((char *)addr + len)); } - printf(" %s\n", details.GetVersion().mb_str()); + printf(" %s\n", (const char *)details.GetVersion().mb_str()); } } @@ -536,11 +588,13 @@ static void TestExecute() wxPuts(_T("*** testing wxExecute ***")); #ifdef __UNIX__ - #define COMMAND "cat -n ../../Makefile" // "echo hi" + #define COMMAND "echo hi" + #define ASYNC_COMMAND "xclock" #define SHELL_COMMAND "echo hi from shell" - #define REDIRECT_COMMAND COMMAND // "date" + #define REDIRECT_COMMAND "cat -n Makefile" #elif defined(__WXMSW__) #define COMMAND "command.com /c echo hi" + #define ASYNC_COMMAND "notepad" #define SHELL_COMMAND "echo hi" #define REDIRECT_COMMAND COMMAND #else @@ -556,19 +610,22 @@ static void TestExecute() wxPrintf(_T("Testing wxExecute: ")); fflush(stdout); - if ( wxExecute(_T(COMMAND), true /* sync */) == 0 ) + if ( wxExecute(_T(COMMAND), wxEXEC_SYNC) == 0 ) wxPuts(_T("Ok.")); else wxPuts(_T("ERROR.")); -#if 0 // no, it doesn't work (yet?) wxPrintf(_T("Testing async wxExecute: ")); fflush(stdout); - if ( wxExecute(COMMAND) != 0 ) + int pid = wxExecute(ASYNC_COMMAND); + if ( pid != 0 ) + { wxPuts(_T("Ok (command launched).")); + if ( wxKill(pid) == -1 ) + wxPuts("ERROR: failed to kill child process."); + } else wxPuts(_T("ERROR.")); -#endif // 0 wxPrintf(_T("Testing wxExecute with redirection:\n")); wxArrayString output; @@ -578,10 +635,25 @@ static void TestExecute() } else { - size_t count = output.GetCount(); - for ( size_t n = 0; n < count; n++ ) + // don't show too much output, MAX_LINES is enough + static const unsigned MAX_LINES = 20; + + const unsigned count = output.size(); + for ( unsigned n = 0; + n < (count > MAX_LINES ? MAX_LINES/2 : count); + n++ ) { - wxPrintf(_T("\t%s\n"), output[n].c_str()); + wxPrintf("%04u:\t%s\n", n + 1, output[n]); + } + + if ( count > MAX_LINES ) + { + wxPrintf("... skipping %u lines...\n", count - MAX_LINES); + + for ( unsigned n = count - MAX_LINES/2; n < count; n++ ) + { + wxPrintf("%04u:\t%s\n", n + 1, output[n]); + } } wxPuts(_T("Ok.")); @@ -967,7 +1039,10 @@ static void TestFileSetTimes() #include "wx/intl.h" #include "wx/utils.h" // for wxSetEnv -static wxLocale gs_localeDefault(wxLANGUAGE_ENGLISH); +static wxLocale gs_localeDefault; + // NOTE: don't init it here as it needs a wxAppTraits object + // and thus must be init-ed after creation of the wxInitializer + // class in the main() // find the name of the language from its value static const wxChar *GetLangName(int lang) @@ -1216,6 +1291,8 @@ static void TestDefaultLang() { wxPuts(_T("*** Testing wxLocale::GetSystemLanguage ***")); + gs_localeDefault.Init(wxLANGUAGE_ENGLISH); + static const wxChar *langStrings[] = { NULL, // system default @@ -1305,32 +1382,6 @@ static void TestMimeEnum() wxPuts(wxEmptyString); } -static void TestMimeOverride() -{ - wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n")); - - static const wxChar *mailcap = _T("/tmp/mailcap"); - static const wxChar *mimetypes = _T("/tmp/mime.types"); - - if ( wxFile::Exists(mailcap) ) - wxPrintf(_T("Loading mailcap from '%s': %s\n"), - mailcap, - wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR")); - else - wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"), - mailcap); - - if ( wxFile::Exists(mimetypes) ) - wxPrintf(_T("Loading mime.types from '%s': %s\n"), - mimetypes, - wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR")); - else - wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"), - mimetypes); - - wxPuts(wxEmptyString); -} - static void TestMimeFilename() { wxPuts(_T("*** Testing MIME type from filename query ***\n")); @@ -1375,6 +1426,35 @@ static void TestMimeFilename() wxPuts(wxEmptyString); } +// these tests were broken by wxMimeTypesManager changes, temporarily disabling +#if 0 + +static void TestMimeOverride() +{ + wxPuts(_T("*** Testing wxMimeTypesManager additional files loading ***\n")); + + static const wxChar *mailcap = _T("/tmp/mailcap"); + static const wxChar *mimetypes = _T("/tmp/mime.types"); + + if ( wxFile::Exists(mailcap) ) + wxPrintf(_T("Loading mailcap from '%s': %s\n"), + mailcap, + wxTheMimeTypesManager->ReadMailcap(mailcap) ? _T("ok") : _T("ERROR")); + else + wxPrintf(_T("WARN: mailcap file '%s' doesn't exist, not loaded.\n"), + mailcap); + + if ( wxFile::Exists(mimetypes) ) + wxPrintf(_T("Loading mime.types from '%s': %s\n"), + mimetypes, + wxTheMimeTypesManager->ReadMimeTypes(mimetypes) ? _T("ok") : _T("ERROR")); + else + wxPrintf(_T("WARN: mime.types file '%s' doesn't exist, not loaded.\n"), + mimetypes); + + wxPuts(wxEmptyString); +} + static void TestMimeAssociate() { wxPuts(_T("*** Testing creation of filetype association ***\n")); @@ -1403,6 +1483,8 @@ static void TestMimeAssociate() wxPuts(wxEmptyString); } +#endif // 0 + #endif // TEST_MIME // ---------------------------------------------------------------------------- @@ -1601,7 +1683,7 @@ static void TestPathList() // regular expressions // ---------------------------------------------------------------------------- -#ifdef TEST_REGEX +#if defined TEST_REGEX && TEST_INTERACTIVE #include "wx/regex.h" @@ -2063,7 +2145,7 @@ static void TestRegConfWrite() static void TestRegConfRead() { - wxConfig *config = new wxConfig(_T("myapp")); + wxRegConfig *config = new wxRegConfig(_T("myapp")); wxString str; long dummy; @@ -2374,10 +2456,10 @@ static void TestSocketClient() #include "wx/protocol/ftp.h" -static wxFTP ftp; - #define FTP_ANONYMOUS +static wxFTP *ftp; + #ifdef FTP_ANONYMOUS static const wxChar *directory = _T("/pub"); static const wxChar *filename = _T("welcome.msg"); @@ -2400,18 +2482,18 @@ static bool TestFtpConnect() wxChar user[256]; wxFgets(user, WXSIZEOF(user), stdin); user[wxStrlen(user) - 1] = '\0'; // chop off '\n' - ftp.SetUser(user); + ftp->SetUser(user); wxChar password[256]; wxPrintf(_T("Password for %s: "), password); wxFgets(password, WXSIZEOF(password), stdin); password[wxStrlen(password) - 1] = '\0'; // chop off '\n' - ftp.SetPassword(password); + ftp->SetPassword(password); wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user); #endif // FTP_ANONYMOUS/!FTP_ANONYMOUS - if ( !ftp.Connect(hostname) ) + if ( !ftp->Connect(hostname) ) { wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname); @@ -2420,72 +2502,34 @@ static bool TestFtpConnect() else { wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"), - hostname, ftp.Pwd().c_str()); - ftp.Close(); + hostname, ftp->Pwd().c_str()); + ftp->Close(); } return true; } -// test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0? -static void TestFtpWuFtpd() -{ - wxFTP ftp; - static const wxChar *hostname = _T("ftp.eudora.com"); - if ( !ftp.Connect(hostname) ) - { - wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname); - } - else - { - static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt"); - wxInputStream *in = ftp.GetInputStream(filename); - if ( !in ) - { - wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename); - } - else - { - size_t size = in->GetSize(); - wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size); - - wxChar *data = new wxChar[size]; - if ( !in->Read(data, size) ) - { - wxPuts(_T("ERROR: read error")); - } - else - { - wxPrintf(_T("Successfully retrieved the file.\n")); - } - - delete [] data; - delete in; - } - } -} - static void TestFtpList() { wxPuts(_T("*** Testing wxFTP file listing ***\n")); // test CWD - if ( !ftp.ChDir(directory) ) + if ( !ftp->ChDir(directory) ) { wxPrintf(_T("ERROR: failed to cd to %s\n"), directory); } - wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str()); + wxPrintf(_T("Current directory is '%s'\n"), ftp->Pwd().c_str()); // test NLIST and LIST wxArrayString files; - if ( !ftp.GetFilesList(files) ) + if ( !ftp->GetFilesList(files) ) { wxPuts(_T("ERROR: failed to get NLIST of files")); } else { - wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str()); + wxPrintf(_T("Brief list of files under '%s':\n"), ftp->Pwd().c_str()); size_t count = files.GetCount(); for ( size_t n = 0; n < count; n++ ) { @@ -2494,13 +2538,13 @@ static void TestFtpList() wxPuts(_T("End of the file list")); } - if ( !ftp.GetDirList(files) ) + if ( !ftp->GetDirList(files) ) { wxPuts(_T("ERROR: failed to get LIST of files")); } else { - wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str()); + wxPrintf(_T("Detailed list of files under '%s':\n"), ftp->Pwd().c_str()); size_t count = files.GetCount(); for ( size_t n = 0; n < count; n++ ) { @@ -2509,12 +2553,12 @@ static void TestFtpList() wxPuts(_T("End of the file list")); } - if ( !ftp.ChDir(_T("..")) ) + if ( !ftp->ChDir(_T("..")) ) { wxPuts(_T("ERROR: failed to cd to ..")); } - wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str()); + wxPrintf(_T("Current directory is '%s'\n"), ftp->Pwd().c_str()); } static void TestFtpDownload() @@ -2522,7 +2566,7 @@ static void TestFtpDownload() wxPuts(_T("*** Testing wxFTP download ***\n")); // test RETR - wxInputStream *in = ftp.GetInputStream(filename); + wxInputStream *in = ftp->GetInputStream(filename); if ( !in ) { wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename); @@ -2552,16 +2596,16 @@ static void TestFtpFileSize() { wxPuts(_T("*** Testing FTP SIZE command ***")); - if ( !ftp.ChDir(directory) ) + if ( !ftp->ChDir(directory) ) { wxPrintf(_T("ERROR: failed to cd to %s\n"), directory); } - wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str()); + wxPrintf(_T("Current directory is '%s'\n"), ftp->Pwd().c_str()); - if ( ftp.FileExists(filename) ) + if ( ftp->FileExists(filename) ) { - int size = ftp.GetFileSize(filename); + int size = ftp->GetFileSize(filename); if ( size == -1 ) wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename); else @@ -2577,26 +2621,28 @@ static void TestFtpMisc() { wxPuts(_T("*** Testing miscellaneous wxFTP functions ***")); - if ( ftp.SendCommand(_T("STAT")) != '2' ) + if ( ftp->SendCommand(_T("STAT")) != '2' ) { wxPuts(_T("ERROR: STAT failed")); } else { - wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str()); + wxPrintf(_T("STAT returned:\n\n%s\n"), ftp->GetLastResult().c_str()); } - if ( ftp.SendCommand(_T("HELP SITE")) != '2' ) + if ( ftp->SendCommand(_T("HELP SITE")) != '2' ) { wxPuts(_T("ERROR: HELP SITE failed")); } else { wxPrintf(_T("The list of site-specific commands:\n\n%s\n"), - ftp.GetLastResult().c_str()); + ftp->GetLastResult().c_str()); } } +#if TEST_INTERACTIVE + static void TestFtpInteractive() { wxPuts(_T("\n*** Interactive wxFTP test ***")); @@ -2622,14 +2668,14 @@ static void TestFtpInteractive() wildcard = buf + 5; wxArrayString files; - if ( !ftp.GetList(files, wildcard, start == _T("LIST")) ) + if ( !ftp->GetList(files, wildcard, start == _T("LIST")) ) { wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str()); } else { wxPrintf(_T("--- %s of '%s' under '%s':\n"), - start.c_str(), wildcard.c_str(), ftp.Pwd().c_str()); + start.c_str(), wildcard.c_str(), ftp->Pwd().c_str()); size_t count = files.GetCount(); for ( size_t n = 0; n < count; n++ ) { @@ -2640,20 +2686,22 @@ static void TestFtpInteractive() } else // !list { - wxChar ch = ftp.SendCommand(buf); + wxChar ch = ftp->SendCommand(buf); wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed")); if ( ch ) { wxPrintf(_T(" (return code %c)"), ch); } - wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str()); + wxPrintf(_T(", server reply:\n%s\n\n"), ftp->GetLastResult().c_str()); } } wxPuts(_T("\n*** done ***")); } +#endif // TEST_INTERACTIVE + static void TestFtpUpload() { wxPuts(_T("*** Testing wxFTP uploading ***\n")); @@ -2661,7 +2709,7 @@ static void TestFtpUpload() // upload a file static const wxChar *file1 = _T("test1"); static const wxChar *file2 = _T("test2"); - wxOutputStream *out = ftp.GetOutputStream(file1); + wxOutputStream *out = ftp->GetOutputStream(file1); if ( out ) { wxPrintf(_T("--- Uploading to %s ---\n"), file1); @@ -2670,17 +2718,17 @@ static void TestFtpUpload() } // send a command to check the remote file - if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' ) + if ( ftp->SendCommand(wxString(_T("STAT ")) + file1) != '2' ) { wxPrintf(_T("ERROR: STAT %s failed\n"), file1); } else { wxPrintf(_T("STAT %s returned:\n\n%s\n"), - file1, ftp.GetLastResult().c_str()); + file1, ftp->GetLastResult().c_str()); } - out = ftp.GetOutputStream(file2); + out = ftp->GetOutputStream(file2); if ( out ) { wxPrintf(_T("--- Uploading to %s ---\n"), file1); @@ -2709,22 +2757,22 @@ public: { } - virtual void Walk(size_t skip = 1) + virtual void Walk(size_t skip = 1, size_t maxdepth = wxSTACKWALKER_MAX_DEPTH) { wxPuts(_T("Stack dump:")); - wxStackWalker::Walk(skip); + wxStackWalker::Walk(skip, maxdepth); } protected: virtual void OnStackFrame(const wxStackFrame& frame) { - printf("[%2d] ", frame.GetLevel()); + printf("[%2d] ", (int) frame.GetLevel()); wxString name = frame.GetName(); if ( !name.empty() ) { - printf("%-20.40s", name.mb_str()); + printf("%-20.40s", (const char*)name.mb_str()); } else { @@ -2734,8 +2782,8 @@ protected: if ( frame.HasSourceLocation() ) { printf("\t%s:%d", - frame.GetFileName().mb_str(), - frame.GetLine()); + (const char*)frame.GetFileName().mb_str(), + (int)frame.GetLine()); } puts(""); @@ -2743,7 +2791,9 @@ protected: wxString type, val; for ( size_t n = 0; frame.GetParam(n, &type, &name, &val); n++ ) { - printf("\t%s %s = %s\n", type.mb_str(), name.mb_str(), val.mb_str()); + printf("\t%s %s = %s\n", (const char*)type.mb_str(), + (const char*)name.mb_str(), + (const char*)val.mb_str()); } } }; @@ -2820,9 +2870,10 @@ static void TestFileStream() { wxFileInputStream fsIn(filename); wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize()); - while ( !fsIn.Eof() ) + int c; + while ( (c=fsIn.GetC()) != wxEOF ) { - wxPutchar(fsIn.GetC()); + wxPutchar(c); } } @@ -2857,9 +2908,10 @@ static void TestMemoryStream() wxMemoryInputStream memInpStream(buf, len); wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize()); - while ( !memInpStream.Eof() ) + int c; + while ( (c=memInpStream.GetC()) != wxEOF ) { - wxPutchar(memInpStream.GetC()); + wxPutchar(c); } wxPuts(_T("\n*** wxMemoryInputStream test done ***")); @@ -3408,13 +3460,18 @@ static void TestZipStreamRead() wxPuts(_T("*** Testing ZIP reading ***\n")); static const wxString filename = _T("foo"); - wxZipInputStream istr(TESTFILE_ZIP, filename); + wxFFileInputStream in(TESTFILE_ZIP); + wxZipInputStream istr(in); + wxZipEntry entry(filename); + istr.OpenEntry(entry); + wxPrintf(_T("Archive size: %u\n"), istr.GetSize()); wxPrintf(_T("Dumping the file '%s':\n"), filename.c_str()); - while ( !istr.Eof() ) + int c; + while ( (c=istr.GetC()) != wxEOF ) { - wxPutchar(istr.GetC()); + wxPutchar(c); fflush(stdout); } @@ -4091,39 +4148,6 @@ static void TestThreadConditions() wxThread::Sleep(500); } -#include "wx/utils.h" - -class MyExecThread : public wxThread -{ -public: - MyExecThread(const wxString& command) : wxThread(wxTHREAD_JOINABLE), - m_command(command) - { - Create(); - } - - virtual ExitCode Entry() - { - return (ExitCode)wxExecute(m_command, wxEXEC_SYNC); - } - -private: - wxString m_command; -}; - -static void TestThreadExec() -{ - wxPuts(_T("*** Testing wxExecute interaction with threads ***\n")); - - MyExecThread thread(_T("true")); - thread.Run(); - - wxPrintf(_T("Main program exit code: %ld.\n"), - wxExecute(_T("false"), wxEXEC_SYNC)); - - wxPrintf(_T("Thread exit code: %ld.\n"), (long)thread.Wait()); -} - // semaphore tests #include "wx/datetime.h" @@ -4375,6 +4399,11 @@ int main(int argc, char **argv) #ifdef TEST_FTP wxLog::AddTraceMask(FTP_TRACE_MASK); + + // wxFTP cannot be a static variable as its ctor needs to access + // wxWidgets internals after it has been initialized + ftp = new wxFTP; + if ( TestFtpConnect() ) { #if TEST_ALL @@ -4386,21 +4415,21 @@ int main(int argc, char **argv) #endif // TEST_ALL #if TEST_INTERACTIVE - TestFtpInteractive(); + //TestFtpInteractive(); #endif } //else: connecting to the FTP server failed - #if 0 - TestFtpWuFtpd(); - #endif + delete ftp; #endif // TEST_FTP #ifdef TEST_MIME //wxLog::AddTraceMask(_T("mime")); TestMimeEnum(); +#if 0 TestMimeOverride(); - // TestMimeAssociate(); + TestMimeAssociate(); +#endif TestMimeFilename(); #endif // TEST_MIME @@ -4468,7 +4497,6 @@ int main(int argc, char **argv) TestThreadSuspend(); TestThreadDelete(); TestThreadConditions(); - TestThreadExec(); TestSemaphore(); #endif #endif // TEST_THREADS