X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/349c045ab360647b141f7c5d013d7351c0e0fa6f..30e60550013cd409a06c3a0287d10feba487187e:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 0cf71b6110..b996573935 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 // ============================================================================ @@ -50,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 @@ -59,12 +110,11 @@ #define TEST_DIR #define TEST_DYNLIB #define TEST_ENVIRON - #define TEST_EXECUTE #define TEST_FILE #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 @@ -84,7 +134,6 @@ #define TEST_TEXTSTREAM #define TEST_THREADS #define TEST_TIMER - // #define TEST_VCARD -- don't enable this (VZ) // #define TEST_VOLUME --FIXME! (RN) #define TEST_WCHAR #define TEST_ZIP @@ -524,93 +573,6 @@ static void TestEnvironment() #endif // TEST_ENVIRON -// ---------------------------------------------------------------------------- -// wxExecute -// ---------------------------------------------------------------------------- - -#ifdef TEST_EXECUTE - -#include "wx/utils.h" - -static void TestExecute() -{ - wxPuts(_T("*** testing wxExecute ***")); - -#ifdef __UNIX__ - #define COMMAND "echo hi" - #define ASYNC_COMMAND "xclock" - #define SHELL_COMMAND "echo hi from shell" - #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 - #error "no command to exec" -#endif // OS - - wxPrintf(_T("Testing wxShell: ")); - fflush(stdout); - if ( wxShell(_T(SHELL_COMMAND)) ) - wxPuts(_T("Ok.")); - else - wxPuts(_T("ERROR.")); - - wxPrintf(_T("Testing wxExecute: ")); - fflush(stdout); - if ( wxExecute(_T(COMMAND), wxEXEC_SYNC) == 0 ) - wxPuts(_T("Ok.")); - else - wxPuts(_T("ERROR.")); - - wxPrintf(_T("Testing async wxExecute: ")); - fflush(stdout); - 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.")); - - wxPrintf(_T("Testing wxExecute with redirection:\n")); - wxArrayString output; - if ( wxExecute(_T(REDIRECT_COMMAND), output) != 0 ) - { - wxPuts(_T("ERROR.")); - } - else - { - // 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("%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.")); - } -} - -#endif // TEST_EXECUTE - // ---------------------------------------------------------------------------- // file // ---------------------------------------------------------------------------- @@ -1632,7 +1594,7 @@ static void TestPathList() // regular expressions // ---------------------------------------------------------------------------- -#ifdef TEST_REGEX +#if defined TEST_REGEX && TEST_INTERACTIVE #include "wx/regex.h" @@ -1709,7 +1671,7 @@ static void TestRegExInteractive() #undef wxPrintf #undef wxSprintf - // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats + // NB: do _not_ use WX_ATTRIBUTE_PRINTF here, we have some invalid formats // in the tests below int wxPrintf( const wxChar *format, ... ); int wxSprintf( wxChar *str, const wxChar *format, ... ); @@ -2404,6 +2366,7 @@ static void TestSocketClient() #ifdef TEST_FTP #include "wx/protocol/ftp.h" +#include "wx/protocol/log.h" #define FTP_ANONYMOUS @@ -2421,10 +2384,6 @@ static bool TestFtpConnect() { wxPuts(_T("*** Testing FTP connect ***")); - // wxFTP cannot be a static variable as its ctor needs to access - // wxWidgets internals after it has been initialized - ftp = new wxFTP; - #ifdef FTP_ANONYMOUS static const wxChar *hostname = _T("ftp.wxwidgets.org"); @@ -2462,44 +2421,6 @@ static bool TestFtpConnect() 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")); @@ -2632,6 +2553,8 @@ static void TestFtpMisc() } } +#if TEST_INTERACTIVE + static void TestFtpInteractive() { wxPuts(_T("\n*** Interactive wxFTP test ***")); @@ -2689,6 +2612,8 @@ static void TestFtpInteractive() wxPuts(_T("\n*** done ***")); } +#endif // TEST_INTERACTIVE + static void TestFtpUpload() { wxPuts(_T("*** Testing wxFTP uploading ***\n")); @@ -2744,11 +2669,11 @@ 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: @@ -2857,9 +2782,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); } } @@ -2894,9 +2820,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 ***")); @@ -3011,248 +2938,6 @@ void TestTimer() #endif // TEST_TIMER -// ---------------------------------------------------------------------------- -// vCard support -// ---------------------------------------------------------------------------- - -#ifdef TEST_VCARD - -#include "wx/vcard.h" - -static void DumpVObject(size_t level, const wxVCardObject& vcard) -{ - void *cookie; - wxVCardObject *vcObj = vcard.GetFirstProp(&cookie); - while ( vcObj ) - { - wxPrintf(_T("%s%s"), - wxString(_T('\t'), level).c_str(), - vcObj->GetName().c_str()); - - wxString value; - switch ( vcObj->GetType() ) - { - case wxVCardObject::String: - case wxVCardObject::UString: - { - wxString val; - vcObj->GetValue(&val); - value << _T('"') << val << _T('"'); - } - break; - - case wxVCardObject::Int: - { - unsigned int i; - vcObj->GetValue(&i); - value.Printf(_T("%u"), i); - } - break; - - case wxVCardObject::Long: - { - unsigned long l; - vcObj->GetValue(&l); - value.Printf(_T("%lu"), l); - } - break; - - case wxVCardObject::None: - break; - - case wxVCardObject::Object: - value = _T(""); - break; - - default: - value = _T(""); - } - - if ( !!value ) - wxPrintf(_T(" = %s"), value.c_str()); - wxPutchar('\n'); - - DumpVObject(level + 1, *vcObj); - - delete vcObj; - vcObj = vcard.GetNextProp(&cookie); - } -} - -static void DumpVCardAddresses(const wxVCard& vcard) -{ - wxPuts(_T("\nShowing all addresses from vCard:\n")); - - size_t nAdr = 0; - void *cookie; - wxVCardAddress *addr = vcard.GetFirstAddress(&cookie); - while ( addr ) - { - wxString flagsStr; - int flags = addr->GetFlags(); - if ( flags & wxVCardAddress::Domestic ) - { - flagsStr << _T("domestic "); - } - if ( flags & wxVCardAddress::Intl ) - { - flagsStr << _T("international "); - } - if ( flags & wxVCardAddress::Postal ) - { - flagsStr << _T("postal "); - } - if ( flags & wxVCardAddress::Parcel ) - { - flagsStr << _T("parcel "); - } - if ( flags & wxVCardAddress::Home ) - { - flagsStr << _T("home "); - } - if ( flags & wxVCardAddress::Work ) - { - flagsStr << _T("work "); - } - - wxPrintf(_T("Address %u:\n") - "\tflags = %s\n" - "\tvalue = %s;%s;%s;%s;%s;%s;%s\n", - ++nAdr, - flagsStr.c_str(), - addr->GetPostOffice().c_str(), - addr->GetExtAddress().c_str(), - addr->GetStreet().c_str(), - addr->GetLocality().c_str(), - addr->GetRegion().c_str(), - addr->GetPostalCode().c_str(), - addr->GetCountry().c_str() - ); - - delete addr; - addr = vcard.GetNextAddress(&cookie); - } -} - -static void DumpVCardPhoneNumbers(const wxVCard& vcard) -{ - wxPuts(_T("\nShowing all phone numbers from vCard:\n")); - - size_t nPhone = 0; - void *cookie; - wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie); - while ( phone ) - { - wxString flagsStr; - int flags = phone->GetFlags(); - if ( flags & wxVCardPhoneNumber::Voice ) - { - flagsStr << _T("voice "); - } - if ( flags & wxVCardPhoneNumber::Fax ) - { - flagsStr << _T("fax "); - } - if ( flags & wxVCardPhoneNumber::Cellular ) - { - flagsStr << _T("cellular "); - } - if ( flags & wxVCardPhoneNumber::Modem ) - { - flagsStr << _T("modem "); - } - if ( flags & wxVCardPhoneNumber::Home ) - { - flagsStr << _T("home "); - } - if ( flags & wxVCardPhoneNumber::Work ) - { - flagsStr << _T("work "); - } - - wxPrintf(_T("Phone number %u:\n") - "\tflags = %s\n" - "\tvalue = %s\n", - ++nPhone, - flagsStr.c_str(), - phone->GetNumber().c_str() - ); - - delete phone; - phone = vcard.GetNextPhoneNumber(&cookie); - } -} - -static void TestVCardRead() -{ - wxPuts(_T("*** Testing wxVCard reading ***\n")); - - wxVCard vcard(_T("vcard.vcf")); - if ( !vcard.IsOk() ) - { - wxPuts(_T("ERROR: couldn't load vCard.")); - } - else - { - // read individual vCard properties - wxVCardObject *vcObj = vcard.GetProperty("FN"); - wxString value; - if ( vcObj ) - { - vcObj->GetValue(&value); - delete vcObj; - } - else - { - value = _T(""); - } - - wxPrintf(_T("Full name retrieved directly: %s\n"), value.c_str()); - - - if ( !vcard.GetFullName(&value) ) - { - value = _T(""); - } - - wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str()); - - // now show how to deal with multiply occurring properties - DumpVCardAddresses(vcard); - DumpVCardPhoneNumbers(vcard); - - // and finally show all - wxPuts(_T("\nNow dumping the entire vCard:\n") - "-----------------------------\n"); - - DumpVObject(0, vcard); - } -} - -static void TestVCardWrite() -{ - wxPuts(_T("*** Testing wxVCard writing ***\n")); - - wxVCard vcard; - if ( !vcard.IsOk() ) - { - wxPuts(_T("ERROR: couldn't create vCard.")); - } - else - { - // set some fields - vcard.SetName("Zeitlin", "Vadim"); - vcard.SetFullName("Vadim Zeitlin"); - vcard.SetOrganization("wxWidgets", "R&D"); - - // just dump the vCard back - wxPuts(_T("Entire vCard follows:\n")); - wxPuts(vcard.Write()); - } -} - -#endif // TEST_VCARD - // ---------------------------------------------------------------------------- // wxVolume tests // ---------------------------------------------------------------------------- @@ -3453,9 +3138,10 @@ static void TestZipStreamRead() 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); } @@ -4132,39 +3818,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" @@ -4356,10 +4009,6 @@ int main(int argc, char **argv) TestEnvironment(); #endif // TEST_ENVIRON -#ifdef TEST_EXECUTE - TestExecute(); -#endif // TEST_EXECUTE - #ifdef TEST_FILECONF TestFileConfRead(); #endif // TEST_FILECONF @@ -4416,6 +4065,12 @@ 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; + ftp->SetLog(new wxProtocolLog(FTP_TRACE_MASK)); + if ( TestFtpConnect() ) { #if TEST_ALL @@ -4427,14 +4082,12 @@ 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 @@ -4511,7 +4164,6 @@ int main(int argc, char **argv) TestThreadSuspend(); TestThreadDelete(); TestThreadConditions(); - TestThreadExec(); TestSemaphore(); #endif #endif // TEST_THREADS @@ -4558,11 +4210,6 @@ int main(int argc, char **argv) wxUsleep(3000); #endif // TEST_USLEEP -#ifdef TEST_VCARD - TestVCardRead(); - TestVCardWrite(); -#endif // TEST_VCARD - #ifdef TEST_VOLUME TestFSVolume(); #endif // TEST_VOLUME