📄 xcnvcttowc.c
字号:
#define Return(result) { \ *ct_bytes = ctcnt; \ *scanned_len = wcnt; \ if (ctcnt < limit) \ *ct_str = 0; \ if (error > 0) return(error); \ return(result); \ }#define SaveStore(c) { \ if (ctcnt >= limit) Return(BadBuffer); \ *ct_str++ = c; \ ctcnt++; \ }#define AppendDesignation(state) { \ int len = strlen(state); \ if ((ctcnt + len) > limit) \ Return(BadBuffer); \ (void) strcpy((char *)ct_str, state); \ ct_str += len; \ ctcnt += len; \ } int_XConvertWCToCT(xlocale, wc_str, wc_len, ct_str, ct_bytes, scanned_len) XLocale xlocale; wchar *wc_str; int wc_len; unsigned char *ct_str; int *ct_bytes; int *scanned_len;{ char *esc, seq[MAXSEQUENCELENGTH]; wchar woffset, wc; int wcnt, ctcnt, crwcnt, crctcnt, limit; int ret, error = 0; ISOStateInfo stateinfo; _State state_sv, state_ext; unsigned char *ct_str_sv = NULL; int len_sv; char *defstr = XDefaultString(); if (!xlocale) xlocale = _XFallBackConvert(); _Xctinit(xlocale); state_sv = xlocale->ct_state; limit = *ct_bytes; wcnt = ctcnt = 0; while ((wc_len > 0) && ((wc = *wc_str) != WNULL)) { _CSID ctGLorGR; /* * filter control characters. */ if (_Xiswcntrl(wc)) { SaveStore(_Xwctoa(wc)); wc_str++, wc_len--, wcnt++; continue; } _XcwGetAll(xlocale, wc, &esc, &woffset, &stateinfo); if (woffset == 0) { int i; /* XXX BUG: need to check/add designate sequence of default string. But current default string is NULL, so OK. :-) */ for (i = 0; *(defstr + i) != 0; i++) { SaveStore(*(defstr + i)) } error++; wc_str++, wc_len--, wcnt++; continue; } if ((ctGLorGR = ctGetGLorGR(xlocale)) == GL && ctGetGLid(xlocale) != (state_sv & 0xff) || ctGLorGR == GR && ctGetGRid(xlocale) != ((state_sv >> 8) & 0xff)) { state_ext = state_sv; state_sv = xlocale->ct_state; /* * append designation of control sequence. */ if (*(esc+1) == 0x25) { ct_str_sv = ct_str + 4; len_sv = strlen(esc) - 6; } AppendDesignation(esc); } /* * remainning buffer length of ct_str. */ crctcnt = limit - ctcnt; if ((ret = _XwcDecomposeGlyphCharset(xlocale, wc_str, wc_len, ct_str, &crctcnt, &crwcnt, (int *)NULL)) < 0) Return(ret); if (ct_str_sv) { *ct_str_sv++ = (crctcnt + len_sv) / 128 + 128; *ct_str_sv = (crctcnt + len_sv) % 128 +128; ct_str_sv = NULL; xlocale->ct_state = state_ext; state_sv = state_ext; } error += ret; wc_str += crwcnt, wc_len -= crwcnt, wcnt += crwcnt; ct_str += crctcnt, ctcnt += crctcnt; } if (!_XcwCheckDefaultState(xlocale)) { (void) _XcwGetDefaultEncoding(xlocale, seq); AppendDesignation(seq); _Xctinit(xlocale); } Return(Success);}int_XctIsExtendSegment(xlocale, ct_str, textlen, bytes)XLocale xlocale;unsigned char *ct_str;int *textlen;int *bytes;{ unsigned char *text; int seqlen; unsigned char name[128]; _CSID csid; int m, l; if(*(ct_str+1) != 0x25 || *(ct_str+2) != 0x2f) return (0); /* Not CT Extend Segment */ *bytes = *(ct_str+3) -'0'; if (*bytes < 0 || *bytes > 4) return (0); /* Not CT Extend Segment */ else if (*bytes == 0) /* I'm not sure. Valiable octes pre char cannot convert */ *bytes = 1; text = (unsigned char *)index((char *)ct_str+6, 0x02) + 1; m = *(ct_str+4); l = *(ct_str+5); seqlen = text - ct_str; strncpy((char *)name, (char *)ct_str+6, seqlen - 6); name[seqlen - 7] =0; csid = _XcwNameGetGLorGRId(name, *(text+1) & 0x80); ctSetGLorGR(xlocale, *(text+1) & 0x80); ctSetid(xlocale, csid); *textlen = (m-128)*128+(l-128) + 5 - seqlen + 1; /* !!!!! */ return(seqlen);}#undef Return#undef SaveStore#undef CombineCode#define Return(result) { \ *wc_bytes = wccnt; \ *scanned_bytes = ctcnt; \ if (wccnt < limit) \ *wc_str = 0; \ if (error > 0) return (error); \ return (result); \ }#define SaveStore(wc) { \ if (wccnt >= limit) Return(BadBuffer); \ *wc_str++ = wc; \ wccnt++; \ }/* * CombineCode(): * concatenate the byte with code if the byte is valid.*/#define CombineCode(code) { \ code = (code << 8) | byte; \ ct_str++; ct_bytes--; \ if (ct_bytes < 1) \ Return(BadTerminate); \ byte = *ct_str; \ }static int_XConvertCTextToWC(xlocale, ct_str, ct_bytes, wc_str, wc_bytes, scanned_bytes) XLocale xlocale; unsigned char *ct_str; int ct_bytes; wchar *wc_str; int *wc_bytes; int *scanned_bytes;{ register unsigned char byte; int wccnt, ctcnt; int code, len, limit, error; int textlen; int bytes; _State state_sv; wchar woffset; state_sv = xlocale->ct_state; limit = *wc_bytes; wccnt = ctcnt = error = 0; if((len = _XctIsExtendSegment(xlocale, ct_str, &textlen, &bytes)) < 0) /* not register encoding by X. */ Return(BadEncoding); ct_str += len; ctcnt += len; ct_bytes -=len; if (ct_bytes < textlen) Return(BadTerminate); /* Nor enough data. What should do? */ _XcwIdGetWoffset(xlocale, &woffset); while (textlen > 0) { /* get codepoint of character. */ code = 0; byte = *ct_str; switch (bytes) { case 4: CombineCode(code); case 3: CombineCode(code); case 2: CombineCode(code); case 1: code = (code << 8) | byte; ct_str++, ct_bytes--; code &= 0x7f7f7f7f; /* MSB off */ break; } SaveStore(woffset | code); textlen -= bytes; ctcnt += bytes; } xlocale->ct_state = state_sv; Return(Success);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -