]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/console/console.cpp
printf() format warrning fix
[wxWidgets.git] / samples / console / console.cpp
index 24c3bf569a620534b872f15ec8d7c08e0197d2c9..8f932dcdd74fa19456eba10ebfc2f23b5cecb6d4 100644 (file)
@@ -28,6 +28,7 @@
 #include "wx/string.h"
 #include "wx/file.h"
 #include "wx/app.h"
+#include "wx/log.h"
 
 // without this pragma, the stupid compiler precompiles #defines below so that
 // changing them doesn't "take place" later!
@@ -81,6 +82,7 @@
     #define TEST_SOCKETS
     #define TEST_STREAMS
     #define TEST_STRINGS
+    #define TEST_TEXTSTREAM
     #define TEST_THREADS
     #define TEST_TIMER
     #define TEST_UNICODE
     #define TEST_ZLIB
 
     #undef TEST_ALL
-    static const bool TEST_ALL = TRUE;
+    static const bool TEST_ALL = true;
 #else
-    #define TEST_DATETIME
+    #define TEST_FILECONF
 
-    static const bool TEST_ALL = FALSE;
+    static const bool TEST_ALL = false;
 #endif
 
 // some tests are interactive, define this to run them
 #ifdef TEST_INTERACTIVE
     #undef TEST_INTERACTIVE
 
-    static const bool TEST_INTERACTIVE = TRUE;
+    static const bool TEST_INTERACTIVE = true;
 #else
-    static const bool TEST_INTERACTIVE = FALSE;
+    static const bool TEST_INTERACTIVE = false;
 #endif
 
 // ----------------------------------------------------------------------------
@@ -205,7 +207,6 @@ static void TestCharset()
 
 #include "wx/cmdline.h"
 #include "wx/datetime.h"
-#include "wx/log.h"
 
 #if wxUSE_CMDLINE_PARSER
 
@@ -278,7 +279,7 @@ static void TestCmdLineConvert()
 
 #ifdef __UNIX__
     static const wxChar *ROOTDIR = _T("/");
-    static const wxChar *TESTDIR = _T("/usr");
+    static const wxChar *TESTDIR = _T("/usr/local/share");
 #elif defined(__WXMSW__)
     static const wxChar *ROOTDIR = _T("c:\\");
     static const wxChar *TESTDIR = _T("d:\\");
@@ -405,6 +406,7 @@ static void TestDirTraverse()
     }
 
     // enum again with custom traverser
+    wxPuts(_T("Now enumerating directories:"));
     wxDir dir(TESTDIR);
     DirPrintTraverser traverser;
     dir.Traverse(traverser, _T(""), wxDIR_DIRS | wxDIR_HIDDEN);
@@ -568,7 +570,7 @@ static void TestExecute()
 
     wxPrintf(_T("Testing wxExecute: "));
     fflush(stdout);
-    if ( wxExecute(COMMAND, TRUE /* sync */) == 0 )
+    if ( wxExecute(COMMAND, true /* sync */) == 0 )
         wxPuts(_T("Ok."));
     else
         wxPuts(_T("ERROR."));
@@ -796,6 +798,12 @@ static void TestFileConfRead()
 
         cont = fileconf.GetNextEntry(name, dummy);
     }
+
+    static const wxChar *testEntry = _T("TestEntry");
+    wxPrintf(_T("\nTesting deletion of newly created \"Test\" entry: "));
+    fileconf.Write(testEntry, _T("A value"));
+    fileconf.DeleteEntry(testEntry);
+    wxPrintf(fileconf.HasEntry(testEntry) ? _T("ERROR\n") : _T("ok\n"));
 }
 
 #endif // TEST_FILECONF
