X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/17af82fb4c8f830f202567259472645c27724c32..33ccf3d1ecb0c52bf4a1323587af92d67a672eb7:/samples/console/console.cpp diff --git a/samples/console/console.cpp b/samples/console/console.cpp index 14e781d047..ce887b1a59 100644 --- a/samples/console/console.cpp +++ b/samples/console/console.cpp @@ -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! @@ -46,7 +49,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 @@ -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_STDPATHS + #define TEST_EXECUTE #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++ ) @@ -147,6 +150,7 @@ static void ShowCmdLine(const wxCmdLineParser& parser) wxString strVal; long lVal; + double dVal; wxDateTime dt; if ( parser.Found(_T("o"), &strVal) ) s << _T("Output file:\t") << strVal << '\n'; @@ -154,6 +158,8 @@ static void ShowCmdLine(const wxCmdLineParser& parser) s << _T("Input dir:\t") << strVal << '\n'; if ( parser.Found(_T("s"), &lVal) ) s << _T("Size:\t") << lVal << '\n'; + if ( parser.Found(_T("f"), &dVal) ) + s << _T("Double:\t") << dVal << '\n'; if ( parser.Found(_T("d"), &dt) ) s << _T("Date:\t") << dt.FormatISODate() << '\n'; if ( parser.Found(_T("project_name"), &strVal) ) @@ -462,7 +468,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; @@ -472,7 +478,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()); } } @@ -530,11 +536,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 @@ -550,19 +558,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; @@ -572,10 +583,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.")); @@ -717,6 +743,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 // ---------------------------------------------------------------------------- @@ -1281,32 +1325,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")); @@ -1351,6 +1369,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")); @@ -1361,7 +1408,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 @@ -1379,8 +1426,84 @@ static void TestMimeAssociate() wxPuts(wxEmptyString); } +#endif // 0 + #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 // ---------------------------------------------------------------------------- @@ -1389,6 +1512,7 @@ static void TestMimeAssociate() #include "wx/utils.h" +#if TEST_INTERACTIVE static void TestDiskInfo() { wxPuts(_T("*** Testing wxGetDiskSpace() ***")); @@ -1417,6 +1541,7 @@ static void TestDiskInfo() } } } +#endif // TEST_INTERACTIVE static void TestOsInfo() { @@ -1427,7 +1552,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()); @@ -1435,6 +1560,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")); @@ -1544,26 +1686,6 @@ static void TestRegExInteractive() #endif // TEST_REGEX -// ---------------------------------------------------------------------------- -// database -// ---------------------------------------------------------------------------- - -#if !wxUSE_ODBC - #undef TEST_ODBC -#endif - -#ifdef TEST_ODBC - -#include "wx/db.h" - -static void TestDbOpen() -{ - HENV henv; - wxDb db(henv); -} - -#endif // TEST_ODBC - // ---------------------------------------------------------------------------- // printf() tests // ---------------------------------------------------------------------------- @@ -1966,7 +2088,7 @@ static void TestRegConfWrite() static void TestRegConfRead() { - wxConfig *config = new wxConfig(_T("myapp")); + wxRegConfig *config = new wxRegConfig(_T("myapp")); wxString str; long dummy; @@ -2622,12 +2744,12 @@ public: 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 { @@ -2637,8 +2759,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(""); @@ -2646,7 +2768,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()); } } }; @@ -2670,6 +2794,7 @@ static void TestStackWalk(const char *argv0) #ifdef TEST_STDPATHS #include "wx/stdpaths.h" +#include "wx/wxchar.h" // wxPrintf static void TestStandardPaths() { @@ -2685,6 +2810,7 @@ static void TestStandardPaths() 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"), @@ -2718,11 +2844,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) ) @@ -2825,6 +2953,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 // ---------------------------------------------------------------------------- @@ -3261,7 +3435,11 @@ 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()); @@ -4107,19 +4285,21 @@ 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_OPTION, "f", "double", "output double", + wxCMD_LINE_VAL_DOUBLE }, - { 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 } @@ -4206,6 +4386,7 @@ int main(int argc, char **argv) TestFileRead(); TestTextFileRead(); TestFileCopy(); + TestTempFile(); #endif // TEST_FILE #ifdef TEST_FILENAME @@ -4247,23 +4428,22 @@ 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(); +#if 0 + TestMimeOverride(); + TestMimeAssociate(); +#endif 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 @@ -4271,10 +4451,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 @@ -4332,6 +4508,7 @@ int main(int argc, char **argv) #ifdef TEST_TIMER TestStopWatch(); + TestTimer(); #endif // TEST_TIMER #ifdef TEST_DATETIME