+static void TestFtpList()
+{
+ puts("*** Testing wxFTP file listing ***\n");
+
+ // test CWD
+ if ( !ftp.ChDir(directory) )
+ {
+ printf("ERROR: failed to cd to %s\n", directory);
+ }
+
+ printf("Current directory is '%s'\n", ftp.Pwd().c_str());
+
+ // test NLIST and LIST
+ wxArrayString 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");
+ }
+ else
+ {
+ 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++ )
+ {
+ printf("\t%s\n", files[n].c_str());
+ }
+ puts("End of the file list");
+ }
+
+ if ( !ftp.ChDir(_T("..")) )
+ {
+ puts("ERROR: failed to cd to ..");
+ }
+
+ printf("Current directory is '%s'\n", ftp.Pwd().c_str());
+}
+
+static void TestFtpDownload()
+{
+ puts("*** Testing wxFTP download ***\n");
+
+ // test RETR
+ 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);
+ fflush(stdout);
+
+ char *data = new char[size];
+ if ( !in->Read(data, size) )
+ {
+ puts("ERROR: read error");
+ }
+ else
+ {
+ printf("\nContents of %s:\n%s\n", filename, data);
+ }
+
+ delete [] data;
+ delete in;
+ }
+}
+
+static void TestFtpFileSize()
+{
+ puts("*** Testing FTP SIZE command ***");
+
+ if ( !ftp.ChDir(directory) )
+ {
+ printf("ERROR: failed to cd to %s\n", directory);
+ }
+
+ printf("Current directory is '%s'\n", ftp.Pwd().c_str());
+
+ if ( ftp.FileExists(filename) )
+ {
+ int size = ftp.GetFileSize(filename);
+ if ( size == -1 )
+ printf("ERROR: couldn't get size of '%s'\n", filename);
+ else
+ printf("Size of '%s' is %d bytes.\n", filename, size);
+ }
+ else
+ {
+ printf("ERROR: '%s' doesn't exist\n", filename);
+ }
+}
+
+static void TestFtpMisc()
+{
+ puts("*** Testing miscellaneous wxFTP functions ***");
+
+ 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());
+ }
+}
+
+static void TestFtpInteractive()
+{
+ puts("\n*** Interactive wxFTP test ***");
+
+ char buf[128];
+
+ for ( ;; )
+ {
+ printf("Enter FTP command: ");
+ if ( !fgets(buf, WXSIZEOF(buf), stdin) )
+ break;
+
+ // kill the last '\n'
+ buf[strlen(buf) - 1] = 0;
+
+ // special handling of LIST and NLST as they require data connection
+ wxString start(buf, 4);
+ start.MakeUpper();
+ if ( start == "LIST" || start == "NLST" )
+ {
+ wxString wildcard;
+ if ( strlen(buf) > 4 )
+ wildcard = buf + 5;
+
+ wxArrayString files;
+ if ( !ftp.GetList(files, wildcard, start == "LIST") )
+ {
+ printf("ERROR: failed to get %s of files\n", start.c_str());
+ }
+ else
+ {
+ printf("--- %s of '%s' under '%s':\n",
+ start.c_str(), wildcard.c_str(), 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");
+ }
+ }
+ else // !list
+ {
+ char ch = ftp.SendCommand(buf);
+ printf("Command %s", ch ? "succeeded" : "failed");
+ if ( ch )
+ {
+ printf(" (return code %c)", ch);
+ }
+
+ printf(", server reply:\n%s\n\n", ftp.GetLastResult().c_str());
+ }
+ }
+
+ puts("\n*** done ***");
+}
+
+static void TestFtpUpload()
+{
+ puts("*** Testing wxFTP uploading ***\n");
+
+ // upload a file
+ static const char *file1 = "test1";
+ static const char *file2 = "test2";
+ wxOutputStream *out = ftp.GetOutputStream(file1);
+ if ( out )
+ {
+ printf("--- Uploading to %s ---\n", file1);
+ out->Write("First hello", 11);
+ 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 )
+ {
+ printf("--- Uploading to %s ---\n", file1);
+ out->Write("Second hello", 12);
+ delete out;
+ }
+}
+
+#endif // TEST_FTP
+
+// ----------------------------------------------------------------------------
+// streams
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_STREAMS
+
+#include <wx/wfstream.h>
+#include <wx/mstream.h>
+
+static void TestFileStream()
+{
+ puts("*** Testing wxFileInputStream ***");
+
+ static const wxChar *filename = _T("testdata.fs");
+ {
+ wxFileOutputStream fsOut(filename);
+ fsOut.Write("foo", 3);
+ }
+
+ wxFileInputStream fsIn(filename);
+ printf("File stream size: %u\n", fsIn.GetSize());
+ while ( !fsIn.Eof() )
+ {
+ putchar(fsIn.GetC());
+ }
+
+ if ( !wxRemoveFile(filename) )
+ {
+ printf("ERROR: failed to remove the file '%s'.\n", filename);
+ }
+
+ puts("\n*** wxFileInputStream test done ***");
+}
+
+static void TestMemoryStream()
+{
+ puts("*** Testing wxMemoryInputStream ***");
+
+ wxChar buf[1024];
+ wxStrncpy(buf, _T("Hello, stream!"), WXSIZEOF(buf));
+
+ wxMemoryInputStream memInpStream(buf, wxStrlen(buf));
+ printf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
+ while ( !memInpStream.Eof() )
+ {
+ putchar(memInpStream.GetC());
+ }
+
+ puts("\n*** wxMemoryInputStream test done ***");
+}
+
+#endif // TEST_STREAMS
+
+// ----------------------------------------------------------------------------
+// timers
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_TIMER
+
+#include <wx/timer.h>
+#include <wx/utils.h>
+
+static void TestStopWatch()
+{
+ puts("*** Testing wxStopWatch ***\n");
+
+ wxStopWatch sw;
+ printf("Sleeping 3 seconds...");
+ wxSleep(3);
+ printf("\telapsed time: %ldms\n", sw.Time());
+
+ sw.Pause();
+ printf("Sleeping 2 more seconds...");
+ wxSleep(2);
+ printf("\telapsed time: %ldms\n", sw.Time());
+
+ sw.Resume();
+ printf("And 3 more seconds...");
+ wxSleep(3);
+ printf("\telapsed time: %ldms\n", sw.Time());
+
+ wxStopWatch sw2;
+ puts("\nChecking for 'backwards clock' bug...");
+ for ( size_t n = 0; n < 70; n++ )
+ {
+ sw2.Start();
+
+ for ( size_t m = 0; m < 100000; m++ )
+ {
+ if ( sw.Time() < 0 || sw2.Time() < 0 )
+ {
+ puts("\ntime is negative - ERROR!");
+ }
+ }
+
+ putchar('.');
+ }
+
+ puts(", ok.");
+}
+
+#endif // TEST_TIMER
+
+// ----------------------------------------------------------------------------
+// vCard support
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_VCARD
+
+#include <wx/vcard.h>
+
+static void DumpVObject(size_t level, const wxVCardObject& vcard)
+{
+ void *cookie;
+ wxVCardObject *vcObj = vcard.GetFirstProp(&cookie);
+ while ( vcObj )
+ {
+ printf("%s%s",
+ wxString(_T('\t'), level).c_str(),
+ vcObj->GetName().c_str());
+
+ wxString value;
+ switch ( vcObj->GetType() )
+ {
+ case wxVCardObject::String:
+ case wxVCardObject::UString:
+ {
+ wxString val;
+ vcObj->GetValue(&val);
+ value << _T('"') << val << _T('"');
+ }
+ break;
+
+ case wxVCardObject::Int:
+ {
+ unsigned int i;
+ vcObj->GetValue(&i);
+ value.Printf(_T("%u"), i);
+ }
+ break;
+
+ case wxVCardObject::Long:
+ {
+ unsigned long l;
+ vcObj->GetValue(&l);
+ value.Printf(_T("%lu"), l);
+ }
+ break;
+
+ case wxVCardObject::None:
+ break;
+
+ case wxVCardObject::Object:
+ value = _T("<node>");
+ break;
+
+ default:
+ value = _T("<unknown value type>");
+ }
+
+ if ( !!value )
+ printf(" = %s", value.c_str());
+ putchar('\n');
+
+ DumpVObject(level + 1, *vcObj);
+
+ delete vcObj;
+ vcObj = vcard.GetNextProp(&cookie);
+ }
+}
+
+static void DumpVCardAddresses(const wxVCard& vcard)
+{
+ puts("\nShowing all addresses from vCard:\n");
+
+ size_t nAdr = 0;
+ void *cookie;
+ wxVCardAddress *addr = vcard.GetFirstAddress(&cookie);
+ while ( addr )
+ {
+ wxString flagsStr;
+ int flags = addr->GetFlags();
+ if ( flags & wxVCardAddress::Domestic )
+ {
+ flagsStr << _T("domestic ");
+ }
+ if ( flags & wxVCardAddress::Intl )
+ {
+ flagsStr << _T("international ");
+ }
+ if ( flags & wxVCardAddress::Postal )
+ {
+ flagsStr << _T("postal ");
+ }
+ if ( flags & wxVCardAddress::Parcel )
+ {
+ flagsStr << _T("parcel ");
+ }
+ if ( flags & wxVCardAddress::Home )
+ {
+ flagsStr << _T("home ");
+ }
+ if ( flags & wxVCardAddress::Work )
+ {
+ flagsStr << _T("work ");
+ }
+
+ printf("Address %u:\n"
+ "\tflags = %s\n"
+ "\tvalue = %s;%s;%s;%s;%s;%s;%s\n",
+ ++nAdr,
+ flagsStr.c_str(),
+ addr->GetPostOffice().c_str(),
+ addr->GetExtAddress().c_str(),
+ addr->GetStreet().c_str(),
+ addr->GetLocality().c_str(),
+ addr->GetRegion().c_str(),
+ addr->GetPostalCode().c_str(),
+ addr->GetCountry().c_str()
+ );
+
+ delete addr;
+ addr = vcard.GetNextAddress(&cookie);
+ }
+}
+
+static void DumpVCardPhoneNumbers(const wxVCard& vcard)
+{
+ puts("\nShowing all phone numbers from vCard:\n");
+
+ size_t nPhone = 0;
+ void *cookie;
+ wxVCardPhoneNumber *phone = vcard.GetFirstPhoneNumber(&cookie);
+ while ( phone )
+ {
+ wxString flagsStr;
+ int flags = phone->GetFlags();
+ if ( flags & wxVCardPhoneNumber::Voice )
+ {
+ flagsStr << _T("voice ");
+ }
+ if ( flags & wxVCardPhoneNumber::Fax )
+ {
+ flagsStr << _T("fax ");
+ }
+ if ( flags & wxVCardPhoneNumber::Cellular )
+ {
+ flagsStr << _T("cellular ");
+ }
+ if ( flags & wxVCardPhoneNumber::Modem )
+ {
+ flagsStr << _T("modem ");
+ }
+ if ( flags & wxVCardPhoneNumber::Home )
+ {
+ flagsStr << _T("home ");
+ }
+ if ( flags & wxVCardPhoneNumber::Work )
+ {
+ flagsStr << _T("work ");
+ }
+
+ printf("Phone number %u:\n"
+ "\tflags = %s\n"
+ "\tvalue = %s\n",
+ ++nPhone,
+ flagsStr.c_str(),
+ phone->GetNumber().c_str()
+ );
+
+ delete phone;
+ phone = vcard.GetNextPhoneNumber(&cookie);
+ }
+}
+
+static void TestVCardRead()
+{
+ puts("*** Testing wxVCard reading ***\n");
+
+ wxVCard vcard(_T("vcard.vcf"));
+ if ( !vcard.IsOk() )
+ {
+ puts("ERROR: couldn't load vCard.");
+ }
+ else
+ {
+ // read individual vCard properties
+ wxVCardObject *vcObj = vcard.GetProperty("FN");
+ wxString value;
+ if ( vcObj )
+ {
+ vcObj->GetValue(&value);
+ delete vcObj;
+ }
+ else
+ {
+ value = _T("<none>");
+ }
+
+ printf("Full name retrieved directly: %s\n", value.c_str());
+
+
+ if ( !vcard.GetFullName(&value) )
+ {
+ value = _T("<none>");
+ }
+
+ printf("Full name from wxVCard API: %s\n", value.c_str());
+
+ // now show how to deal with multiply occuring properties
+ DumpVCardAddresses(vcard);
+ DumpVCardPhoneNumbers(vcard);
+
+ // and finally show all
+ puts("\nNow dumping the entire vCard:\n"
+ "-----------------------------\n");
+
+ DumpVObject(0, vcard);
+ }
+}
+
+static void TestVCardWrite()
+{
+ puts("*** Testing wxVCard writing ***\n");
+
+ wxVCard vcard;
+ if ( !vcard.IsOk() )
+ {
+ puts("ERROR: couldn't create vCard.");
+ }
+ else
+ {
+ // set some fields
+ vcard.SetName("Zeitlin", "Vadim");
+ vcard.SetFullName("Vadim Zeitlin");
+ vcard.SetOrganization("wxWindows", "R&D");
+
+ // just dump the vCard back
+ puts("Entire vCard follows:\n");
+ puts(vcard.Write());
+ }
+}
+
+#endif // TEST_VCARD
+
+// ----------------------------------------------------------------------------
+// wide char (Unicode) support
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_WCHAR
+
+#include <wx/strconv.h>
+#include <wx/fontenc.h>
+#include <wx/encconv.h>
+#include <wx/buffer.h>
+
+static void TestUtf8()
+{
+ puts("*** Testing UTF8 support ***\n");
+
+ static const char textInUtf8[] =
+ {
+ 208, 157, 208, 181, 209, 129, 208, 186, 208, 176, 208, 183, 208, 176,
+ 208, 189, 208, 189, 208, 190, 32, 208, 191, 208, 190, 209, 128, 208,
+ 176, 208, 180, 208, 190, 208, 178, 208, 176, 208, 187, 32, 208, 188,
+ 208, 181, 208, 189, 209, 143, 32, 209, 129, 208, 178, 208, 190, 208,
+ 181, 208, 185, 32, 208, 186, 209, 128, 209, 131, 209, 130, 208, 181,
+ 208, 185, 209, 136, 208, 181, 208, 185, 32, 208, 189, 208, 190, 208,
+ 178, 208, 190, 209, 129, 209, 130, 209, 140, 209, 142, 0
+ };
+
+ char buf[1024];
+ wchar_t wbuf[1024];
+ if ( wxConvUTF8.MB2WC(wbuf, textInUtf8, WXSIZEOF(textInUtf8)) <= 0 )
+ {
+ puts("ERROR: UTF-8 decoding failed.");
+ }
+ else
+ {
+ // using wxEncodingConverter
+#if 0
+ wxEncodingConverter ec;
+ ec.Init(wxFONTENCODING_UNICODE, wxFONTENCODING_KOI8);
+ ec.Convert(wbuf, buf);
+#else // using wxCSConv
+ wxCSConv conv(_T("koi8-r"));
+ if ( conv.WC2MB(buf, wbuf, 0 /* not needed wcslen(wbuf) */) <= 0 )
+ {
+ puts("ERROR: conversion to KOI8-R failed.");
+ }
+ else
+#endif
+
+ printf("The resulting string (in koi8-r): %s\n", buf);
+ }
+}
+
+#endif // TEST_WCHAR
+
+// ----------------------------------------------------------------------------
+// ZIP stream
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_ZIP
+
+#include "wx/filesys.h"
+#include "wx/fs_zip.h"
+#include "wx/zipstrm.h"
+
+static const wxChar *TESTFILE_ZIP = _T("testdata.zip");
+
+static void TestZipStreamRead()
+{
+ puts("*** Testing ZIP reading ***\n");
+
+ static const wxChar *filename = _T("foo");
+ wxZipInputStream istr(TESTFILE_ZIP, filename);
+ printf("Archive size: %u\n", istr.GetSize());
+
+ printf("Dumping the file '%s':\n", filename);
+ while ( !istr.Eof() )
+ {
+ putchar(istr.GetC());
+ fflush(stdout);
+ }
+
+ puts("\n----- done ------");
+}
+
+static void DumpZipDirectory(wxFileSystem& fs,
+ const wxString& dir,
+ const wxString& indent)
+{
+ wxString prefix = wxString::Format(_T("%s#zip:%s"),
+ TESTFILE_ZIP, dir.c_str());
+ wxString wildcard = prefix + _T("/*");
+
+ wxString dirname = fs.FindFirst(wildcard, wxDIR);
+ while ( !dirname.empty() )
+ {
+ if ( !dirname.StartsWith(prefix + _T('/'), &dirname) )
+ {
+ wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
+
+ break;
+ }
+
+ wxPrintf(_T("%s%s\n"), indent.c_str(), dirname.c_str());
+
+ DumpZipDirectory(fs, dirname,
+ indent + wxString(_T(' '), 4));
+
+ dirname = fs.FindNext();
+ }
+
+ wxString filename = fs.FindFirst(wildcard, wxFILE);
+ while ( !filename.empty() )
+ {
+ if ( !filename.StartsWith(prefix, &filename) )
+ {
+ wxPrintf(_T("ERROR: unexpected wxFileSystem::FindNext result\n"));
+
+ break;
+ }
+
+ wxPrintf(_T("%s%s\n"), indent.c_str(), filename.c_str());
+
+ filename = fs.FindNext();
+ }
+}
+
+static void TestZipFileSystem()
+{
+ puts("*** Testing ZIP file system ***\n");
+
+ wxFileSystem::AddHandler(new wxZipFSHandler);
+ wxFileSystem fs;
+ wxPrintf(_T("Dumping all files in the archive %s:\n"), TESTFILE_ZIP);
+
+ DumpZipDirectory(fs, _T(""), wxString(_T(' '), 4));
+}
+
+#endif // TEST_ZIP