@@ -851,39 +859,39 @@ static struct FileNameInfo
 } filenames[] =
 {
     // Unix file names
-    { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), TRUE, wxPATH_UNIX },
-    { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), TRUE, wxPATH_UNIX },
-    { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), TRUE, wxPATH_UNIX },
-    { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), FALSE, wxPATH_UNIX },
-    { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
-    { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), TRUE, wxPATH_UNIX },
-    { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), TRUE, wxPATH_UNIX },
-    { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), FALSE, wxPATH_UNIX },
-    { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), TRUE, wxPATH_UNIX },
+    { _T("/usr/bin/ls"), _T(""), _T("/usr/bin"), _T("ls"), _T(""), true, wxPATH_UNIX },
+    { _T("/usr/bin/"), _T(""), _T("/usr/bin"), _T(""), _T(""), true, wxPATH_UNIX },
+    { _T("~/.zshrc"), _T(""), _T("~"), _T(".zshrc"), _T(""), true, wxPATH_UNIX },
+    { _T("../../foo"), _T(""), _T("../.."), _T("foo"), _T(""), false, wxPATH_UNIX },
+    { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_UNIX },
+    { _T("~/foo.bar"), _T(""), _T("~"), _T("foo"), _T("bar"), true, wxPATH_UNIX },
+    { _T("/foo"), _T(""), _T("/"), _T("foo"), _T(""), true, wxPATH_UNIX },
+    { _T("Mahogany-0.60/foo.bar"), _T(""), _T("Mahogany-0.60"), _T("foo"), _T("bar"), false, wxPATH_UNIX },
+    { _T("/tmp/wxwin.tar.bz"), _T(""), _T("/tmp"), _T("wxwin.tar"), _T("bz"), true, wxPATH_UNIX },
 
     // Windows file names
-    { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
-    { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
-    { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), FALSE, wxPATH_DOS },
-    { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
-    { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), TRUE, wxPATH_DOS },
-    { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
-    { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), TRUE, wxPATH_DOS },
+    { _T("foo.bar"), _T(""), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
+    { _T("\\foo.bar"), _T(""), _T("\\"), _T("foo"), _T("bar"), false, wxPATH_DOS },
+    { _T("c:foo.bar"), _T("c"), _T(""), _T("foo"), _T("bar"), false, wxPATH_DOS },
+    { _T("c:\\foo.bar"), _T("c"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
+    { _T("c:\\Windows\\command.com"), _T("c"), _T("\\Windows"), _T("command"), _T("com"), true, wxPATH_DOS },
+    { _T("\\\\server\\foo.bar"), _T("server"), _T("\\"), _T("foo"), _T("bar"), true, wxPATH_DOS },
+    { _T("\\\\server\\dir\\foo.bar"), _T("server"), _T("\\dir"), _T("foo"), _T("bar"), true, wxPATH_DOS },
 
     // wxFileName support for Mac file names is broken currently
 #if 0
     // Mac file names
-    { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
-    { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), TRUE, wxPATH_MAC },
-    { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), TRUE, wxPATH_MAC },
-    { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), FALSE, wxPATH_MAC },
-    { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC },
-    { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), FALSE, wxPATH_MAC },
+    { _T("Volume:Dir:File"), _T("Volume"), _T("Dir"), _T("File"), _T(""), true, wxPATH_MAC },
+    { _T("Volume:Dir:Subdir:File"), _T("Volume"), _T("Dir:Subdir"), _T("File"), _T(""), true, wxPATH_MAC },
+    { _T("Volume:"), _T("Volume"), _T(""), _T(""), _T(""), true, wxPATH_MAC },
+    { _T(":Dir:File"), _T(""), _T("Dir"), _T("File"), _T(""), false, wxPATH_MAC },
+    { _T(":File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
+    { _T("File.Ext"), _T(""), _T(""), _T("File"), _T(".Ext"), false, wxPATH_MAC },
 #endif // 0
 
     // VMS file names
-    { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), TRUE, wxPATH_VMS },
-    { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), FALSE, wxPATH_VMS },
+    { _T("device:[dir1.dir2.dir3]file.txt"), _T("device"), _T("dir1.dir2.dir3"), _T("file"), _T("txt"), true, wxPATH_VMS },
+    { _T("file.txt"), _T(""), _T(""), _T("file"), _T("txt"), false, wxPATH_VMS },
 };
 
 static void TestFileNameConstruction()
@@ -1033,6 +1041,24 @@ static void TestFileNameMakeRelative()
     }
 }
 
