From 6274740de9539329e3d11866af09359fabc052cd Mon Sep 17 00:00:00 2001 From: Dick Hollenbeck Date: Thu, 5 Dec 2013 14:36:18 -0600 Subject: [PATCH] add a concept of an 8 bit string class for testing and experimentation. --- tools/UTF8.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 tools/UTF8.cpp diff --git a/tools/UTF8.cpp b/tools/UTF8.cpp new file mode 100644 index 0000000000..aaee8af8f4 --- /dev/null +++ b/tools/UTF8.cpp @@ -0,0 +1,73 @@ + +#include +#include +#include + + +/** + * Class UTF8 + * is an 8 bit std::string assuredly encoded in UTF8 that supplies special + * conversion support to and from wxString. + */ +class UTF8 : public std::string +{ + +public: + + UTF8( const wxString& o ) : + std::string( (const char*) o.utf8_str() ) + { + } + + UTF8( const char* txt ) : + std::string( txt ) + { + } + + UTF8( const std::string& o ) : + std::string( o ) + { + } + + UTF8() : + std::string() + { + } + + UTF8& operator = ( const wxString& o ) + { + std::string::operator=( (const char*) o.utf8_str() ); + } + + operator wxString () const + { + return wxString( c_str(), wxConvUTF8 ); + } +}; + + +void aFunctionTaking_wxString( const wxString& wx ) +{ + printf( "%s: '%s'\n", __func__, UTF8( wx ).c_str() ); +} + + +int main() +{ + UTF8 utf; + std::string str = "input"; + wxString wx = wxT( "input" ); + + utf = str; + + wxString wx2 = utf; + + UTF8 utf2 = wx2; + + printf( "here is some text:%s\n", utf2.c_str() ); + + // this is the key accomplishment here, passing a UTF8 to a function taking wxString: + aFunctionTaking_wxString( utf2 ); + + return 0; +}