X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/5ba95b79361a7a2e214a9db048788bbc7590fd87..e39d30c05b9febb396cd9f133b9df7718142e299:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 35037e5b97..4eed13cd22 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // Name: samples/console/console.cpp -// Purpose: a sample console (as opposed to GUI) progam using wxWidgets +// Purpose: A sample console (as opposed to GUI) program using wxWidgets // Author: Vadim Zeitlin // Modified by: // Created: 04.10.99 @@ -25,6 +25,9 @@ #include "wx/file.h" #include "wx/app.h" #include "wx/log.h" +#include "wx/apptrait.h" +#include "wx/platinfo.h" +#include "wx/wxchar.h" // without this pragma, the stupid compiler precompiles #defines below so that // changing them doesn't "take place" later! @@ -53,7 +56,7 @@ #define TEST_CMDLINE #define TEST_DATETIME #define TEST_DIR - #define TEST_DLLLOADER + #define TEST_DYNLIB #define TEST_ENVIRON #define TEST_EXECUTE #define TEST_FILE @@ -65,8 +68,8 @@ #define TEST_LOCALE #define TEST_LOG #define TEST_MIME + #define TEST_MODULE #define TEST_PATHLIST - #define TEST_ODBC #define TEST_PRINTF #define TEST_REGCONF #define TEST_REGEX @@ -85,7 +88,7 @@ #define TEST_WCHAR #define TEST_ZIP #else // #if TEST_ALL - #define TEST_STACKWALKER + #define TEST_CMDLINE #endif // some tests are interactive, define this to run them @@ -133,7 +136,7 @@ static wxString MakePrintable(const wxChar *s) static void ShowCmdLine(const wxCmdLineParser& parser) { - wxString s = _T("Input files: "); + wxString s = _T("Command line parsed successfully:\nInput files: "); size_t count = parser.GetParamCount(); for ( size_t param = 0; param < count; param++ ) @@ -201,7 +204,7 @@ static void TestCmdLineConvert() #ifdef __UNIX__ static const wxChar *ROOTDIR = _T("/"); static const wxChar *TESTDIR = _T("/usr/local/share"); -#elif defined(__WXMSW__) +#elif defined(__WXMSW__) || defined(__DOS__) || defined(__OS2__) static const wxChar *ROOTDIR = _T("c:\\"); static const wxChar *TESTDIR = _T("d:\\"); #else @@ -228,6 +231,8 @@ static void TestDirEnumHelper(wxDir& dir, wxPuts(wxEmptyString); } +#if TEST_ALL + static void TestDirEnum() { wxPuts(_T("*** Testing wxDir::GetFirst/GetNext ***")); @@ -283,6 +288,8 @@ static void TestDirEnum() TestDirEnumHelper(dirNo); } +#endif // TEST_ALL + class DirPrintTraverser : public wxDirTraverser { public: @@ -333,6 +340,8 @@ static void TestDirTraverse() dir.Traverse(traverser, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN); } +#if TEST_ALL + static void TestDirExists() { wxPuts(_T("*** Testing wxDir::Exists() ***")); @@ -366,13 +375,15 @@ static void TestDirExists() } } +#endif // TEST_ALL + #endif // TEST_DIR // ---------------------------------------------------------------------------- // wxDllLoader // ---------------------------------------------------------------------------- -#ifdef TEST_DLLLOADER +#ifdef TEST_DYNLIB #include "wx/dynlib.h" @@ -398,7 +409,7 @@ static void TestDllLoad() } else { - typedef int (*wxStrlenType)(const char *); + typedef int (wxSTDCALL *wxStrlenType)(const char *); wxStrlenType pfnStrlen = (wxStrlenType)lib.GetSymbol(FUNC_NAME); if ( !pfnStrlen ) { @@ -419,6 +430,26 @@ static void TestDllLoad() wxPuts(_T("... ok")); } } + +#ifdef __WXMSW__ + static const wxChar *FUNC_NAME_AW = _T("lstrlen"); + + typedef int (wxSTDCALL *wxStrlenTypeAorW)(const wxChar *); + wxStrlenTypeAorW + pfnStrlenAorW = (wxStrlenTypeAorW)lib.GetSymbolAorW(FUNC_NAME_AW); + if ( !pfnStrlenAorW ) + { + wxPrintf(_T("ERROR: function '%s' wasn't found in '%s'.\n"), + FUNC_NAME_AW, LIB_NAME); + } + else + { + if ( pfnStrlenAorW(_T("foobar")) != 6 ) + { + wxPrintf(_T("ERROR: loaded function is not wxStrlen()!\n")); + } + } +#endif // __WXMSW__ } } @@ -450,7 +481,7 @@ static void TestDllListLoaded() #endif -#endif // TEST_DLLLOADER +#endif // TEST_DYNLIB // ---------------------------------------------------------------------------- // wxGet/SetEnv @@ -689,6 +720,24 @@ static void TestFileCopy() wxPuts(wxEmptyString); } +static void TestTempFile() +{ + wxPuts(_T("*** wxTempFile test ***")); + + wxTempFile tmpFile; + if ( tmpFile.Open(_T("test2")) && tmpFile.Write(_T("the answer is 42")) ) + { + if ( tmpFile.Commit() ) + wxPuts(_T("File committed.")); + else + wxPuts(_T("ERROR: could't commit temp file.")); + + wxRemoveFile(_T("test2")); + } + + wxPuts(wxEmptyString); +} + #endif // TEST_FILE // ---------------------------------------------------------------------------- @@ -869,8 +918,8 @@ static void TestFileNameCwd() #ifdef TEST_FILETIME -#include -#include +#include "wx/filename.h" +#include "wx/datetime.h" static void TestFileGetTimes() { @@ -1333,7 +1382,7 @@ static void TestMimeAssociate() _T(""), // print cmd _T("XYZ File"), // description _T(".xyz"), // extensions - NULL // end of extensions + wxNullPtr // end of extensions ); ftInfo.SetShortDesc(_T("XYZFile")); // used under Win32 only @@ -1353,6 +1402,80 @@ static void TestMimeAssociate() #endif // TEST_MIME +// ---------------------------------------------------------------------------- +// module dependencies feature +// ---------------------------------------------------------------------------- + +#ifdef TEST_MODULE + +#include "wx/module.h" + +class wxTestModule : public wxModule +{ +protected: + virtual bool OnInit() { wxPrintf(_T("Load module: %s\n"), GetClassInfo()->GetClassName()); return true; } + virtual void OnExit() { wxPrintf(_T("Unload module: %s\n"), GetClassInfo()->GetClassName()); } +}; + +class wxTestModuleA : public wxTestModule +{ +public: + wxTestModuleA(); +private: + DECLARE_DYNAMIC_CLASS(wxTestModuleA) +}; + +class wxTestModuleB : public wxTestModule +{ +public: + wxTestModuleB(); +private: + DECLARE_DYNAMIC_CLASS(wxTestModuleB) +}; + +class wxTestModuleC : public wxTestModule +{ +public: + wxTestModuleC(); +private: + DECLARE_DYNAMIC_CLASS(wxTestModuleC) +}; + +class wxTestModuleD : public wxTestModule +{ +public: + wxTestModuleD(); +private: + DECLARE_DYNAMIC_CLASS(wxTestModuleD) +}; + +IMPLEMENT_DYNAMIC_CLASS(wxTestModuleC, wxModule) +wxTestModuleC::wxTestModuleC() +{ + AddDependency(CLASSINFO(wxTestModuleD)); +} + +IMPLEMENT_DYNAMIC_CLASS(wxTestModuleA, wxModule) +wxTestModuleA::wxTestModuleA() +{ + AddDependency(CLASSINFO(wxTestModuleB)); + AddDependency(CLASSINFO(wxTestModuleD)); +} + +IMPLEMENT_DYNAMIC_CLASS(wxTestModuleD, wxModule) +wxTestModuleD::wxTestModuleD() +{ +} + +IMPLEMENT_DYNAMIC_CLASS(wxTestModuleB, wxModule) +wxTestModuleB::wxTestModuleB() +{ + AddDependency(CLASSINFO(wxTestModuleD)); + AddDependency(CLASSINFO(wxTestModuleC)); +} + +#endif // TEST_MODULE + // ---------------------------------------------------------------------------- // misc information functions // ---------------------------------------------------------------------------- @@ -1361,6 +1484,7 @@ static void TestMimeAssociate() #include "wx/utils.h" +#if TEST_INTERACTIVE static void TestDiskInfo() { wxPuts(_T("*** Testing wxGetDiskSpace() ***")); @@ -1389,6 +1513,7 @@ static void TestDiskInfo() } } } +#endif // TEST_INTERACTIVE static void TestOsInfo() { @@ -1399,7 +1524,7 @@ static void TestOsInfo() wxPrintf(_T("Running under: %s, version %d.%d\n"), wxGetOsDescription().c_str(), major, minor); - wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory()); + wxPrintf(_T("%ld free bytes of memory left.\n"), wxGetFreeMemory().ToLong()); wxPrintf(_T("Host name is %s (%s).\n"), wxGetHostName().c_str(), wxGetFullHostName().c_str()); @@ -1407,6 +1532,23 @@ static void TestOsInfo() wxPuts(wxEmptyString); } +static void TestPlatformInfo() +{ + wxPuts(_T("*** Testing wxPlatformInfo functions ***\n")); + + // get this platform + wxPlatformInfo plat; + + wxPrintf(_T("Operating system family name is: %s\n"), plat.GetOperatingSystemFamilyName().c_str()); + wxPrintf(_T("Operating system name is: %s\n"), plat.GetOperatingSystemIdName().c_str()); + wxPrintf(_T("Port ID name is: %s\n"), plat.GetPortIdName().c_str()); + wxPrintf(_T("Port ID short name is: %s\n"), plat.GetPortIdShortName().c_str()); + wxPrintf(_T("Architecture is: %s\n"), plat.GetArchName().c_str()); + wxPrintf(_T("Endianness is: %s\n"), plat.GetEndiannessName().c_str()); + + wxPuts(wxEmptyString); +} + static void TestUserInfo() { wxPuts(_T("*** Testing user info functions ***\n")); @@ -1516,26 +1658,6 @@ static void TestRegExInteractive() #endif // TEST_REGEX -// ---------------------------------------------------------------------------- -// database -// ---------------------------------------------------------------------------- - -#if !wxUSE_ODBC - #undef TEST_ODBC -#endif - -#ifdef TEST_ODBC - -#include - -static void TestDbOpen() -{ - HENV henv; - wxDb db(henv); -} - -#endif // TEST_ODBC - // ---------------------------------------------------------------------------- // printf() tests // ---------------------------------------------------------------------------- @@ -2085,9 +2207,9 @@ static void TestScopeGuard() wxON_BLOCK_EXIT2(function2, 3.14, 'p'); Object obj; - wxON_BLOCK_EXIT_OBJ0(obj, &Object::method0); - wxON_BLOCK_EXIT_OBJ1(obj, &Object::method1, 7); - wxON_BLOCK_EXIT_OBJ2(obj, &Object::method2, 2.71, 'e'); + wxON_BLOCK_EXIT_OBJ0(obj, Object::method0); + wxON_BLOCK_EXIT_OBJ1(obj, Object::method1, 7); + wxON_BLOCK_EXIT_OBJ2(obj, Object::method2, 2.71, 'e'); wxScopeGuard dismissed = wxMakeGuard(function0); dismissed.Dismiss(); @@ -2642,6 +2764,7 @@ static void TestStackWalk(const char *argv0) #ifdef TEST_STDPATHS #include "wx/stdpaths.h" +#include "wx/wxchar.h" // wxPrintf static void TestStandardPaths() { @@ -2656,7 +2779,18 @@ static void TestStandardPaths() wxPrintf(_T("Data dir (sys local):\t%s\n"), stdp.GetLocalDataDir().c_str()); wxPrintf(_T("Data dir (user):\t%s\n"), stdp.GetUserDataDir().c_str()); wxPrintf(_T("Data dir (user local):\t%s\n"), stdp.GetUserLocalDataDir().c_str()); + wxPrintf(_T("Documents dir:\t\t%s\n"), stdp.GetDocumentsDir().c_str()); + wxPrintf(_T("Executable path:\t%s\n"), stdp.GetExecutablePath().c_str()); wxPrintf(_T("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str()); + wxPrintf(_T("Resources dir:\t\t%s\n"), stdp.GetResourcesDir().c_str()); + wxPrintf(_T("Localized res. dir:\t%s\n"), + stdp.GetLocalizedResourcesDir(_T("fr")).c_str()); + wxPrintf(_T("Message catalogs dir:\t%s\n"), + stdp.GetLocalizedResourcesDir + ( + _T("fr"), + wxStandardPaths::ResourceCat_Messages + ).c_str()); } #endif // TEST_STDPATHS @@ -2680,11 +2814,13 @@ static void TestFileStream() fsOut.Write("foo", 3); } - wxFileInputStream fsIn(filename); - wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize()); - while ( !fsIn.Eof() ) { - wxPutchar(fsIn.GetC()); + wxFileInputStream fsIn(filename); + wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize()); + while ( !fsIn.Eof() ) + { + wxPutchar(fsIn.GetC()); + } } if ( !wxRemoveFile(filename) ) @@ -2787,6 +2923,52 @@ static void TestStopWatch() wxPuts(_T(", ok.")); } +#include "wx/timer.h" +#include "wx/evtloop.h" + +void TestTimer() +{ + wxPuts(_T("*** Testing wxTimer ***\n")); + + class MyTimer : public wxTimer + { + public: + MyTimer() : wxTimer() { m_num = 0; } + + virtual void Notify() + { + wxPrintf(_T("%d"), m_num++); + fflush(stdout); + + if ( m_num == 10 ) + { + wxPrintf(_T("... exiting the event loop")); + Stop(); + + wxEventLoop::GetActive()->Exit(0); + wxPuts(_T(", ok.")); + } + + fflush(stdout); + } + + private: + int m_num; + }; + + wxEventLoop loop; + + wxTimer timer1; + timer1.Start(100, true /* one shot */); + timer1.Stop(); + timer1.Start(100, true /* one shot */); + + MyTimer timer; + timer.Start(500); + + loop.Run(); +} + #endif // TEST_TIMER // ---------------------------------------------------------------------------- @@ -2995,7 +3177,7 @@ static void TestVCardRead() wxPrintf(_T("Full name from wxVCard API: %s\n"), value.c_str()); - // now show how to deal with multiply occuring properties + // now show how to deal with multiply occurring properties DumpVCardAddresses(vcard); DumpVCardPhoneNumbers(vcard); @@ -4069,19 +4251,19 @@ int main(int argc, char **argv) #if wxUSE_CMDLINE_PARSER static const wxCmdLineEntryDesc cmdLineDesc[] = { - { wxCMD_LINE_SWITCH, _T("h"), _T("help"), _T("show this help message"), + { wxCMD_LINE_SWITCH, "h", "help", "show this help message", wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, - { wxCMD_LINE_SWITCH, _T("v"), _T("verbose"), _T("be verbose") }, - { wxCMD_LINE_SWITCH, _T("q"), _T("quiet"), _T("be quiet") }, + { wxCMD_LINE_SWITCH, "v", "verbose", "be verbose" }, + { wxCMD_LINE_SWITCH, "q", "quiet", "be quiet" }, - { wxCMD_LINE_OPTION, _T("o"), _T("output"), _T("output file") }, - { wxCMD_LINE_OPTION, _T("i"), _T("input"), _T("input dir") }, - { wxCMD_LINE_OPTION, _T("s"), _T("size"), _T("output block size"), + { wxCMD_LINE_OPTION, "o", "output", "output file" }, + { wxCMD_LINE_OPTION, "i", "input", "input dir" }, + { wxCMD_LINE_OPTION, "s", "size", "output block size", wxCMD_LINE_VAL_NUMBER }, - { wxCMD_LINE_OPTION, _T("d"), _T("date"), _T("output file date"), + { wxCMD_LINE_OPTION, "d", "date", "output file date", wxCMD_LINE_VAL_DATE }, - { wxCMD_LINE_PARAM, NULL, NULL, _T("input file"), + { wxCMD_LINE_PARAM, NULL, NULL, "input file", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_MULTIPLE }, { wxCMD_LINE_NONE } @@ -4119,10 +4301,10 @@ int main(int argc, char **argv) TestDirTraverse(); #endif // TEST_DIR -#ifdef TEST_DLLLOADER +#ifdef TEST_DYNLIB TestDllLoad(); TestDllListLoaded(); -#endif // TEST_DLLLOADER +#endif // TEST_DYNLIB #ifdef TEST_ENVIRON TestEnvironment(); @@ -4168,6 +4350,7 @@ int main(int argc, char **argv) TestFileRead(); TestTextFileRead(); TestFileCopy(); + TestTempFile(); #endif // TEST_FILE #ifdef TEST_FILENAME @@ -4209,23 +4392,20 @@ int main(int argc, char **argv) #endif // TEST_FTP #ifdef TEST_MIME - wxLog::AddTraceMask(_T("mime")); - #if TEST_ALL - TestMimeEnum(); - TestMimeOverride(); - TestMimeAssociate(); - #endif + //wxLog::AddTraceMask(_T("mime")); + TestMimeEnum(); + TestMimeOverride(); + // TestMimeAssociate(); TestMimeFilename(); #endif // TEST_MIME #ifdef TEST_INFO_FUNCTIONS - #if TEST_ALL - TestOsInfo(); - TestUserInfo(); + TestOsInfo(); + TestPlatformInfo(); + TestUserInfo(); - #if TEST_INTERACTIVE - TestDiskInfo(); - #endif + #if TEST_INTERACTIVE + TestDiskInfo(); #endif #endif // TEST_INFO_FUNCTIONS @@ -4233,10 +4413,6 @@ int main(int argc, char **argv) TestPathList(); #endif // TEST_PATHLIST -#ifdef TEST_ODBC - TestDbOpen(); -#endif // TEST_ODBC - #ifdef TEST_PRINTF TestPrintf(); #endif // TEST_PRINTF @@ -4294,6 +4470,7 @@ int main(int argc, char **argv) #ifdef TEST_TIMER TestStopWatch(); + TestTimer(); #endif // TEST_TIMER #ifdef TEST_DATETIME @@ -4365,4 +4542,3 @@ int main(int argc, char **argv) wxUnusedVar(argv); return 0; } -