[C++-sig] Re: Automatic PyUnicode to 'const char*'
Stefan Seefeld
seefeld at sympatico.ca
Thu Jul 31 14:55:53 CEST 2003
David Abrahams wrote:
> "Lijun Qin" <qinlj at 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
More information about the Cplusplus-sig
mailing list