/* * yamamori@kt.rim.or.jp */ #include #include extern char *_Xsetlocale(int, const char *); char * setlocale() { static int flag = 0; if (!flag) { flag = 1; MB_CUR_MAX = 2; _Xsetlocale(0, ""); XtSetLanguageProc(NULL, NULL, NULL); } return _Xsetlocale(0, NULL); } int mbtowc(wchar_t *pwc, const char *s, size_t n) { int r; wchar_t wc; if (s == NULL) { return 0; } if (s[0] == '\0') { wc = 0; r = 0; } else if ((s[0] & 0x80) && n >= 2) { wc = 0x30000000 | ((s[0]<<7)&0x3f80) | (s[1]&0x7f); r = 2; } else { wc = (s[0]&0xff); r = 1; } if (pwc != NULL) { *pwc = wc; } return r; } size_t mbstowcs(wchar_t *pwcs, const char *s, size_t n) { int r, i; for (r = 0; r < n; r++) { if ((i = mbtowc(&pwcs[r], s, 2)) < 0) { return i; } if (pwcs[r] == 0) { break; } s += i; } return r; } int mblen(const char *s, size_t n) { return mbtowc(NULL, s, n); } int wctomb(char *s, wchar_t wc) { if (s == NULL) { return 0; } if (wc < 0x100) { s[0] = wc; return 1; } s[0] = (0x80|((wc>>7)&0x7f)); s[1] = (0x80|(wc&0x7f)); return 2; } size_t wcstombs(char *s, const wchar_t *pwcs, size_t n) { int r, i; for (r = 0; r < n;) { i = wctomb(&s[r], *pwcs); if (s[r] == '\0') { break; } pwcs++; r += i; } return r; }