+static void TestFileNameMakeAbsolute()
+{
+    wxPuts(_T("*** testing wxFileName::MakeAbsolute() ***"));
+
+    for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+    {
+        const FileNameInfo& fni = filenames[n];
+        wxFileName fn(fni.fullname, fni.format);
+
+        wxPrintf(_T("'%s' absolutized: "),
+               fn.GetFullPath(fni.format).c_str());
+        fn.MakeAbsolute();
+        wxPrintf(_T("'%s'\n"), fn.GetFullPath(fni.format).c_str());
+    }
+
+    wxPuts(_T(""));
+}
+
 static void TestFileNameComparison()
 {
     // TODO!
@@ -1124,7 +1150,7 @@ static void TestHash()
 
     {
         wxHashFoos hash;
-        hash.DeleteContents(TRUE);
+        hash.DeleteContents(true);
 
         wxPrintf(_T("Hash created: %u foos in hash, %u foos totally\n"),
                hash.GetCount(), Foo::count);
@@ -1353,7 +1379,7 @@ static void TestListCtor()
         wxPrintf(_T("After 2nd list creation: %u and %u objects in the lists, %u objects total.\n"),
                list1.GetCount(), list2.GetCount(), Bar::GetNumber());
 
-        list1.DeleteContents(TRUE);
+        list1.DeleteContents(true);
     }
 
     wxPrintf(_T("After list destruction: %u objects left.\n"), Bar::GetNumber());
@@ -2254,26 +2280,26 @@ static void TestRegExCompile()
         bool correct;
     } regExCompTestData[] =
     {
-        { _T("foo"), TRUE },
-        { _T("foo("), FALSE },
-        { _T("foo(bar"), FALSE },
-        { _T("foo(bar)"), TRUE },
-        { _T("foo["), FALSE },
-        { _T("foo[bar"), FALSE },
-        { _T("foo[bar]"), TRUE },
-        { _T("foo{"), TRUE },
-        { _T("foo{1"), FALSE },
-        { _T("foo{bar"), TRUE },
-        { _T("foo{1}"), TRUE },
-        { _T("foo{1,2}"), TRUE },
-        { _T("foo{bar}"), TRUE },
-        { _T("foo*"), TRUE },
-        { _T("foo**"), FALSE },
-        { _T("foo+"), TRUE },
-        { _T("foo++"), FALSE },
-        { _T("foo?"), TRUE },
-        { _T("foo??"), FALSE },
-        { _T("foo?+"), FALSE },
+        { _T("foo"), true },
+        { _T("foo("), false },
+        { _T("foo(bar"), false },
+        { _T("foo(bar)"), true },
+        { _T("foo["), false },
+        { _T("foo[bar"), false },
+        { _T("foo[bar]"), true },
+        { _T("foo{"), true },
+        { _T("foo{1"), false },
+        { _T("foo{bar"), true },
+        { _T("foo{1}"), true },
+        { _T("foo{1,2}"), true },
+        { _T("foo{bar}"), true },
+        { _T("foo*"), true },
+        { _T("foo**"), false },
+        { _T("foo+"), true },
+        { _T("foo++"), false },
+        { _T("foo?"), true },
+        { _T("foo??"), false },
+        { _T("foo?+"), false },
     };
 
     wxRegEx re;
@@ -2300,12 +2326,12 @@ static void TestRegExMatch()
         bool correct;
     } regExMatchTestData[] =
     {
-        { _T("foo"), _T("bar"), FALSE },
-        { _T("foo"), _T("foobar"), TRUE },
-        { _T("^foo"), _T("foobar"), TRUE },
-        { _T("^foo"), _T("barfoo"), FALSE },
-        { _T("bar$"), _T("barbar"), TRUE },
-        { _T("bar$"), _T("barbar "), FALSE },
+        { _T("foo"), _T("bar"), false },
+        { _T("foo"), _T("foobar"), true },
+        { _T("^foo"), _T("foobar"), true },
+        { _T("^foo"), _T("barfoo"), false },
+        { _T("bar$"), _T("barbar"), true },
+        { _T("bar$"), _T("barbar "), false },
     };
 
     for ( size_t n = 0; n < WXSIZEOF(regExMatchTestData); n++ )
@@ -2986,7 +3012,8 @@ static void TestSocketServer()
         return;
     }
 
-    for ( ;; )
+    bool quit = false;
+    while ( !quit )
     {
         wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
 
@@ -3001,7 +3028,8 @@ static void TestSocketServer()
 
         server->SetTimeout(60); // 1 min
 
-        while ( socket->IsConnected() )
+        bool close = false;
+        while ( !close && socket->IsConnected() )
         {
             wxString s;
             wxChar ch = _T('\0');
@@ -3033,19 +3061,31 @@ static void TestSocketServer()
             }
 
             wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
-            if ( s == _T("bye") )
+            if ( s == _T("close") )
             {
-                delete socket;
+                wxPuts(_T("Closing connection"));
 
-                break;
+                close = true;
             }
+            else if ( s == _T("quit") )
+            {
+                close =
+                quit = true;
 
-            socket->Write(s.MakeUpper().c_str(), s.length());
-            socket->Write("\r\n", 2);
-            wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
+                wxPuts(_T("Shutting down the server"));
+            }
+            else // not a special command
+            {
+                socket->Write(s.MakeUpper().c_str(), s.length());
+                socket->Write("\r\n", 2);
+                wxPrintf(_T("Server: wrote '%s'.\n"), s.c_str());
+            }
         }
 
