Question

In porting a big app from a Windows to Linux, I need to be able to convert between wide characters and multibyte characters. To do this, I have code that looks like this:

void IConv(const InType* begin, const InType* end, const char* inCode, OutType* outBegin,  OutType*& outEnd, const char* outCode)
{
    assert(end >= begin);
    assert(outEnd > outBegin);

    iconv_t cd = iconv_open(outCode, inCode);
    if (cd == reinterpret_cast<iconv_t>(-1))
        throw (InvalidLocale ());
 /* blah, blah, blah other code we never reach */
 }

That code is always throwing an exception. To debug this, I created a simpler version that uses the same parameters as the code that fails. Here's my test code

int main( void )
{

    const char outCode[] = ""; 
    const char inCode[] = "wchar_t";

    //Using wchar_t and "" means that iconv will just use the system locale settings.
    iconv_t cd = iconv_open(outCode, inCode); 
    if (cd == reinterpret_cast<iconv_t>(-1))
    {
        printf("iconv failed to use outCode %s and inCode %s\n",outCode, inCode);
        return 1;
    }

    iconv_close(cd);
    return 0;
}

Notice that the code is pretty much the same. But in my test code I never see a failure, whereas the IConv function always fails. The locale on the system is set via the LANG env variable, which in this case is always ISO-8859-1.

So, the question is, does anyone know of any particular behavior in iconv that might present itself in a big app, but not in a simple case? Thank you

Was it helpful?

Solution

The problem is likely that the target machine doesn't have the appropriate iconv libraries and indexes installed. See /usr/lib[64]/gconv. The shared libraries are typically part of the glibc installation. Tools such as localedef can create them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top