]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/console/console.cpp
Added optional parameter to SelectDocumentType() and SelectViewType() to allow the...
[wxWidgets.git] / samples / console / console.cpp
index 984046b47ac5f849fabe15d5504ecd809e5e7084..395b6d6f2fa42799e082b40a23db8134a62c6d50 100644 (file)
 //#define TEST_EXECUTE
 //#define TEST_FILE
 //#define TEST_FILECONF
+#define TEST_FILENAME
+//#define TEST_FTP
 //#define TEST_HASH
 //#define TEST_LIST
 //#define TEST_LOG
 //#define TEST_LONGLONG
-#define TEST_MIME
+//#define TEST_MIME
 //#define TEST_INFO_FUNCTIONS
 //#define TEST_REGISTRY
 //#define TEST_SOCKETS
@@ -395,6 +397,7 @@ static void TestExecute()
 #ifdef TEST_FILE
 
 #include <wx/file.h>
+#include <wx/ffile.h>
 #include <wx/textfile.h>
 
 static void TestFileRead()
@@ -471,6 +474,55 @@ static void TestTextFileRead()
     puts("");
 }
 
+static void TestFileCopy()
+{
+    puts("*** Testing wxCopyFile ***");
+
+    static const wxChar *filename1 = _T("testdata.fc");
+    static const wxChar *filename2 = _T("test2");
+    if ( !wxCopyFile(filename1, filename2) )
+    {
+        puts("ERROR: failed to copy file");
+    }
+    else
+    {
+        wxFFile f1(filename1, "rb"),
+                f2(filename2, "rb");
+
+        if ( !f1.IsOpened() || !f2.IsOpened() )
+        {
+            puts("ERROR: failed to open file(s)");
+        }
+        else
+        {
+            wxString s1, s2;
+            if ( !f1.ReadAll(&s1) || !f2.ReadAll(&s2) )
+            {
+                puts("ERROR: failed to read file(s)");
+            }
+            else
+            {
+                if ( (s1.length() != s2.length()) ||
+                     (memcmp(s1.c_str(), s2.c_str(), s1.length()) != 0) )
+                {
+                    puts("ERROR: copy error!");
+                }
+                else
+                {
+                    puts("File was copied ok.");
+                }
+            }
+        }
+    }
+
+    if ( !wxRemoveFile(filename2) )
+    {
+        puts("ERROR: failed to remove the file");
+    }
+
+    puts("");
+}
+
 #endif // TEST_FILE
 
 // ----------------------------------------------------------------------------
@@ -536,6 +588,95 @@ static void TestFileConfRead()
 
 #endif // TEST_FILECONF
 
+// ----------------------------------------------------------------------------
+// wxFileName
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_FILENAME
+
+#include <wx/filename.h>
+
+static struct FileNameInfo
+{
+    const wxChar *fullname;
+    const wxChar *path;
+    const wxChar *name;
+    const wxChar *ext;
+} filenames[] =
+{
+    { _T("/usr/bin/ls"), _T("/usr/bin"), _T("ls"), _T("") },
+    { _T("/usr/bin/"), _T("/usr/bin"), _T(""), _T("") },
+    { _T("~/.zshrc"), _T("~"), _T(".zshrc"), _T("") },
+    { _T("../../foo"), _T("../.."), _T("foo"), _T("") },
+    { _T("foo.bar"), _T(""), _T("foo"), _T("bar") },
+    { _T("~/foo.bar"), _T("~"), _T("foo"), _T("bar") },
+    { _T("Mahogany-0.60/foo.bar"), _T("Mahogany-0.60"), _T("foo"), _T("bar") },
+    { _T("/tmp/wxwin.tar.bz"), _T("/tmp"), _T("wxwin.tar"), _T("bz") },
+};
+
+static void TestFileNameConstruction()
+{
+    puts("*** testing wxFileName construction ***");
+
+    for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+    {
+        wxFileName fn(filenames[n].fullname, wxPATH_UNIX);
+
+        printf("Filename: '%s'\t", fn.GetFullPath().c_str());
+        if ( !fn.Normalize(wxPATH_NORM_ALL, _T(""), wxPATH_UNIX) )
+        {
+            puts("ERROR (couldn't be normalized)");
+        }
+        else
+        {
+            printf("normalized: '%s'\n", fn.GetFullPath().c_str());
+        }
+    }
+
+    puts("");
+}
+
+static void TestFileNameSplit()
+{
+    puts("*** testing wxFileName splitting ***");
+
+    for ( size_t n = 0; n < WXSIZEOF(filenames); n++ )
+    {
+        const FileNameInfo &fni = filenames[n];
+        wxString path, name, ext;
+        wxFileName::SplitPath(fni.fullname, &path, &name, &ext);
+
+        printf("%s -> path = '%s', name = '%s', ext = '%s'",
+               fni.fullname, path.c_str(), name.c_str(), ext.c_str());
+        if ( path != fni.path )
+            printf(" (ERROR: path = '%s')", fni.path);
+        if ( name != fni.name )
+            printf(" (ERROR: name = '%s')", fni.name);
+        if ( ext != fni.ext )
+            printf(" (ERROR: ext = '%s')", fni.ext);
+        puts("");
+    }
+
+    puts("");
+}
+
+static void TestFileNameComparison()
+{
+    // TODO!
+}
+
+static void TestFileNameOperations()
+{
+    // TODO!
+}
+
+static void TestFileNameCwd()
+{
+    // TODO!
+}
+
+#endif // TEST_FILENAME
+
 // ----------------------------------------------------------------------------
 // wxHashTable
 // ----------------------------------------------------------------------------
