📄 xcnvcttowc.c
字号:
/* * $XConsortium: XCnvCTToWC.c,v 1.22 91/11/17 16:15:29 rws Exp $ *//* * Copyright 1990, 1991 by OMRON Corporation, NTT Software Corporation, * and Nippon Telegraph and Telephone Corporation * Copyright 1991 by the Massachusetts Institute of Technology * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that * the above copyright notice appear in all copies and that both that * copyright notice and this permission notice appear in supporting * documentation, and that the names of OMRON, NTT Software, NTT, and M.I.T. * not be used in advertising or publicity pertaining to distribution of the * software without specific, written prior permission. OMRON, NTT Software, * NTT, and M.I.T. make no representations about the suitability of this * software for any purpose. It is provided "as is" without express or * implied warranty. * * OMRON, NTT SOFTWARE, NTT, AND M.I.T. DISCLAIM ALL WARRANTIES WITH REGARD * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY * AND FITNESS, IN NO EVENT SHALL OMRON, NTT SOFTWARE, NTT, OR M.I.T. BE * LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * * Authors: Li Yuhong OMRON Corporation * Hiroshi Kuribayashi OMRON Corporation * *//* * This files includes 2 functions: * * _XConvertCTToWC() * _XConvertWCToCT() * */#include "Xlibint.h"#include "Xlocaleint.h"#include <X11/Xos.h>#include <X11/Xutil.h>static int _XConvertCTextToWC();extern int _XParseISOEncoding();extern Bool _XcwEscSetStatus();extern void _XwcSetCsid();/* * Function Name: _XConvertCTToWC **//* * Constant Definitions*/#define HT 0x09 /* horizontal tab */#define NL 0x0A /* new line */#define ESC 0x1B /* escape */#define CSI 0x9B /* control sequence introducer. */#define SPACE 0x20 /* space */#define GL94MIN 0x21 /* manimun of code of GL 94 charset */ #define GL94MAX 0x7E /* maximun of code of GL 94 charset *//* * Macro Procedure Definitions*//* * Return(): * set result values before funciton returns. * If there is more room for output buffer wc_str, provide additional * service, i.e., append WNULL at tail.*/#define Return(result) { \ *wc_len = wcnt; \ *scanned_bytes = ctcnt; \ if (wcnt < limit) *wc_str = WNULL; \ if (state) *state = xlocale->ct_state; \ if (error > 0) return(error); \ return(result); \ }/* * SaveStore(): * store converted wchar code to buffer, and make sure no overflow.*/#define SaveStore(wc) { \ if (wcnt >= limit) \ Return(BadBuffer); \ *wc_str++ = wc; \ wcnt++; \ }/* * CombineCode(): * concatenate the byte with code if the byte is valid.*/#define CombineCode(code) { \ if (byte < stateinfo.code_min || byte > stateinfo.code_max) { \ error++; \ byte = stateinfo.code_min; \ } \ code = (code << 8) | byte; \ ct_str++, ct_bytes--; \ if (ct_bytes < 1) \ Return(BadTerminate); \ byte = *ct_str; \ }int_XConvertCTToWC(xlocale, ct_str, ct_bytes, wc_str, wc_len, scanned_bytes, state) XLocale xlocale; unsigned char *ct_str; int ct_bytes; wchar *wc_str; int *wc_len; int *scanned_bytes; _State *state;{ register unsigned char byte; register unsigned int code; wchar woffset; int wcnt, ctcnt, limit, len; ISOStateInfo stateinfo; char seq[MAXSEQUENCELENGTH]; int error = 0; if (!xlocale) xlocale = _XFallBackConvert(); if (state && *state != XDEFAULT_STATE) { xlocale->ct_state = *state; if (_XcwIdCheck(xlocale) == False) _Xctinit(xlocale); } else _Xctinit(xlocale); limit = *wc_len; wcnt = ctcnt = 0; while (ct_bytes > 0 && (byte = *ct_str) != 0) { switch (byte) { case HT: case NL: case SPACE: SaveStore(_Xatowc(byte)); ct_str++, ct_bytes--, ctcnt++; continue; case CSI: /* not supported yet */ case ESC: /* parse the control escape sequence of CT encoding. */ switch (_XParseISOEncoding(ct_str, ct_bytes, &len, &stateinfo)) { case Success: if (*(ct_str+1) == 0x25) { /* Extend segmant */ int tmplen = limit - wcnt; int ret; if((ret = _XConvertCTextToWC(xlocale, ct_str, ct_bytes, wc_str, &tmplen, &len)) < 0) Return(ret); wc_str += tmplen; wcnt +=tmplen; ct_str += len, ct_bytes -= len, ctcnt += len; continue; } /* * In mose case the control sequence is new one, so we * set it to current state directly, enhance little speed * without no comparision. * */ (void) strncpy(seq, (char *)ct_str, len); seq[len] = '\0'; if (_XcwEscSetStatus(xlocale, seq) == True) { ct_str += len, ct_bytes -= len; ctcnt += len; continue; } /* * Actually, it is not registered encoding, can not be * recognized by this convertor. go to next step. */ case BadEncoding: /* * wrong escape sequence, the function can not recover * this error, return it. */ Return(BadEncoding); case BadTerminate: Return(BadTerminate); default: /* never go to here */ Return(BadEncoding); ; } default: /* get codepoint of character. */ ctSetGLorGR(xlocale, byte&0x80); _XcwIdGetAll(xlocale, &woffset, &stateinfo); code = 0; switch (stateinfo.code_bytes) { case 4: CombineCode(code); case 3: CombineCode(code); case 2: CombineCode(code); case 1: if (byte < stateinfo.code_min || byte > stateinfo.code_max) { error++; byte = stateinfo.code_min; } code = (code << 8) | byte; ct_str++, ct_bytes--; code &= 0x7f7f7f7f; /* MSB off */ break; } SaveStore(woffset | code); /* * after no any error, then advance scanned_bytes * "ctcnt". */ ctcnt += stateinfo.code_bytes; continue; } } Return(Success);}/* * Function Name: _XConvertWCToCT **/#undef Return#undef SaveStore
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -