// type differs because a function may either return pointer to the buffer
// directly or have to use intermediate buffer for translation.
#if wxUSE_UNICODE
- const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const
- { return conv.cWC2MB(c_str()); }
+ const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const;
const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
const wxWX2MBbuf mbc_str() const { return mb_str(); }
#if wxUSE_WCHAR_T
- const wxWCharBuffer wc_str(wxMBConv& conv) const
- { return conv.cMB2WC(c_str()); }
+ const wxWCharBuffer wc_str(wxMBConv& conv) const;
#endif // wxUSE_WCHAR_T
const wxChar* fn_str() const { return c_str(); }
#define wxCtime _tctime
#else /* !TCHAR-aware compilers */
+ #if __DARWIN__ && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 ) && !defined(__MWERKS__)
+ /* even though they are defined and "implemented", they are bad and just
+ stubs so we need our own - we need these even in ANSI builds!! */
+ #define mbstowcs wxInternalMbstowcs
+ #define wcstombs wxInternalWcstombs
+
+ WXDLLIMPEXP_BASE size_t wxInternalMbstowcs (wchar_t *, const char *, size_t);
+ WXDLLIMPEXP_BASE size_t wxInternalWcstombs (char *, const wchar_t *, size_t);
+ #endif
+
/* No UNICODE in the c library except wchar_t typedef on mac OSX 10.2 and less - roll our own */
#if wxUSE_UNICODE && __DARWIN__ && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 ) && !defined(__MWERKS__)
-
+
/* we need everything! */
#define wxNEED_WX_STRING_H
#define wxNEED_WX_CTYPE_H
-
+
#define wxFgetchar(c) wxFgetc(c, stdin)
#define wxFputc wxPutc
#define wxFputchar(c) wxPutc(c, stdout)
#define wxNEED_WX_STDIO_H
#define wxNEED_WX_STDLIB_H
#define wxNEED_WX_TIME_H
-
- /* even though they are defined and "implemented", they are bad and just
- stubs so we need our own */
- #define mbstowcs wxInternalMbstowcs
- #define wcstombs wxInternalWcstombs
-
- WXDLLIMPEXP_BASE size_t wxInternalMbstowcs (wchar_t *, const char *, size_t);
- WXDLLIMPEXP_BASE size_t wxInternalWcstombs (char *, const wchar_t *, size_t);
-
+
#elif wxUSE_UNICODE
#include <wctype.h>
//else: the conversion failed -- leave the string empty (what else?)
}
}
+}
+
+const wxCharBuffer wxString::mb_str(wxMBConv& conv) const
+{
+ const wxChar* szEnd = (*this).c_str() + length() + 1;
+ const wxChar* szPos = (*this).c_str();
+ const wxChar* szStart = szPos;
+
+ wxCharBuffer buffer(length() + 1);
+
+ //Convert the string until the length() is reached, continuing the
+ //loop every time a null character is reached
+ while(szPos != szEnd)
+ {
+ size_t nLen = conv.WC2MB(NULL, szPos, 0);
+
+ wxASSERT(nLen != (size_t)-1); //should not be true! If it is system wctomb could be bad
+
+ if ( conv.WC2MB(&buffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
+ {
+ //error - return empty buffer
+ wxFAIL_MSG(wxT("Error converting wide-character string to a multi-byte string"));
+ buffer.data()[0] = '\0';
+ return buffer;
+ }
+
+ szPos += nLen + 1;
+ }
+
+ return buffer;
}
#else // ANSI
// leave empty
}
+
+const wxWCharBuffer wxString::wc_str(wxMBConv& conv) const
+{
+ const wxChar* szEnd = (*this).c_str() + length() + 1;
+ const wxChar* szPos = (*this).c_str();
+ const wxChar* szStart = szPos;
+
+ wxWCharBuffer buffer(length() + 1);
+
+ //Convert the string until the length() is reached, continuing the
+ //loop every time a null character is reached
+ while(szPos != szEnd)
+ {
+ size_t nLen = conv.MB2WC(NULL, szPos, 0);
+
+ wxASSERT(nLen != (size_t)-1); //should not be true! If it is system mbtowc could be bad
+
+ if ( conv.MB2WC(&buffer.data()[szPos - szStart], szPos, nLen + 1) == (size_t)-1 )
+ {
+ //error - return empty buffer
+ wxFAIL_MSG(wxT("Error converting multi-byte string to a wide-character string"));
+ buffer.data()[0] = '\0';
+ return buffer;
+ }
+
+ szPos += nLen + 1;
+ }
+
+ return buffer;
+}
+
#endif // wxUSE_WCHAR_T
#endif // Unicode/ANSI
WXDLLEXPORT int wxToupper(wxChar ch) { return (wxChar)CharUpper((LPTSTR)(ch)); }
#endif
-#if wxUSE_UNICODE && __DARWIN__ && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
+#if __DARWIN__ && ( MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_2 )
WXDLLEXPORT size_t wxInternalMbstowcs (wchar_t * out, const char * in, size_t outlen)
{
#if wxUSE_WCHAR_T
CPPUNIT_TEST( ConstructorsWithConversion );
#endif
+ CPPUNIT_TEST( Conversion );
CPPUNIT_TEST( Extraction );
CPPUNIT_TEST( Find );
CPPUNIT_TEST( Tokenizer );
#if wxUSE_WCHAR_T
void ConstructorsWithConversion();
#endif
+ void Conversion();
void Extraction();
void Find();
void SingleTokenizerTest( wxChar *str, wxChar *delims, size_t count , wxStringTokenizerMode mode );
}
#endif
+void StringTestCase::Conversion()
+{
+#if wxUSE_UNICODE
+ wxString szTheString(wxT("TheString"));
+ szTheString.insert(3, 1, '\0');
+ wxCharBuffer theBuffer = szTheString.mb_str();
+
+ CPPUNIT_ASSERT( memcmp(theBuffer.data(), "The\0String", 11) == 0 );
+#else
+# if wxUSE_WCHAR_T
+ wxString szTheString(wxT("TheString"));
+ szTheString.insert(3, 1, '\0');
+ wxWCharBuffer theBuffer = szTheString.wc_str(wxConvLibc);
+
+ CPPUNIT_ASSERT( memcmp(theBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
+
+ wxString szLocalTheString(wxT("TheString"));
+ szLocalTheString.insert(3, 1, '\0');
+ wxWCharBuffer theLocalBuffer = szLocalTheString.wc_str(wxConvLocal);
+
+ CPPUNIT_ASSERT( memcmp(theLocalBuffer.data(), L"The\0String", 11 * sizeof(wchar_t)) == 0 );
+# endif
+#endif
+}
+
void StringTestCase::Extraction()
{
wxString s(_T("Hello, world!"));