X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bbfa03228a5fc1f23565cf59ee29629bc4336d65..fa699cbaaf217af186cd04dd10d6ec67c8667136:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 804577456c..fa356714ad 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -1,108 +1,117 @@ ///////////////////////////////////////////////////////////////////////////// // Name: samples/console/console.cpp -// Purpose: a sample console (as opposed to GUI) progam using wxWindows +// Purpose: A sample console (as opposed to GUI) program using wxWidgets // Author: Vadim Zeitlin // Modified by: // Created: 04.10.99 // RCS-ID: $Id$ // Copyright: (c) 1999 Vadim Zeitlin -// Licence: wxWindows license +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#include +// ============================================================================ +// declarations +// ============================================================================ -#include -#include -#include -#include +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- -static size_t gs_counter = (size_t)-1; -static wxCriticalSection gs_critsect; +// For compilers that support precompilation, includes "wx/wx.h". +#include "wx/wxprec.h" -class MyThread : public wxThread -{ -public: - MyThread(char ch); +#ifdef __BORLANDC__ + #pragma hdrstop +#endif - // thread execution starts here - virtual void *Entry(); +// for all others, include the necessary headers (this file is usually all you +// need because it includes almost all "standard" wxWidgets headers) +#ifndef WX_PRECOMP + #include "wx/wx.h" +#endif - // and stops here - virtual void OnExit(); +#include +#include -public: - char m_ch; -}; +// ============================================================================ +// implementation +// ============================================================================ -MyThread::MyThread(char ch) +static const wxCmdLineEntryDesc cmdLineDesc[] = { - m_ch = ch; + { wxCMD_LINE_SWITCH, "h", "help", "show this help message", + wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, + { wxCMD_LINE_SWITCH, "d", "dummy", "a dummy switch" }, + // ... your other command line options here... - Create(); -} + { wxCMD_LINE_NONE } +}; -void *MyThread::Entry() +int main(int argc, char **argv) { - { - wxCriticalSectionLocker lock(gs_critsect); - if ( gs_counter == (size_t)-1 ) - gs_counter = 1; - else - gs_counter++; - } + wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "program"); - for ( size_t n = 0; n < 10; n++ ) + wxInitializer initializer; + if ( !initializer ) { - if ( TestDestroy() ) - break; - - putchar(m_ch); - fflush(stdout); - - wxThread::Sleep(100); + fprintf(stderr, "Failed to initialize the wxWidgets library, aborting."); + return -1; } - return NULL; -} - -void MyThread::OnExit() -{ - wxCriticalSectionLocker lock(gs_critsect); - gs_counter--; -} - -int main(int argc, char **argv) -{ - if ( !wxInitialize() ) + wxCmdLineParser parser(cmdLineDesc, argc, argv); + switch ( parser.Parse() ) { - fprintf(stderr, "Failed to initialize the wxWindows library, aborting."); - } + case -1: + // help was given, terminating + break; - static const size_t nThreads = 3; - MyThread *threads[nThreads]; - size_t n; - for ( n = 0; n < nThreads; n++ ) - { - threads[n] = new MyThread('+' + n); - threads[n]->Run(); - } + case 0: + // everything is ok; proceed + if (parser.Found("d")) + { + wxPrintf("Dummy switch was given...\n"); + + while (1) + { + wxChar input[128]; + wxPrintf("Try to guess the magic number (type 'quit' to escape): "); + if ( !wxFgets(input, WXSIZEOF(input), stdin) ) + break; + + // kill the last '\n' + input[wxStrlen(input) - 1] = 0; + + if (wxStrcmp(input, "quit") == 0) + break; + + long val; + if (!wxString(input).ToLong(&val)) + { + wxPrintf("Invalid number...\n"); + continue; + } + + if (val == 42) + wxPrintf("You guessed!\n"); + else + wxPrintf("Bad luck!\n"); + } + } + break; - // wait until all threads terminate - for ( ;; ) - { - wxCriticalSectionLocker lock(gs_critsect); - if ( !gs_counter ) + default: break; } - puts("\nThat's all, folks!"); - - for ( n = 0; n < nThreads; n++ ) + if ( argc == 1 ) { - threads[n]->Delete(); + // If there were no command-line options supplied, emit a message + // otherwise it's not obvious that the sample ran successfully + wxPrintf("Welcome to the wxWidgets 'console' sample!\n"); + wxPrintf("For more information, run it again with the --help option\n"); } - wxUninitialize(); + // do something useful here return 0; }