-        wxPuts(_T("Server: lost a client."));
+        if ( !close )
+        {
+            wxPuts(_T("Server: lost a client unexpectedly."));
+        }
 
         socket->Destroy();
     }
@@ -3140,7 +3180,7 @@ static bool TestFtpConnect()
     {
         wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
 
-        return FALSE;
+        return false;
     }
     else
     {
@@ -3148,7 +3188,7 @@ static bool TestFtpConnect()
                hostname, ftp.Pwd().c_str());
     }
 
-    return TRUE;
+    return true;
 }
 
 // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
@@ -4122,7 +4162,6 @@ static void TestZlibStreamRead()
 
 #include <math.h>
 
-#include "wx/date.h"
 #include "wx/datetime.h"
 
 // the test data
@@ -4228,8 +4267,8 @@ static void TestTimeStatic()
         // first line: the years to test
         { 1990, 1976, 2000, 2030, 1984, },
 
-        // second line: TRUE if leap, FALSE otherwise
-        { FALSE, TRUE, TRUE, FALSE, TRUE }
+        // second line: true if leap, false otherwise
+        { false, true, true, false, true }
     };
 
     for ( size_t n = 0; n < nYears; n++ )
@@ -4831,7 +4870,7 @@ static void TestTimeFormat()
             }
             else
             {
-                bool equal = FALSE; // suppress compilaer warning
+                bool equal = false; // suppress compilaer warning
                 switch ( kind )
                 {
                     case CompareBoth:
@@ -4875,8 +4914,8 @@ static void TestTimeParse()
 
     static const ParseTestData parseTestDates[] =
     {
-        { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, TRUE },
-        { _T("Wed, 1 Dec 1999 05:17:20 +0300"),  {  1, wxDateTime::Dec, 1999, 03, 17, 20 }, TRUE },
+        { _T("Sat, 18 Dec 1999 00:46:40 +0100"), { 18, wxDateTime::Dec, 1999, 00, 46, 40 }, true },
+        { _T("Wed, 1 Dec 1999 05:17:20 +0300"),  {  1, wxDateTime::Dec, 1999, 03, 17, 20 }, true },
     };
 
     for ( size_t n = 0; n < WXSIZEOF(parseTestDates); n++ )
@@ -5122,39 +5161,46 @@ static void TestTimeSpanFormat()
     wxPuts(_T(""));
 }
 
-#if 0
+#endif // TEST_DATETIME
 
-// test compatibility with the old wxDate/wxTime classes
-static void TestTimeCompatibility()
-{
-    wxPuts(_T("\n*** wxDateTime compatibility test ***"));
+// ----------------------------------------------------------------------------
+// wxTextInput/OutputStream
+// ----------------------------------------------------------------------------
 
-    wxPrintf(_T("wxDate for JDN 0: %s\n"), wxDate(0l).FormatDate().c_str());
-    wxPrintf(_T("wxDate for MJD 0: %s\n"), wxDate(2400000).FormatDate().c_str());
+#ifdef TEST_TEXTSTREAM
 
-    double jdnNow = wxDateTime::Now().GetJDN();
-    long jdnMidnight = (long)(jdnNow - 0.5);
-    wxPrintf(_T("wxDate for today: %s\n"), wxDate(jdnMidnight).FormatDate().c_str());
+#include "wx/txtstrm.h"
+#include "wx/wfstream.h"
 
-    jdnMidnight = wxDate().Set().GetJulianDate();
-    wxPrintf(_T("wxDateTime for today: %s\n"),
-            wxDateTime((double)(jdnMidnight + 0.5)).Format("%c", wxDateTime::GMT0).c_str());
+static void TestTextInputStream()
+{
+    wxPuts(_T("\n*** wxTextInputStream test ***"));
 
-    int flags = wxEUROPEAN;//wxFULL;
-    wxDate date;
-    date.Set();
-    wxPrintf(_T("Today is %s\n"), date.FormatDate(flags).c_str());
-    for ( int n = 0; n < 7; n++ )
+    wxFileInputStream fsIn(_T("testdata.fc"));
+    if ( !fsIn.Ok() )
     {
-        wxPrintf(_T("Previous %s is %s\n"),
-               wxDateTime::GetWeekDayName((wxDateTime::WeekDay)n),
-               date.Previous(n + 1).FormatDate(flags).c_str());
+        wxPuts(_T("ERROR: couldn't open file."));
     }
-}
+    else
+    {
+        wxTextInputStream tis(fsIn);
 
-#endif // 0
+        size_t line = 1;
+        for ( ;; )
+        {
+            const wxString s = tis.ReadLine();
 
-#endif // TEST_DATETIME
+            // line could be non empty if the last line of the file isn't
+            // terminated with EOL
+            if ( fsIn.Eof() && s.empty() )
+                break;
+
+            wxPrintf(_T("Line %d: %s\n"), line++, s.c_str());
+        }
+    }
+}
+
+#endif // TEST_TEXTSTREAM
 
 // ----------------------------------------------------------------------------
 // threads
@@ -5202,7 +5248,7 @@ public:
     {
         m_n = n;
         m_ch = ch;
-        m_cancelled = FALSE;
+        m_cancelled = false;
 
         Create();
     }
@@ -5217,7 +5263,7 @@ private:
     size_t m_n; // number of characters to write
     wxChar m_ch;  // character to write
 
-    bool m_cancelled;   // FALSE if we exit normally
+    bool m_cancelled;   // false if we exit normally
 };
 
 wxThread::ExitCode MyDetachedThread::Entry()
