Porting to OS/2: Case #13 iconv()

iconv() is used to convert one charset  to the other charset. OS/2 kLIBC supports iconv() natively using Uni*() APIs of OS/2. By the way, it has some limitations which Uni*() APIs have. That is, kLIBC iconv() does not support charsets which is not supported by Uni*() APIs. For examples, Uni*() APIs does not support UTF charsets except UTF-8. And some extensions are not supported. For examples, //TRANSLIT and //IGNORE.

So if you use kLIBC iconv() with some projects, iconv() may fail due to these. To workaround these, you may use a wrapper function like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#if defined(__KLIBC__) && !defined(iconv_open)
/* kLIBC iconv_open() does not support UTF-16LE and //TRANSLIT */
static iconv_t os2_iconv_open (const char *tocode, const char *fromcode)
{
    char to[strlen(tocode) + 1];
    char from[strlen(fromcode) + 1];
    char *p;

    strcpy(to, tocode);
    strcpy(from, fromcode);

    if (!strncmp(to, "UTF-16", 6))
    {
        strcpy(to, "UCS-2");
        memmove(to + 5, to + 6, strlen(to + 6));
    }

    p = strstr(to, "//");
    if (p)
        *p = '\0';

    if (!strncmp(from, "UTF-16", 6))
    {
        strcpy(from, "UCS-2");
        memmove(from + 5, from + 6, strlen(from + 6));
    }

    p = strstr(from, "//");
    if (p)
        *p = '\0';

    return iconv_open(to, from);
}

#define iconv_open(t, f) os2_iconv_open(t, f)
#endif


This workaround is used by lame port. It changes UTF-16 to UCS-2 and ignores any extensions. Of course, you should include all the relevant headers.

At line 1, checking if iconv_open was defined is to confirm iconv_open is from libiconv not kLIBC.

For those who want to use full-featured iconv() not a workarounded iconv(), there is GNU libiconv. And it is already ported to OS/2. Visit here.


If you use one of them, the above workaround is not needed. However, you have to modify your settings to use it. Let's assume that it is installed into /usr. Then prepend /usr/include to C_INCLUDE_PATH and CPLUS_INCLUDE_PATH. And prepend /usr/lib to LIBRARY_PATH. Then you can use libiconv version of iconv() instead of kLIBC version.
 

댓글

이 블로그의 인기 게시물

토렌트: < 왕좌의 게임 > 시즌 1 ~ 시즌 8 완결편 마그넷

토렌트: NGC < 코스모스 > 우리말 더빙 전편(1편~13편) 마그넷

토렌트: < 스타워즈 > Ep.1 ~ Ep.6 마그넷