samedi 12 août 2017

Empty string (char *) received from codecvt

ALL,

I have a following code:

In the header

class MySQLDatabase
{
protected:
    struct MySQLImpl;
    MySQLImpl *m_pimpl;
};

struct MySQLDatabase::MySQLImpl
{
    std::wstring_convert<std::codecvt_utf8<wchar_t> > m_myconv;
    std::wstring m_host, m_user, m_password, m_dbName, m_socket, m_catalog;
};

In the cpp

int MySQLDatabase::Connect()
{
            std::wstring tname;
// some code here
            const char *temp1 = m_pimpl->m_myconv.to_bytes( tname.c_str() ).c_str();
            const char *temp2 = m_pimpl->m_myconv.to_bytes( ownerName.c_str() ).c_str();
            const char *temp3 = m_pimpl->m_myconv.to_bytes( fieldName.c_str() ).c_str();
            unsigned long str_length1 = strlen( temp1 ), str_length2 = strlen( temp2 ), str_length3 = strlen( temp3 );
            str_data1 = new char[str_length1 + 1], str_data2 = new char[str_length2 + 1], str_data3 = new char[str_length3 + 1];
            memset( str_data1, '\0', str_length1 + 1 );
            memset( str_data2, '\0', str_length2 + 1 );
            memset( str_data3, '\0', str_length3 + 1 );
// some more code here
}

I am trying to compile and run this code on Linux with gcc 5.4 in c++11 mode. The tname variable contains L"draft.abc\303\237".

The problem is that the temp1 is "" (empty string). And so is temp2 and temp3 (ownerName is L"" and fieldName is L"name").

Am I doing something wrong with the conversion?

Aucun commentaire:

Enregistrer un commentaire