Automatic PyUnicode to 'const char*'
Hi all, I'm using boost.python to wrap the WTL (Windows Template Libaray), using VC 7.1 and porting some old code previously use win32ui.pyd. Basically it is easy to do, though gccxml failed to parse the ATL/WTL code so I can not use Pyste. But there is a trouble, does anybody know how to automaticly convert PyUnicode to 'const char *'? Without this, I have to change lot of code lines to explictly use str() function. Lijun Qin http://www.solidshare.com
"Lijun Qin" <qinlj@solidshare.com> writes:
Hi all,
I'm using boost.python to wrap the WTL (Windows Template Libaray), using VC 7.1 and porting some old code previously use win32ui.pyd. Basically it is easy to do, though gccxml failed to parse the ATL/WTL code so I can not use Pyste. But there is a trouble, does anybody know how to automaticly convert PyUnicode to 'const char *'? Without this, I have to change lot of code lines to explictly use str() function.
I'm not an expert in it, but I thought Unicode used 16- or 32- byte wchar_t characters. How would you convert it to char const*? -- Dave Abrahams Boost Consulting www.boost-consulting.com
David Abrahams wrote:
"Lijun Qin" <qinlj@solidshare.com> writes:
Hi all,
I'm using boost.python to wrap the WTL (Windows Template Libaray), using VC 7.1 and porting some old code previously use win32ui.pyd. Basically it is easy to do, though gccxml failed to parse the ATL/WTL code so I can not use Pyste. But there is a trouble, does anybody know how to automaticly convert PyUnicode to 'const char *'? Without this, I have to change lot of code lines to explictly use str() function.
I'm not an expert in it, but I thought Unicode used 16- or 32- byte wchar_t characters. How would you convert it to char const*?
unicode allows different encodings, some (such as utf-8) with variably sized character representations. This means that conversions usually can't be done on-the-fly without helper constructs, i.e. some memory management is needed. As an example, I'm doing such conversions in a xml library. The C implementation uses 'xmlChar *', which is just an alias for 'char *', but really holds utf-8 encoded text. I convert it to various string types (the public API is parametrized for the string type): Here is the conversion between xmlChar * and std::string: struct string_convert { static std::string in(const xmlChar *buffer) { return buffer ? std::string((char *)buffer) : std::string(); } static xmlChar *out(const std::string &buffer) { return (xmlChar *)buffer.c_str(); } }; And here is the conversion between xmlChar * and Qt's QString: struct QStringConvert { struct Converter { Converter(const QCString &s) : utf8(s) {} operator const xmlChar *() const { return (const xmlChar *)static_cast<const char *>(utf8);} QCString utf8; }; static QString in(const xmlChar *buffer) { return buffer ? QString::fromUtf8((const char *)buffer) : QString(); } static Converter out(const QString &buffer) { return Converter(buffer.utf8()); } }; Regards, Stefan
Stefan Seefeld <seefeld@sympatico.ca> writes:
David Abrahams wrote:
"Lijun Qin" <qinlj@solidshare.com> writes:
Hi all,
I'm using boost.python to wrap the WTL (Windows Template Libaray), using VC 7.1 and porting some old code previously use win32ui.pyd. Basically it is easy to do, though gccxml failed to parse the ATL/WTL code so I can not use Pyste. But there is a trouble, does anybody know how to automaticly convert PyUnicode to 'const char *'? Without this, I have to change lot of code lines to explictly use str() function. I'm not an expert in it, but I thought Unicode used 16- or 32- byte wchar_t characters. How would you convert it to char const*?
unicode allows different encodings, some (such as utf-8) with variably sized character representations. This means that conversions usually can't be done on-the-fly without helper constructs, i.e. some memory management is needed.
As an example, I'm doing such conversions in a xml library. The C implementation uses 'xmlChar *', which is just an alias for 'char *', but really holds utf-8 encoded text. I convert it to various string types (the public API is parametrized for the string type):
To do that kind of narrowing, at the moment, the only way would be to write a thin wrapper function: void f(char const*); void f_thin_wrapper(object unicode) { str narrowed(unicode); f(extract<char const*>(str)); } we may have some support for doing this automatically in the version of Boost.Python which integrates with Luabind, but that's a ways off yet. -- Dave Abrahams Boost Consulting www.boost-consulting.com
participants (3)
-
David Abrahams -
Lijun Qin -
Stefan Seefeld