ÔÚFreeBSDÏ£¬¶Ô¶à×Ö½ÚÓïÑÔ±àÂëµÄ´¦ÀíÊÇͨ¹ý¡°¶à×Ö½Ú×Ö·û£¨mb£©Óë¿í×Ö½Ú×Ö·û£¨wc£©µÄ»¥Ïàת»»¡±À´Ìṩ֧³ÖµÄ¡£×÷ΪϵͳÄÚ²¿£¬FreebsdÔÚlibcº¯Êý¼¶±ðÉ϶ԡ°¿í×Ö½Úwc¡°ÌṩÁ˺ܺõÄÖ§³Ö£¬ËùÓпí×Ö½Ú´¦Àíº¯Êý¾ù¶¨ÒåÔÚ¡°/usr/src/include/wchar.h¡°ÖС£
FreeBSDÖжԶà×Ö½ÚÓïÑÔ±àÂë½øÐÐÖ§³ÖµÄ»ù±¾ÔÀíÊÇ£º
1¡¢ÌṩÁËͳһµÄ´¦Àí»úÖÆ£¬¾ÍÊÇ¿í×Ö½Ú´¦Àí£¬ÕâÑù¾Í±ÜÃâ³öÏÖ¡°°ë¸ö×Ö¡±µÄÎÊÌ⣬ËùÓеÄ×Ö·û¶¼ÊÇ×÷Ϊһ¸öµ¥¶ÀµÄ¶ÔÏ󣨿í×Ö½Ú×Ö·û£©À´´¦Àí¡£ÔÚfreebsdϵͳÖУ¬¿í×Ö½Ú×Ö·û¶¨ÒåΪ4¸ö×Ö½Ú£¬¿ÉÒÔÈÝÄÉUnicodeµÄËùÓÐ×Ö·û¡£ÔÚlibcÖУ¬FreebsdÌṩÁËÒÔϺ¯Êý£¬À´Ö§³Öwc<=>mb
mb -> wc
×Ö·û´¦Àí£º mbtowc mbrtowc
×Ö·û´®´¦Àí£º mbstowcs mbsrtowcs mbsnrtowcs
wc -> mb
×Ö·û´¦Àí£ºwctomb wcrtomb
×Ö·û´®´¦Àí£ºwcstombs wcsrtombs wcsnrtombs
ͨ¹ýÕâЩº¯Êý£¬¿ÉÒÔʵÏÖϵͳµ×²ã¶Ô¶à×Ö½ÚÓïÑÔ±àÂëµÄ´¦Àí£¬´óÖµÄÁ÷³ÌÊÇ£º
¶à×Ö½Ú×Ö·ûÊ×ÏÈҪת»»³É¿í×Ö½Ú×Ö·û£¬È»ºóÓÉϵͳÌṩµÄ¿í×Ö½Ú×Ö·û¡¢×Ö·û´®´¦Àíº¯Êý½øÐд¦Àí£¬ÖîÈçÅÅÐò¡¢±È½Ï¡¢Êä³ö¡£¸ù¾ÝÐèÒªÔÙ°Ñ´¦ÀíµÄ½á¹ûͨ¹ýwc -> mbµÄº¯Êý½øÐÐת»»£¬×ª»»³ÉÌض¨µÄ¶à×Ö·û±àÂë¸ñʽ¡£
2¡¢¶ÔÌض¨¶à×Ö·û±àÂë¸ñʽµÄ°ó¶¨£º
¶ÔÓÚºº×Ö´¦ÀíÀ´Ëµ£¬´æÔÚ¶àÖÖ±àÂë¸ñʽ£¬ÖîÈçUTF-8¡¢GB2312¡¢GBK¡¢GB18030µÈµÈ£¬ÓÉÓÚ±àÂ뷽ʽµÄ²»Í¬£¬ÔÚ½øÐÐwc<->mbת»»µÄʱºò£¬¾ÍÐèÒª²»Í¬µÄת»»¹æÔò¶ÔÆä½øÐÐÖ§³Ö¡£ÕâÖְ󶨴ÓÔÀíÉÏ˵ÊÇͨ¹ýlocaleµÄLC_CTYPEÀ´ÊµÏֵġ£
¾ßÌåµÄʵÏÖ£¬ÊÇÔÚ/usr/src/lib/libc/localeĿ¼Ï£¬ÔÀíÊǵ±Ó¦ÓóÌÐòµ÷ÓÃsetlocaleº¯ÊýÀ´É趨localeʱ£¬ÏµÍ³»á¶ÔLC_CTYPE½øÐмì²é£¬²¢¸ù¾ÝLC_CTYPEµÄÖµ£¬¶Ôwc<->mb´¦Àíº¯Êý½øÐÐÇл»¡£ÀýÈ磺
ÔĶÁmbrtowcº¯Êý£¨mbrtowc.c£©
size_t
mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
size_t n, mbstate_t * __restrict ps)
{
static mbstate_t mbs;
if (ps == NULL)
ps = &mbs;
return (__mbrtowc(pwc, s, n, ps));
}
Ëü×îÖÕµ÷ÓõÄÊÇ__mbrtowc(),__mbrtowcÊÇÒ»¸öº¯ÊýÖ¸Õ룬ÔÚsetrunelocale.cÖУ¬Õâ¸öÖ¸Õë»á¸ù¾Ýϵͳµ±Ç°µÄLC_CTYPE½øÐгõʼ»¯£¬Èç¹ûµ±Ç°µÄLC_CTYPE=zh_CN.GB18030£¬ÏµÍ³»áתÏòÖ´ÐÐGB18030.cÖеÄ_GB18030_init()º¯Êý£¬ÔÚÕâ¸öº¯ÊýÀïÃ棬Õâ¸öº¯ÊýÖ¸Õë»áÖ¸ÏòGB18030.cÖеÄ_GB18030_mbrtowc£¨£©º¯Êý£º
int
_GB18030_init(_RuneLocale *rl)
{
__mbrtowc = _GB18030_mbrtowc;
__wcrtomb = _GB18030_wcrtomb;
__mbsinit = _GB18030_mbsinit;
_CurrentRuneLocale = rl;
__mb_cur_max = 4;
return (0);
}
¶ø_GB18030_mbrtowc()º¯Êý¾ßÌåʵÏÖÁËGB18030±àÂëµÄ×Ö·ûµ½¿í×Ö½Ú×Ö·ûµÄת»»£º
static size_t
_GB18030_mbrtowc(wchar_t * __restrict pwc, const char * __restrict s,
size_t n, mbstate_t * __restrict ps)
{
_GB18030State *gs;
wchar_t wch;
int ch, len, ocount;
size_t ncopy;
gs = (_GB18030State *)ps;
if (gs->count < 0 || gs->count > sizeof(gs->bytes)) {
errno = EINVAL;
return ((size_t)-1);
}
if (s == NULL) {
s = "";
n = 1;
pwc = NULL;
}
ncopy = MIN(MIN(n, MB_CUR_MAX), sizeof(gs->bytes) - gs->count);
memcpy(gs->bytes + gs->count, s, ncopy);
ocount = gs->count;
gs->count += ncopy;
s = (char *)gs->bytes;
n = gs->count;
if (n == 0)
/* Incomplete multibyte sequence */
return ((size_t)-2);
/*
* Single byte: [00-7f]
* Two byte: [81-fe][40-7e,80-fe]
* Four byte: [81-fe][30-39][81-fe][30-39]
*/
ch = (unsigned char)*s++;
if (ch <= 0x7f) {
len = 1;
wch = ch;
} else if (ch >= 0x81 && ch <= 0xfe) {
wch = ch;
if (n < 2)
return ((size_t)-2);
ch = (unsigned char)*s++;
if ((ch >= 0x40 && ch <= 0x7e) || (ch >= 0x80 && ch <= 0xfe)) {
wch = (wch << 8) | ch;
len = 2;
} else if (ch >= 0x30 && ch <= 0x39) {
/*
* Strip high bit off the wide character we will
* eventually output so that it is positive when
* cast to wint_t on 32-bit twos-complement machines.
*/
wch = ((wch & 0x7f) << 8) | ch;
if (n < 3)
return ((size_t)-2);
ch = (unsigned char)*s++;
if (ch < 0x81 || ch > 0xfe)
goto ilseq;
wch = (wch << 8) | ch;
if (n < 4)
return ((size_t)-2);
ch = (unsigned char)*s++;
if (ch < 0x30 || ch > 0x39)
goto ilseq;
wch = (wch << 8) | ch;
len = 4;
} else
goto ilseq;
} else
goto ilseq;
if (pwc != NULL)
*pwc = wch;
gs->count = 0;
return (wch == L'\0' ? 0 : len - ocount);
ilseq:
errno = EILSEQ;
return ((size_t)-1);
}
FreebsdÕâÖÖÀûÓÃLocaleµÄ°ó¶¨»úÖÆ£¬¿ÉÒԷdz£Áé»îµÄ¶Ô¶àÖÖ¶à×Ö½Ú±àÂë×Ö·û´¦ÀíÌṩ֧³Ö£¬ÎÒÃÇ¿ÉÒÔ°ÑÈçÏÂÕ⼸¸öº¯ÊýÖ¸Õëµ±³ÉÒ»¸ö½Ó¿Ú£¬Èç¹ûҪʵÏÖ¶ÔijÖÖ¶à×Ö½Ú±àÂëµÄÖ§³Ö£¬Ö»ÒªÊµÏÖÕ⼸¸öº¯ÊýµÄʵÏÖ¼´¿É¡£ÕâÖÖ»úÖƺܺõÄÌåÏÖÁËUnixϵͳ¶ÔI18nµÄÖ§³Ö£¬ÏÂÃæÊÇUTF_8µÄ°ó¶¨£º
__mbrtowc = _UTF8_mbrtowc;
__wcrtomb = _UTF8_wcrtomb;
__mbsinit = _UTF8_mbsinit;
__mbsnrtowcs = _UTF8_mbsnrtowcs;
__wcsnrtombs = _UTF8_wcsnrtombs;
¾ßÌåµÄ´úÂ붼ÊÇÔÚ/usr/src/lib/lib/localeĿ¼ÏÂ
GB18030.c euc.c utf-8.c gb2312.cµÈµÈ£¬·Ö±ð¶Ô¼¸ÖÖºº×ֵıàÂë·½°¸Ìṩ֧³Ö¡£
Óɴ˿ɼû£¬Freebsdͨ¹ýlocale»úÖƶԺº×Ö´¦ÀíµÄÖ§³Ö£¬»¹ÊǺܳä·ÖµÄ£¬Ò²¿ÉÒÔ¿´³ö£¬Ö±½ÓÓ°ÏìFreebsd¶ÔÖÐÎÄ×Ö·û´¦ÀíµÄlocaleÊÇLC_CTYPE¡£