@@ -1241,7 +1382,6 @@ static void TestRegistryAssociation()
 
 #include <wx/socket.h>
 #include <wx/protocol/protocol.h>
-#include <wx/protocol/ftp.h>
 #include <wx/protocol/http.h>
 
 static void TestSocketServer()
@@ -1364,17 +1504,70 @@ static void TestSocketClient()
     }
 }
 
+#endif // TEST_SOCKETS
+
+#ifdef TEST_FTP
+
+#include <wx/protocol/ftp.h>
+
 static void TestProtocolFtp()
 {
     puts("*** Testing wxFTP download ***\n");
 
-    wxLog::AddTraceMask(_T("ftp"));
+    wxFTP ftp;
 
+#ifdef TEST_WUFTPD // test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
+    static const char *hostname = "ftp.eudora.com";
+    if ( !ftp.Connect(hostname) )
+    {
+        printf("ERROR: failed to connect to %s\n", hostname);
+    }
+    else
+    {
+        static const char *filename = "eudora/pubs/draft-gellens-submit-09.txt";
+        wxInputStream *in = ftp.GetInputStream(filename);
+        if ( !in )
+        {
+            printf("ERROR: couldn't get input stream for %s\n", filename);
+        }
+        else
+        {
+            size_t size = in->StreamSize();
+            printf("Reading file %s (%u bytes)...", filename, size);
+
+            char *data = new char[size];
+            if ( !in->Read(data, size) )
+            {
+                puts("ERROR: read error");
+            }
+            else
+            {
+                printf("Successfully retrieved the file.\n");
+            }
+
+            delete [] data;
+            delete in;
+        }
+    }
+#else // !TEST_WUFTPD
+
+#if 1
     static const char *hostname = "ftp.wxwindows.org";
+    static const char *directory = "pub";
+    static const char *filename = "welcome.msg";
 
-    printf("--- Attempting to connect to %s:21...\n", hostname);
+    printf("--- Attempting to connect to %s:21 anonymously...\n", hostname);
+#else
+    static const char *hostname = "localhost";
+    static const char *user = "zeitlin";
+    static const char *directory = "/tmp";
+
+    ftp.SetUser(user);
+    ftp.SetPassword("password");
+
+    printf("--- Attempting to connect to %s:21 as %s...\n", hostname, user);
+#endif
 
-    wxFTP ftp;
     if ( !ftp.Connect(hostname) )
     {
         printf("ERROR: failed to connect to %s\n", hostname);
@@ -1383,19 +1576,37 @@ static void TestProtocolFtp()
     {
         printf("--- Connected to %s, current directory is '%s'\n",
                hostname, ftp.Pwd().c_str());
-        if ( !ftp.ChDir(_T("pub")) )
+
+        // test CWD
+        if ( !ftp.ChDir(directory) )
         {
-            puts("ERROR: failed to cd to pub");
+            printf("ERROR: failed to cd to %s\n", directory);
         }
 
+        // test NLIST and LIST
         wxArrayString files;
-        if ( !ftp.GetList(files) )
+        if ( !ftp.GetFilesList(files) )
+        {
+            puts("ERROR: failed to get NLIST of files");
+        }
+        else
+        {
+            printf("Brief list of files under '%s':\n", ftp.Pwd().c_str());
+            size_t count = files.GetCount();
+            for ( size_t n = 0; n < count; n++ )
+            {
+                printf("\t%s\n", files[n].c_str());
+            }
+            puts("End of the file list");
+        }
+
+        if ( !ftp.GetDirList(files) )
         {
-            puts("ERROR: failed to get list of files");
+            puts("ERROR: failed to get LIST of files");
         }
         else
         {
-            printf("List of files under '%s':\n", ftp.Pwd().c_str());
+            printf("Detailed list of files under '%s':\n", ftp.Pwd().c_str());
             size_t count = files.GetCount();
             for ( size_t n = 0; n < count; n++ )
             {
@@ -1409,11 +1620,11 @@ static void TestProtocolFtp()
             puts("ERROR: failed to cd to ..");
         }
 
-        static const char *filename = "welcome.msg";
+        // test RETR
         wxInputStream *in = ftp.GetInputStream(filename);
         if ( !in )
         {
-            puts("ERROR: couldn't get input stream");
+            printf("ERROR: couldn't get input stream for %s\n", filename);
         }
         else
         {
@@ -1433,22 +1644,41 @@ static void TestProtocolFtp()
             delete [] data;
             delete in;
         }
+
+        // test some other FTP commands
+        if ( ftp.SendCommand("STAT") != '2' )
+        {
+            puts("ERROR: STAT failed");
+        }
+        else
+        {
+            printf("STAT returned:\n\n%s\n", ftp.GetLastResult().c_str());
+        }
+
+        if ( ftp.SendCommand("HELP SITE") != '2' )
+        {
+            puts("ERROR: HELP SITE failed");
+        }
+        else
+        {
+            printf("The list of site-specific commands:\n\n%s\n",
+                   ftp.GetLastResult().c_str());
+        }
     }
+#endif // TEST_WUFTPD/!TEST_WUFTPD
 }
 
 static void TestProtocolFtpUpload()
 {
     puts("*** Testing wxFTP uploading ***\n");
 
-    wxLog::AddTraceMask(_T("ftp"));
-
     static const char *hostname = "localhost";
 
     printf("--- Attempting to connect to %s:21...\n", hostname);
 
     wxFTP ftp;
     ftp.SetUser("zeitlin");
-    ftp.SetPassword("insert your password here");
+    ftp.SetPassword("password");
     if ( !ftp.Connect(hostname) )
     {
         printf("ERROR: failed to connect to %s\n", hostname);
@@ -1469,6 +1699,17 @@ static void TestProtocolFtpUpload()
             delete out;
         }
 
+        // send a command to check the remote file
+        if ( ftp.SendCommand(wxString("STAT ") + file1) != '2' )
+        {
+            printf("ERROR: STAT %s failed\n", file1);
+        }
+        else
+        {
+            printf("STAT %s returned:\n\n%s\n",
+                   file1, ftp.GetLastResult().c_str());
+        }
+
         out = ftp.GetOutputStream(file2);
         if ( out )
         {
@@ -1479,7 +1720,7 @@ static void TestProtocolFtpUpload()
     }
 }
 
-#endif // TEST_SOCKETS
+#endif // TEST_FTP
 
 // ----------------------------------------------------------------------------
 // streams
@@ -3785,10 +4026,24 @@ int main(int argc, char **argv)
 
 #ifdef TEST_FILE
     if ( 0 )
+    {
         TestFileRead();
-    TestTextFileRead();
+        TestTextFileRead();
+    }
+    TestFileCopy();
 #endif // TEST_FILE
 
+#ifdef TEST_FILENAME
+    TestFileNameSplit();
+    if ( 0 )
+    {
+        TestFileNameConstruction();
+        TestFileNameCwd();
+        TestFileNameComparison();
+        TestFileNameOperations();
+    }
+#endif // TEST_FILENAME
+
 #ifdef TEST_THREADS
     int nCPUs = wxThread::GetCPUCount();
     printf("This system has %d CPUs\n", nCPUs);
@@ -3858,12 +4113,17 @@ int main(int argc, char **argv)
     if ( 0 )
     {
         TestSocketServer();
-        TestSocketClient();
-        TestProtocolFtp();
     }
-        TestProtocolFtpUpload();
+        TestSocketClient();
 #endif // TEST_SOCKETS
 
+#ifdef TEST_FTP
+    wxLog::AddTraceMask(_T("ftp"));
+    TestProtocolFtp();
+    if ( 0 )
+    TestProtocolFtpUpload();
+#endif // TEST_FTP
+
 #ifdef TEST_STREAMS
     TestMemoryStream();
 #endif // TEST_STREAMS
@@ -3873,7 +4133,7 @@ int main(int argc, char **argv)
 #endif // TEST_TIMER
 
 #ifdef TEST_DATETIME
-    if ( 0 )
+    if ( 1 )
     {
         TestTimeSet();
         TestTimeStatic();