@@ -5234,7 +5280,7 @@ wxThread::ExitCode MyDetachedThread::Entry()
     {
         if ( TestDestroy() )
         {
-            m_cancelled = TRUE;
+            m_cancelled = true;
 
             break;
         }
@@ -5853,7 +5899,7 @@ static void TestStringSub()
     {
         wxString prefix = prefixes[n], rest;
         bool rc = s.StartsWith(prefix, &rest);
-        wxPrintf(_T("StartsWith('%s') = %s"), prefix.c_str(), rc ? _T("TRUE") : _T("FALSE"));
+        wxPrintf(_T("StartsWith('%s') = %s"), prefix.c_str(), rc ? _T("true") : _T("false"));
         if ( rc )
         {
             wxPrintf(_T(" (the rest is '%s')\n"), rest.c_str());
@@ -6299,7 +6345,7 @@ int main(int argc, char **argv)
         PrintArray(_T("a1"), a1);
 
         wxPuts(_T("*** After sorting a1 in reverse order"));
-        a1.Sort(TRUE);
+        a1.Sort(true);
         PrintArray(_T("a1"), a1);
 
         wxPuts(_T("*** After sorting a1 by the string length"));
@@ -6317,9 +6363,9 @@ int main(int argc, char **argv)
     if ( TEST_ALL )
     {
         TestDirExists();
-        TestDirTraverse();
+        TestDirEnum();
     }
-    TestDirEnum();
+    TestDirTraverse();
 #endif // TEST_DIR
 
 #ifdef TEST_DLLLOADER
@@ -6347,12 +6393,17 @@ int main(int argc, char **argv)
 #endif // TEST_LOCALE
 
 #ifdef TEST_LOG
+    wxPuts(_T("*** Testing wxLog ***"));
+
     wxString s;
     for ( size_t n = 0; n < 8000; n++ )
     {
         s << (wxChar)(_T('A') + (n % 26));
     }
 
+    wxLogWarning(_T("The length of the string is %lu"),
+                 (unsigned long)s.length());
+
     wxString msg;
     msg.Printf(_T("A very very long message: '%s', the end!\n"), s.c_str());
 
@@ -6389,6 +6440,7 @@ int main(int argc, char **argv)
     {
         TestFileNameConstruction();
         TestFileNameMakeRelative();
+        TestFileNameMakeAbsolute();
         TestFileNameSplit();
         TestFileNameTemp();
         TestFileNameCwd();
@@ -6525,6 +6577,10 @@ int main(int argc, char **argv)
         TestMemoryStream();
 #endif // TEST_STREAMS
 
+#ifdef TEST_TEXTSTREAM
+    TestTextInputStream();
+#endif // TEST_TEXTSTREAM
+
 #ifdef TEST_THREADS
     int nCPUs = wxThread::GetCPUCount();
     wxPrintf(_T("This system has %d CPUs\n"), nCPUs);