+#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
+// ----------------------------------------------------------------------------
+
+/*
+ NB: this stuff was taken from the glibc test suite and modified to build
+ in wxWidgets: if I read the copyright below properly, this shouldn't
+ be a problem
+ */
+
+#ifdef TEST_PRINTF
+
+#ifdef wxTEST_PRINTF
+ // use our functions from wxchar.cpp
+ #undef wxPrintf
+ #undef wxSprintf
+
+ // NB: do _not_ use ATTRIBUTE_PRINTF here, we have some invalid formats
+ // in the tests below
+ int wxPrintf( const wxChar *format, ... );
+ int wxSprintf( wxChar *str, const wxChar *format, ... );
+#endif
+
+#include "wx/longlong.h"
+
+#include <float.h>
+
+static void rfg1 (void);
+static void rfg2 (void);
+
+
+static void
+fmtchk (const wxChar *fmt)
+{
+ (void) wxPrintf(_T("%s:\t`"), fmt);
+ (void) wxPrintf(fmt, 0x12);
+ (void) wxPrintf(_T("'\n"));
+}
+
+static void
+fmtst1chk (const wxChar *fmt)
+{
+ (void) wxPrintf(_T("%s:\t`"), fmt);
+ (void) wxPrintf(fmt, 4, 0x12);
+ (void) wxPrintf(_T("'\n"));
+}
+
+static void
+fmtst2chk (const wxChar *fmt)
+{
+ (void) wxPrintf(_T("%s:\t`"), fmt);
+ (void) wxPrintf(fmt, 4, 4, 0x12);
+ (void) wxPrintf(_T("'\n"));
+}
+
+/* This page is covered by the following copyright: */
+
+/* (C) Copyright C E Chew
+ *
+ * Feel free to copy, use and distribute this software provided:
+ *
+ * 1. you do not pretend that you wrote it
+ * 2. you leave this copyright notice intact.
+ */
+
+/*
+ * Extracted from exercise.c for glibc-1.05 bug report by Bruce Evans.
+ */
+
+#define DEC -123
+#define INT 255
+#define UNS (~0)
+
+/* Formatted Output Test
+ *
+ * This exercises the output formatting code.
+ */
+
+wxChar *PointerNull = NULL;
+
+static void
+fp_test (void)
+{
+ int i, j, k, l;
+ wxChar buf[7];
+ wxChar *prefix = buf;
+ wxChar tp[20];
+
+ wxPuts(_T("\nFormatted output test"));
+ wxPrintf(_T("prefix 6d 6o 6x 6X 6u\n"));
+ wxStrcpy(prefix, _T("%"));
+ for (i = 0; i < 2; i++) {
+ for (j = 0; j < 2; j++) {
+ for (k = 0; k < 2; k++) {
+ for (l = 0; l < 2; l++) {
+ wxStrcpy(prefix, _T("%"));
+ if (i == 0) wxStrcat(prefix, _T("-"));
+ if (j == 0) wxStrcat(prefix, _T("+"));
+ if (k == 0) wxStrcat(prefix, _T("#"));
+ if (l == 0) wxStrcat(prefix, _T("0"));
+ wxPrintf(_T("%5s |"), prefix);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6d |"));
+ wxPrintf(tp, DEC);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6o |"));
+ wxPrintf(tp, INT);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6x |"));
+ wxPrintf(tp, INT);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6X |"));
+ wxPrintf(tp, INT);
+ wxStrcpy(tp, prefix);
+ wxStrcat(tp, _T("6u |"));
+ wxPrintf(tp, UNS);
+ wxPrintf(_T("\n"));
+ }
+ }
+ }
+ }
+ wxPrintf(_T("%10s\n"), PointerNull);
+ wxPrintf(_T("%-10s\n"), PointerNull);
+}
+
+static void TestPrintf()
+{
+ static wxChar shortstr[] = _T("Hi, Z.");
+ static wxChar longstr[] = _T("Good morning, Doctor Chandra. This is Hal. \
+I am ready for my first lesson today.");
+ int result = 0;
+ wxString test_format;
+
+ fmtchk(_T("%.4x"));
+ fmtchk(_T("%04x"));
+ fmtchk(_T("%4.4x"));
+ fmtchk(_T("%04.4x"));
+ fmtchk(_T("%4.3x"));
+ fmtchk(_T("%04.3x"));
+
+ fmtst1chk(_T("%.*x"));
+ fmtst1chk(_T("%0*x"));
+ fmtst2chk(_T("%*.*x"));
+ fmtst2chk(_T("%0*.*x"));
+
+ wxString bad_format = _T("bad format:\t\"%b\"\n");
+ wxPrintf(bad_format.c_str());
+ wxPrintf(_T("nil pointer (padded):\t\"%10p\"\n"), (void *) NULL);
+
+ wxPrintf(_T("decimal negative:\t\"%d\"\n"), -2345);
+ wxPrintf(_T("octal negative:\t\"%o\"\n"), -2345);
+ wxPrintf(_T("hex negative:\t\"%x\"\n"), -2345);
+ wxPrintf(_T("long decimal number:\t\"%ld\"\n"), -123456L);
+ wxPrintf(_T("long octal negative:\t\"%lo\"\n"), -2345L);
+ wxPrintf(_T("long unsigned decimal number:\t\"%lu\"\n"), -123456L);
+ wxPrintf(_T("zero-padded LDN:\t\"%010ld\"\n"), -123456L);
+ test_format = _T("left-adjusted ZLDN:\t\"%-010ld\"\n");
+ wxPrintf(test_format.c_str(), -123456);
+ wxPrintf(_T("space-padded LDN:\t\"%10ld\"\n"), -123456L);
+ wxPrintf(_T("left-adjusted SLDN:\t\"%-10ld\"\n"), -123456L);
+
+ test_format = _T("zero-padded string:\t\"%010s\"\n");
+ wxPrintf(test_format.c_str(), shortstr);
+ test_format = _T("left-adjusted Z string:\t\"%-010s\"\n");
+ wxPrintf(test_format.c_str(), shortstr);
+ wxPrintf(_T("space-padded string:\t\"%10s\"\n"), shortstr);
+ wxPrintf(_T("left-adjusted S string:\t\"%-10s\"\n"), shortstr);
+ wxPrintf(_T("null string:\t\"%s\"\n"), PointerNull);
+ wxPrintf(_T("limited string:\t\"%.22s\"\n"), longstr);
+
+ wxPrintf(_T("e-style >= 1:\t\"%e\"\n"), 12.34);
+ wxPrintf(_T("e-style >= .1:\t\"%e\"\n"), 0.1234);
+ wxPrintf(_T("e-style < .1:\t\"%e\"\n"), 0.001234);
+ wxPrintf(_T("e-style big:\t\"%.60e\"\n"), 1e20);
+ wxPrintf(_T("e-style == .1:\t\"%e\"\n"), 0.1);
+ wxPrintf(_T("f-style >= 1:\t\"%f\"\n"), 12.34);
+ wxPrintf(_T("f-style >= .1:\t\"%f\"\n"), 0.1234);
+ wxPrintf(_T("f-style < .1:\t\"%f\"\n"), 0.001234);
+ wxPrintf(_T("g-style >= 1:\t\"%g\"\n"), 12.34);
+ wxPrintf(_T("g-style >= .1:\t\"%g\"\n"), 0.1234);
+ wxPrintf(_T("g-style < .1:\t\"%g\"\n"), 0.001234);
+ wxPrintf(_T("g-style big:\t\"%.60g\"\n"), 1e20);
+
+ wxPrintf (_T(" %6.5f\n"), .099999999860301614);
+ wxPrintf (_T(" %6.5f\n"), .1);
+ wxPrintf (_T("x%5.4fx\n"), .5);
+
+ wxPrintf (_T("%#03x\n"), 1);
+
+ //wxPrintf (_T("something really insane: %.10000f\n"), 1.0);
+
+ {
+ double d = FLT_MIN;
+ int niter = 17;
+
+ while (niter-- != 0)
+ wxPrintf (_T("%.17e\n"), d / 2);
+ fflush (stdout);
+ }
+
+#ifndef __WATCOMC__
+ // Open Watcom cause compiler error here
+ // Error! E173: col(24) floating-point constant too small to represent
+ wxPrintf (_T("%15.5e\n"), 4.9406564584124654e-324);
+#endif
+
+#define FORMAT _T("|%12.4f|%12.4e|%12.4g|\n")
+ wxPrintf (FORMAT, 0.0, 0.0, 0.0);
+ wxPrintf (FORMAT, 1.0, 1.0, 1.0);
+ wxPrintf (FORMAT, -1.0, -1.0, -1.0);
+ wxPrintf (FORMAT, 100.0, 100.0, 100.0);
+ wxPrintf (FORMAT, 1000.0, 1000.0, 1000.0);
+ wxPrintf (FORMAT, 10000.0, 10000.0, 10000.0);
+ wxPrintf (FORMAT, 12345.0, 12345.0, 12345.0);
+ wxPrintf (FORMAT, 100000.0, 100000.0, 100000.0);
+ wxPrintf (FORMAT, 123456.0, 123456.0, 123456.0);
+#undef FORMAT
+
+ {
+ wxChar buf[20];
+ int rc = wxSnprintf (buf, WXSIZEOF(buf), _T("%30s"), _T("foo"));
+
+ wxPrintf(_T("snprintf (\"%%30s\", \"foo\") == %d, \"%.*s\"\n"),
+ rc, WXSIZEOF(buf), buf);
+#if 0
+ wxChar buf2[512];
+ wxPrintf ("snprintf (\"%%.999999u\", 10)\n",
+ wxSnprintf(buf2, WXSIZEOFbuf2), "%.999999u", 10));
+#endif
+ }
+
+ fp_test ();
+
+ wxPrintf (_T("%e should be 1.234568e+06\n"), 1234567.8);
+ wxPrintf (_T("%f should be 1234567.800000\n"), 1234567.8);
+ wxPrintf (_T("%g should be 1.23457e+06\n"), 1234567.8);
+ wxPrintf (_T("%g should be 123.456\n"), 123.456);
+ wxPrintf (_T("%g should be 1e+06\n"), 1000000.0);
+ wxPrintf (_T("%g should be 10\n"), 10.0);
+ wxPrintf (_T("%g should be 0.02\n"), 0.02);
+
+ {
+ double x=1.0;
+ wxPrintf(_T("%.17f\n"),(1.0/x/10.0+1.0)*x-x);
+ }
+
+ {
+ wxChar buf[200];
+
+ wxSprintf(buf,_T("%*s%*s%*s"),-1,_T("one"),-20,_T("two"),-30,_T("three"));
+
+ result |= wxStrcmp (buf,
+ _T("onetwo three "));
+
+ wxPuts (result != 0 ? _T("Test failed!") : _T("Test ok."));
+ }
+
+#ifdef wxLongLong_t
+ {
+ wxChar buf[200];
+
+ wxSprintf(buf, _T("%07") wxLongLongFmtSpec _T("o"), wxLL(040000000000));
+ #if 0
+ // for some reason below line fails under Borland
+ wxPrintf (_T("sprintf (buf, \"%%07Lo\", 040000000000ll) = %s"), buf);
+ #endif
+
+ if (wxStrcmp (buf, _T("40000000000")) != 0)
+ {
+ result = 1;
+ wxPuts (_T("\tFAILED"));
+ }
+ wxUnusedVar(result);
+ wxPuts (wxEmptyString);
+ }
+#endif // wxLongLong_t
+
+ wxPrintf (_T("printf (\"%%hhu\", %u) = %hhu\n"), UCHAR_MAX + 2, UCHAR_MAX + 2);
+ wxPrintf (_T("printf (\"%%hu\", %u) = %hu\n"), USHRT_MAX + 2, USHRT_MAX + 2);
+
+ wxPuts (_T("--- Should be no further output. ---"));
+ rfg1 ();
+ rfg2 ();
+
+#if 0
+ {
+ wxChar bytes[7];
+ wxChar buf[20];
+
+ memset (bytes, '\xff', sizeof bytes);
+ wxSprintf (buf, _T("foo%hhn\n"), &bytes[3]);
+ if (bytes[0] != '\xff' || bytes[1] != '\xff' || bytes[2] != '\xff'
+ || bytes[4] != '\xff' || bytes[5] != '\xff' || bytes[6] != '\xff')
+ {
+ wxPuts (_T("%hhn overwrite more bytes"));
+ result = 1;
+ }
+ if (bytes[3] != 3)
+ {
+ wxPuts (_T("%hhn wrote incorrect value"));
+ result = 1;
+ }
+ }
+#endif
+}
+
+static void
+rfg1 (void)
+{
+ wxChar buf[100];
+
+ wxSprintf (buf, _T("%5.s"), _T("xyz"));
+ if (wxStrcmp (buf, _T(" ")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" "));
+ wxSprintf (buf, _T("%5.f"), 33.3);
+ if (wxStrcmp (buf, _T(" 33")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 33"));
+ wxSprintf (buf, _T("%8.e"), 33.3e7);
+ if (wxStrcmp (buf, _T(" 3e+08")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3e+08"));
+ wxSprintf (buf, _T("%8.E"), 33.3e7);
+ if (wxStrcmp (buf, _T(" 3E+08")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3E+08"));
+ wxSprintf (buf, _T("%.g"), 33.3);
+ if (wxStrcmp (buf, _T("3e+01")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3e+01"));
+ wxSprintf (buf, _T("%.G"), 33.3);
+ if (wxStrcmp (buf, _T("3E+01")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3E+01"));
+}
+
+static void
+rfg2 (void)
+{
+ int prec;
+ wxChar buf[100];
+ wxString test_format;
+
+ prec = 0;
+ wxSprintf (buf, _T("%.*g"), prec, 3.3);
+ if (wxStrcmp (buf, _T("3")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
+ prec = 0;
+ wxSprintf (buf, _T("%.*G"), prec, 3.3);
+ if (wxStrcmp (buf, _T("3")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T("3"));
+ prec = 0;
+ wxSprintf (buf, _T("%7.*G"), prec, 3.33);
+ if (wxStrcmp (buf, _T(" 3")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 3"));
+ prec = 3;
+ test_format = _T("%04.*o");
+ wxSprintf (buf, test_format.c_str(), prec, 33);
+ if (wxStrcmp (buf, _T(" 041")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 041"));
+ prec = 7;
+ test_format = _T("%09.*u");
+ wxSprintf (buf, test_format.c_str(), prec, 33);
+ if (wxStrcmp (buf, _T(" 0000033")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 0000033"));
+ prec = 3;
+ test_format = _T("%04.*x");
+ wxSprintf (buf, test_format.c_str(), prec, 33);
+ if (wxStrcmp (buf, _T(" 021")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
+ prec = 3;
+ test_format = _T("%04.*X");
+ wxSprintf (buf, test_format.c_str(), prec, 33);
+ if (wxStrcmp (buf, _T(" 021")) != 0)
+ wxPrintf (_T("got: '%s', expected: '%s'\n"), buf, _T(" 021"));
+}
+
+#endif // TEST_PRINTF
+
+// ----------------------------------------------------------------------------
+// registry and related stuff
+// ----------------------------------------------------------------------------
+
+// this is for MSW only
+#ifndef __WXMSW__
+ #undef TEST_REGCONF
+ #undef TEST_REGISTRY
+#endif
+
+#ifdef TEST_REGCONF
+
+#include "wx/confbase.h"
+#include "wx/msw/regconf.h"
+
+#if 0
+static void TestRegConfWrite()
+{
+ wxConfig *config = new wxConfig(_T("myapp"));
+ config->SetPath(_T("/group1"));
+ config->Write(_T("entry1"), _T("foo"));
+ config->SetPath(_T("/group2"));
+ config->Write(_T("entry1"), _T("bar"));
+}
+#endif
+
+static void TestRegConfRead()
+{
+ wxConfig *config = new wxConfig(_T("myapp"));
+
+ wxString str;
+ long dummy;
+ config->SetPath(_T("/"));
+ wxPuts(_T("Enumerating / subgroups:"));
+ bool bCont = config->GetFirstGroup(str, dummy);
+ while(bCont)
+ {
+ wxPuts(str);
+ bCont = config->GetNextGroup(str, dummy);
+ }
+}
+
+#endif // TEST_REGCONF
+
+#ifdef TEST_REGISTRY
+
+#include "wx/msw/registry.h"
+
+// I chose this one because I liked its name, but it probably only exists under
+// NT
+static const wxChar *TESTKEY =
+ _T("HKEY_LOCAL_MACHINE\\SYSTEM\\ControlSet001\\Control\\CrashControl");
+
+static void TestRegistryRead()
+{
+ wxPuts(_T("*** testing registry reading ***"));
+
+ wxRegKey key(TESTKEY);
+ wxPrintf(_T("The test key name is '%s'.\n"), key.GetName().c_str());
+ if ( !key.Open() )
+ {
+ wxPuts(_T("ERROR: test key can't be opened, aborting test."));
+
+ return;
+ }
+
+ size_t nSubKeys, nValues;
+ if ( key.GetKeyInfo(&nSubKeys, NULL, &nValues, NULL) )
+ {
+ wxPrintf(_T("It has %u subkeys and %u values.\n"), nSubKeys, nValues);
+ }
+
+ wxPrintf(_T("Enumerating values:\n"));
+
+ long dummy;
+ wxString value;
+ bool cont = key.GetFirstValue(value, dummy);
+ while ( cont )
+ {
+ wxPrintf(_T("Value '%s': type "), value.c_str());
+ switch ( key.GetValueType(value) )
+ {
+ case wxRegKey::Type_None: wxPrintf(_T("ERROR (none)")); break;
+ case wxRegKey::Type_String: wxPrintf(_T("SZ")); break;
+ case wxRegKey::Type_Expand_String: wxPrintf(_T("EXPAND_SZ")); break;
+ case wxRegKey::Type_Binary: wxPrintf(_T("BINARY")); break;
+ case wxRegKey::Type_Dword: wxPrintf(_T("DWORD")); break;
+ case wxRegKey::Type_Multi_String: wxPrintf(_T("MULTI_SZ")); break;
+ default: wxPrintf(_T("other (unknown)")); break;
+ }
+
+ wxPrintf(_T(", value = "));
+ if ( key.IsNumericValue(value) )
+ {
+ long val;
+ key.QueryValue(value, &val);
+ wxPrintf(_T("%ld"), val);
+ }
+ else // string
+ {
+ wxString val;
+ key.QueryValue(value, val);
+ wxPrintf(_T("'%s'"), val.c_str());
+
+ key.QueryRawValue(value, val);
+ wxPrintf(_T(" (raw value '%s')"), val.c_str());
+ }
+
+ wxPutchar('\n');
+
+ cont = key.GetNextValue(value, dummy);
+ }
+}
+
+static void TestRegistryAssociation()
+{
+ /*
+ The second call to deleteself genertaes an error message, with a
+ messagebox saying .flo is crucial to system operation, while the .ddf
+ call also fails, but with no error message
+ */
+
+ wxRegKey key;
+
+ key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
+ key.Create();
+ key = _T("ddxf_auto_file") ;
+ key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
+ key.Create();
+ key = _T("ddxf_auto_file") ;
+ key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
+ key.Create();
+ key = _T("program,0") ;
+ key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
+ key.Create();
+ key = _T("program \"%1\"") ;
+
+ key.SetName(_T("HKEY_CLASSES_ROOT\\.ddf") );
+ key.DeleteSelf();
+ key.SetName(_T("HKEY_CLASSES_ROOT\\.flo") );
+ key.DeleteSelf();
+ key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\DefaultIcon"));
+ key.DeleteSelf();
+ key.SetName(_T("HKEY_CLASSES_ROOT\\ddxf_auto_file\\shell\\open\\command"));
+ key.DeleteSelf();
+}
+
+#endif // TEST_REGISTRY
+
+// ----------------------------------------------------------------------------
+// scope guard
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_SCOPEGUARD
+
+#include "wx/scopeguard.h"
+
+static void function0() { puts("function0()"); }
+static void function1(int n) { printf("function1(%d)\n", n); }
+static void function2(double x, char c) { printf("function2(%g, %c)\n", x, c); }
+
+struct Object
+{
+ void method0() { printf("method0()\n"); }
+ void method1(int n) { printf("method1(%d)\n", n); }
+ void method2(double x, char c) { printf("method2(%g, %c)\n", x, c); }
+};
+
+static void TestScopeGuard()
+{
+ wxON_BLOCK_EXIT0(function0);
+ wxON_BLOCK_EXIT1(function1, 17);
+ 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');
+
+ wxScopeGuard dismissed = wxMakeGuard(function0);
+ dismissed.Dismiss();
+}
+
+#endif
+
+// ----------------------------------------------------------------------------
+// sockets
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_SOCKETS
+
+#include "wx/socket.h"
+#include "wx/protocol/protocol.h"
+#include "wx/protocol/http.h"
+
+static void TestSocketServer()
+{
+ wxPuts(_T("*** Testing wxSocketServer ***\n"));
+
+ static const int PORT = 3000;
+
+ wxIPV4address addr;
+ addr.Service(PORT);
+
+ wxSocketServer *server = new wxSocketServer(addr);
+ if ( !server->Ok() )
+ {
+ wxPuts(_T("ERROR: failed to bind"));
+
+ return;
+ }
+
+ bool quit = false;
+ while ( !quit )
+ {
+ wxPrintf(_T("Server: waiting for connection on port %d...\n"), PORT);
+
+ wxSocketBase *socket = server->Accept();
+ if ( !socket )
+ {
+ wxPuts(_T("ERROR: wxSocketServer::Accept() failed."));
+ break;
+ }
+
+ wxPuts(_T("Server: got a client."));
+
+ server->SetTimeout(60); // 1 min
+
+ bool close = false;
+ while ( !close && socket->IsConnected() )
+ {
+ wxString s;
+ wxChar ch = _T('\0');
+ for ( ;; )
+ {
+ if ( socket->Read(&ch, sizeof(ch)).Error() )
+ {
+ // don't log error if the client just close the connection
+ if ( socket->IsConnected() )
+ {
+ wxPuts(_T("ERROR: in wxSocket::Read."));
+ }
+
+ break;
+ }
+
+ if ( ch == '\r' )
+ continue;
+
+ if ( ch == '\n' )
+ break;
+
+ s += ch;
+ }
+
+ if ( ch != '\n' )
+ {
+ break;
+ }
+
+ wxPrintf(_T("Server: got '%s'.\n"), s.c_str());
+ if ( s == _T("close") )
+ {
+ wxPuts(_T("Closing connection"));
+
+ close = true;
+ }
+ else if ( s == _T("quit") )
+ {
+ close =
+ quit = true;
+
+ 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());
+ }
+ }
+
+ if ( !close )
+ {
+ wxPuts(_T("Server: lost a client unexpectedly."));
+ }
+
+ socket->Destroy();
+ }
+
+ // same as "delete server" but is consistent with GUI programs
+ server->Destroy();
+}
+
+static void TestSocketClient()
+{
+ wxPuts(_T("*** Testing wxSocketClient ***\n"));
+
+ static const wxChar *hostname = _T("www.wxwidgets.org");
+
+ wxIPV4address addr;
+ addr.Hostname(hostname);
+ addr.Service(80);
+
+ wxPrintf(_T("--- Attempting to connect to %s:80...\n"), hostname);
+
+ wxSocketClient client;
+ if ( !client.Connect(addr) )
+ {
+ wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
+ }
+ else
+ {
+ wxPrintf(_T("--- Connected to %s:%u...\n"),
+ addr.Hostname().c_str(), addr.Service());
+
+ wxChar buf[8192];
+
+ // could use simply "GET" here I suppose
+ wxString cmdGet =
+ wxString::Format(_T("GET http://%s/\r\n"), hostname);
+ client.Write(cmdGet, cmdGet.length());
+ wxPrintf(_T("--- Sent command '%s' to the server\n"),
+ MakePrintable(cmdGet).c_str());
+ client.Read(buf, WXSIZEOF(buf));
+ wxPrintf(_T("--- Server replied:\n%s"), buf);
+ }
+}
+
+#endif // TEST_SOCKETS
+
+// ----------------------------------------------------------------------------
+// FTP
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_FTP
+
+#include "wx/protocol/ftp.h"
+
+static wxFTP ftp;
+
+#define FTP_ANONYMOUS
+
+#ifdef FTP_ANONYMOUS
+ static const wxChar *directory = _T("/pub");
+ static const wxChar *filename = _T("welcome.msg");
+#else
+ static const wxChar *directory = _T("/etc");
+ static const wxChar *filename = _T("issue");
+#endif
+
+static bool TestFtpConnect()
+{
+ wxPuts(_T("*** Testing FTP connect ***"));
+
+#ifdef FTP_ANONYMOUS
+ static const wxChar *hostname = _T("ftp.wxwidgets.org");
+
+ wxPrintf(_T("--- Attempting to connect to %s:21 anonymously...\n"), hostname);
+#else // !FTP_ANONYMOUS
+ static const wxChar *hostname = "localhost";
+
+ wxChar user[256];
+ wxFgets(user, WXSIZEOF(user), stdin);
+ user[wxStrlen(user) - 1] = '\0'; // chop off '\n'
+ ftp.SetUser(user);
+
+ wxChar password[256];
+ wxPrintf(_T("Password for %s: "), password);
+ wxFgets(password, WXSIZEOF(password), stdin);
+ password[wxStrlen(password) - 1] = '\0'; // chop off '\n'
+ ftp.SetPassword(password);
+
+ wxPrintf(_T("--- Attempting to connect to %s:21 as %s...\n"), hostname, user);
+#endif // FTP_ANONYMOUS/!FTP_ANONYMOUS
+
+ if ( !ftp.Connect(hostname) )
+ {
+ wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
+
+ return false;
+ }
+ else
+ {
+ wxPrintf(_T("--- Connected to %s, current directory is '%s'\n"),
+ hostname, ftp.Pwd().c_str());
+ ftp.Close();
+ }
+
+ return true;
+}
+
+// test (fixed?) wxFTP bug with wu-ftpd >= 2.6.0?
+static void TestFtpWuFtpd()
+{
+ wxFTP ftp;
+ static const wxChar *hostname = _T("ftp.eudora.com");
+ if ( !ftp.Connect(hostname) )
+ {
+ wxPrintf(_T("ERROR: failed to connect to %s\n"), hostname);
+ }
+ else
+ {
+ static const wxChar *filename = _T("eudora/pubs/draft-gellens-submit-09.txt");
+ wxInputStream *in = ftp.GetInputStream(filename);
+ if ( !in )
+ {
+ wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
+ }
+ else
+ {
+ size_t size = in->GetSize();
+ wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
+
+ wxChar *data = new wxChar[size];
+ if ( !in->Read(data, size) )
+ {
+ wxPuts(_T("ERROR: read error"));
+ }
+ else
+ {
+ wxPrintf(_T("Successfully retrieved the file.\n"));
+ }
+
+ delete [] data;
+ delete in;
+ }
+ }
+}
+
+static void TestFtpList()
+{
+ wxPuts(_T("*** Testing wxFTP file listing ***\n"));
+
+ // test CWD
+ if ( !ftp.ChDir(directory) )
+ {
+ wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
+ }
+
+ wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
+
+ // test NLIST and LIST
+ wxArrayString files;
+ if ( !ftp.GetFilesList(files) )
+ {
+ wxPuts(_T("ERROR: failed to get NLIST of files"));
+ }
+ else
+ {
+ wxPrintf(_T("Brief list of files under '%s':\n"), ftp.Pwd().c_str());
+ size_t count = files.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ wxPrintf(_T("\t%s\n"), files[n].c_str());
+ }
+ wxPuts(_T("End of the file list"));
+ }
+
+ if ( !ftp.GetDirList(files) )
+ {
+ wxPuts(_T("ERROR: failed to get LIST of files"));
+ }
+ else
+ {
+ wxPrintf(_T("Detailed list of files under '%s':\n"), ftp.Pwd().c_str());
+ size_t count = files.GetCount();
+ for ( size_t n = 0; n < count; n++ )
+ {
+ wxPrintf(_T("\t%s\n"), files[n].c_str());
+ }
+ wxPuts(_T("End of the file list"));
+ }
+
+ if ( !ftp.ChDir(_T("..")) )
+ {
+ wxPuts(_T("ERROR: failed to cd to .."));
+ }
+
+ wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
+}
+
+static void TestFtpDownload()
+{
+ wxPuts(_T("*** Testing wxFTP download ***\n"));
+
+ // test RETR
+ wxInputStream *in = ftp.GetInputStream(filename);
+ if ( !in )
+ {
+ wxPrintf(_T("ERROR: couldn't get input stream for %s\n"), filename);
+ }
+ else
+ {
+ size_t size = in->GetSize();
+ wxPrintf(_T("Reading file %s (%u bytes)..."), filename, size);
+ fflush(stdout);
+
+ wxChar *data = new wxChar[size];
+ if ( !in->Read(data, size) )
+ {
+ wxPuts(_T("ERROR: read error"));
+ }
+ else
+ {
+ wxPrintf(_T("\nContents of %s:\n%s\n"), filename, data);
+ }
+
+ delete [] data;
+ delete in;
+ }
+}
+
+static void TestFtpFileSize()
+{
+ wxPuts(_T("*** Testing FTP SIZE command ***"));
+
+ if ( !ftp.ChDir(directory) )
+ {
+ wxPrintf(_T("ERROR: failed to cd to %s\n"), directory);
+ }
+
+ wxPrintf(_T("Current directory is '%s'\n"), ftp.Pwd().c_str());
+
+ if ( ftp.FileExists(filename) )
+ {
+ int size = ftp.GetFileSize(filename);
+ if ( size == -1 )
+ wxPrintf(_T("ERROR: couldn't get size of '%s'\n"), filename);
+ else
+ wxPrintf(_T("Size of '%s' is %d bytes.\n"), filename, size);
+ }
+ else
+ {
+ wxPrintf(_T("ERROR: '%s' doesn't exist\n"), filename);
+ }
+}
+
+static void TestFtpMisc()
+{
+ wxPuts(_T("*** Testing miscellaneous wxFTP functions ***"));
+
+ if ( ftp.SendCommand(_T("STAT")) != '2' )
+ {
+ wxPuts(_T("ERROR: STAT failed"));
+ }
+ else
+ {
+ wxPrintf(_T("STAT returned:\n\n%s\n"), ftp.GetLastResult().c_str());
+ }
+
+ if ( ftp.SendCommand(_T("HELP SITE")) != '2' )
+ {
+ wxPuts(_T("ERROR: HELP SITE failed"));
+ }
+ else
+ {
+ wxPrintf(_T("The list of site-specific commands:\n\n%s\n"),
+ ftp.GetLastResult().c_str());
+ }
+}
+
+static void TestFtpInteractive()
+{
+ wxPuts(_T("\n*** Interactive wxFTP test ***"));
+
+ wxChar buf[128];
+
+ for ( ;; )
+ {
+ wxPrintf(_T("Enter FTP command: "));
+ if ( !wxFgets(buf, WXSIZEOF(buf), stdin) )
+ break;
+
+ // kill the last '\n'
+ buf[wxStrlen(buf) - 1] = 0;
+
+ // special handling of LIST and NLST as they require data connection
+ wxString start(buf, 4);
+ start.MakeUpper();
+ if ( start == _T("LIST") || start == _T("NLST") )
+ {
+ wxString wildcard;
+ if ( wxStrlen(buf) > 4 )
+ wildcard = buf + 5;
+
+ wxArrayString files;
+ if ( !ftp.GetList(files, wildcard, start == _T("LIST")) )
+ {
+ wxPrintf(_T("ERROR: failed to get %s of files\n"), start.c_str());
+ }
+ else
+ {
+ wxPrintf(_T("--- %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++ )
+ {
+ wxPrintf(_T("\t%s\n"), files[n].c_str());
+ }
+ wxPuts(_T("--- End of the file list"));
+ }
+ }
+ else // !list
+ {
+ wxChar ch = ftp.SendCommand(buf);
+ wxPrintf(_T("Command %s"), ch ? _T("succeeded") : _T("failed"));
+ if ( ch )
+ {
+ wxPrintf(_T(" (return code %c)"), ch);
+ }
+
+ wxPrintf(_T(", server reply:\n%s\n\n"), ftp.GetLastResult().c_str());
+ }
+ }
+
+ wxPuts(_T("\n*** done ***"));
+}
+
+static void TestFtpUpload()
+{
+ wxPuts(_T("*** Testing wxFTP uploading ***\n"));
+
+ // upload a file
+ static const wxChar *file1 = _T("test1");
+ static const wxChar *file2 = _T("test2");
+ wxOutputStream *out = ftp.GetOutputStream(file1);
+ if ( out )
+ {
+ wxPrintf(_T("--- Uploading to %s ---\n"), file1);
+ out->Write("First hello", 11);
+ delete out;
+ }
+
+ // send a command to check the remote file
+ if ( ftp.SendCommand(wxString(_T("STAT ")) + file1) != '2' )
+ {
+ wxPrintf(_T("ERROR: STAT %s failed\n"), file1);
+ }
+ else
+ {
+ wxPrintf(_T("STAT %s returned:\n\n%s\n"),
+ file1, ftp.GetLastResult().c_str());
+ }
+
+ out = ftp.GetOutputStream(file2);
+ if ( out )
+ {
+ wxPrintf(_T("--- Uploading to %s ---\n"), file1);
+ out->Write("Second hello", 12);
+ delete out;
+ }
+}
+
+#endif // TEST_FTP
+
+// ----------------------------------------------------------------------------
+// stack backtrace
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_STACKWALKER
+
+#if wxUSE_STACKWALKER
+
+#include "wx/stackwalk.h"
+
+class StackDump : public wxStackWalker
+{
+public:
+ StackDump(const char *argv0)
+ : wxStackWalker(argv0)
+ {
+ }
+
+ virtual void Walk(size_t skip = 1)
+ {
+ wxPuts(_T("Stack dump:"));
+
+ wxStackWalker::Walk(skip);
+ }
+
+protected:
+ virtual void OnStackFrame(const wxStackFrame& frame)
+ {
+ printf("[%2d] ", frame.GetLevel());
+
+ wxString name = frame.GetName();
+ if ( !name.empty() )
+ {
+ printf("%-20.40s", name.mb_str());
+ }
+ else
+ {
+ printf("0x%08lx", (unsigned long)frame.GetAddress());
+ }
+
+ if ( frame.HasSourceLocation() )
+ {
+ printf("\t%s:%d",
+ frame.GetFileName().mb_str(),
+ frame.GetLine());
+ }
+
+ puts("");
+
+ 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());
+ }
+ }
+};
+
+static void TestStackWalk(const char *argv0)
+{
+ wxPuts(_T("*** Testing wxStackWalker ***\n"));
+
+ StackDump dump(argv0);
+ dump.Walk();
+}
+
+#endif // wxUSE_STACKWALKER
+
+#endif // TEST_STACKWALKER
+
+// ----------------------------------------------------------------------------
+// standard paths
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_STDPATHS
+
+#include "wx/stdpaths.h"
+
+static void TestStandardPaths()
+{
+ wxPuts(_T("*** Testing wxStandardPaths ***\n"));
+
+ wxTheApp->SetAppName(_T("console"));
+
+ wxStandardPathsBase& stdp = wxStandardPaths::Get();
+ wxPrintf(_T("Config dir (sys):\t%s\n"), stdp.GetConfigDir().c_str());
+ wxPrintf(_T("Config dir (user):\t%s\n"), stdp.GetUserConfigDir().c_str());
+ wxPrintf(_T("Data dir (sys):\t\t%s\n"), stdp.GetDataDir().c_str());
+ 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("Plugins dir:\t\t%s\n"), stdp.GetPluginsDir().c_str());
+}
+
+#endif // TEST_STDPATHS
+
+// ----------------------------------------------------------------------------
+// streams
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_STREAMS
+
+#include "wx/wfstream.h"
+#include "wx/mstream.h"
+
+static void TestFileStream()
+{
+ wxPuts(_T("*** Testing wxFileInputStream ***"));
+
+ static const wxString filename = _T("testdata.fs");
+ {
+ wxFileOutputStream fsOut(filename);
+ fsOut.Write("foo", 3);
+ }
+
+ wxFileInputStream fsIn(filename);
+ wxPrintf(_T("File stream size: %u\n"), fsIn.GetSize());
+ while ( !fsIn.Eof() )
+ {
+ wxPutchar(fsIn.GetC());
+ }
+
+ if ( !wxRemoveFile(filename) )
+ {
+ wxPrintf(_T("ERROR: failed to remove the file '%s'.\n"), filename.c_str());
+ }
+
+ wxPuts(_T("\n*** wxFileInputStream test done ***"));
+}
+
+static void TestMemoryStream()
+{
+ wxPuts(_T("*** Testing wxMemoryOutputStream ***"));
+
+ wxMemoryOutputStream memOutStream;
+ wxPrintf(_T("Initially out stream offset: %lu\n"),
+ (unsigned long)memOutStream.TellO());
+
+ for ( const wxChar *p = _T("Hello, stream!"); *p; p++ )
+ {
+ memOutStream.PutC(*p);
+ }
+
+ wxPrintf(_T("Final out stream offset: %lu\n"),
+ (unsigned long)memOutStream.TellO());
+
+ wxPuts(_T("*** Testing wxMemoryInputStream ***"));
+
+ wxChar buf[1024];
+ size_t len = memOutStream.CopyTo(buf, WXSIZEOF(buf));
+
+ wxMemoryInputStream memInpStream(buf, len);
+ wxPrintf(_T("Memory stream size: %u\n"), memInpStream.GetSize());
+ while ( !memInpStream.Eof() )
+ {
+ wxPutchar(memInpStream.GetC());
+ }
+
+ wxPuts(_T("\n*** wxMemoryInputStream test done ***"));
+}
+
+#endif // TEST_STREAMS
+
+// ----------------------------------------------------------------------------
+// timers
+// ----------------------------------------------------------------------------
+
+#ifdef TEST_TIMER
+
+#include "wx/stopwatch.h"
+#include "wx/utils.h"
+
+static void TestStopWatch()
+{
+ wxPuts(_T("*** Testing wxStopWatch ***\n"));
+
+ wxStopWatch sw;
+ sw.Pause();
+ wxPrintf(_T("Initially paused, after 2 seconds time is..."));
+ fflush(stdout);
+ wxSleep(2);
+ wxPrintf(_T("\t%ldms\n"), sw.Time());
+
+ wxPrintf(_T("Resuming stopwatch and sleeping 3 seconds..."));
+ fflush(stdout);
+ sw.Resume();
+ wxSleep(3);
+ wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
+
+ sw.Pause();
+ wxPrintf(_T("Pausing agan and sleeping 2 more seconds..."));
+ fflush(stdout);
+ wxSleep(2);
+ wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
+
+ sw.Resume();
+ wxPrintf(_T("Finally resuming and sleeping 2 more seconds..."));
+ fflush(stdout);
+ wxSleep(2);
+ wxPrintf(_T("\telapsed time: %ldms\n"), sw.Time());
+
+ wxStopWatch sw2;
+ wxPuts(_T("\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 )
+ {
+ wxPuts(_T("\ntime is negative - ERROR!"));
+ }
+ }
+
+ wxPutchar('.');
+ fflush(stdout);
+ }
+
+ wxPuts(_T(", 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 )
+ {
+ wxPrintf(_T("%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 )
+ wxPrintf(_T(" = %s"), value.c_str());
+ wxPutchar('\n');