📄 ltf_lib.c
字号:
/* *---------------------------------------------------------------------- * T-Kernel / Standard Extension * * Copyright (C) 2006 by Ken Sakamura. All rights reserved. * T-Kernel / Standard Extension is distributed * under the T-License for T-Kernel / Standard Extension. *---------------------------------------------------------------------- * * Version: 1.00.00 * Released by T-Engine Forum(http://www.t-engine.org) at 2006/8/11. * *---------------------------------------------------------------------- *//* * ltf_lib.c (seio) * * Standard input/output * tf library subsets */#include <basic.h>#include <libstr.h>#include <tcode.h>#include <extension/errno.h>#include <sys/util.h>#include "tf.h"#include "ltf_inner.h"#include "../taddef.h"LOCAL VOID vClrCtxStat( TF_CTX ctx);#ifdef DEBUG#define DP(x) printf x#else#define DP(x)#endifLOCAL UB pubNStr[] = { 0, 0};LOCAL UH puhZenFusen[] = { 0xffa2, 0x0006, 0x0300, 0x0000, 0x0000};LOCAL UH puhHanFusen[] = { 0xffa2, 0x0006, 0x0300, 0x0000, 0x0102}; /* TC-based size of character scale-up/down tags */#define N_FUSEN (W)(sizeof(puhZenFusen)/sizeof(puhZenFusen[0]))#define MAX_SLEN 0x7fffffff /* MAX Size of characters to be converted */#define SUB_SEG_SZ 2 /* Segment size */#define TSD_TTS_PUB_2 2#define TSD_TTS_VAL_2 2#define TSD_TTS_MSK_0XFF (TC)0xff#define TSD_TTS_VAL_0XFFFF (TC)0xffff#define TSD_TTS_PHF_0X0102 (TC)0x0102#define TSD_TTS_SFT_8 (TC)8#define TSD_TTS_SFT_16 (TC)16#define TSD_TOC_UBX_2 2EXPORT ER tf_open_ctx(TF_CTX *ctxp){ *ctxp = ( TF_CTX)Vmalloc(( sizeof( struct tf_ctx))); if( *ctxp != 0){ memset( *ctxp, 0, (size_t)sizeof( struct tf_ctx)); (*ctxp)->uhLang = TSC_SYS; (*ctxp)->ubX2TUndefChar = TSD_TOC_UBX_2; return 0; } return E_NOMEM;}EXPORT ER tf_close_ctx(TF_CTX ctx){ Vfree( ctx); return 0;}/* * Initialize TX_CTX processing state * Settings are not changed */LOCAL VOID vClrCtxStat( TF_CTX ctx){ memset( &ctx->srcs, 0, (size_t)sizeof( ctx->srcs)); ctx->wState = 0;}/* * Convert various character codes into TRON codes * Implement basic functions only */EXPORT WER tf_strtotcs(TF_CTX ctx, const UB *src, W slen, UW attr, TC *dst, W *dlenp){ W wErr = 0, wDstP = 0, wI, wJ, wWantLen, wLen, wZenHan; TC tcTmp; if(( ctx == NULL )||( ctx->chgfunc == NULL )||( dlenp == NULL)){ wErr = E_PAR; goto err_ret; } if( (attr & TF_ATTR_START) != 0U){ if( (attr & TF_ATTR_CONT) != 0U){ SET_TFCONT( ctx->wState); }else{ SET_TFNCONT( ctx->wState); } if( (attr & TF_ATTR_SUPPRESS_FUSEN) != 0U){ SET_TFSUPPR( ctx->wState); }else{ SET_TFNSUPPR( ctx->wState); } } if( src != 0){ if( (attr & TF_ATTR_CONT) != 0U){ SET_TFCONT( ctx->wState); }else{ SET_TFNCONT( ctx->wState); } } if( IS_TF_RUN( ctx) != 0){ if((( attr & TF_ATTR_START) != 0U) && src){ SET_TF_NOTRUN( ctx); goto tf_notrun; }else if( src != 0){ wErr = E_PAR; goto err_ret; } if( ctx->srcs.xs.wStoredTCLen != 0){ if( !dst){ wErr = E_PAR; goto err_ret; } for( wI = 0;(( wI < *dlenp )&&( wI < ctx->srcs.xs.wStoredTCLen)); wI++){ dst[ wI] = ctx->srcs.xs.ptcBuf[ ctx->srcs.xs.wStoredTCP + wI]; } wDstP = wI; ctx->srcs.xs.wStoredTCLen -= wI; ctx->srcs.xs.wStoredTCP += wI; if( ctx->srcs.xs.wStoredTCLen != 0){ *dlenp = wDstP; /* Conversion finished partially */ wErr = 1; goto err_ret; } } }else{ slen = ( slen < 0)? MAX_SLEN: slen;tf_notrun: if( (attr & TF_ATTR_START) != 0){ /* First call */ vClrCtxStat( ctx); if( (attr & TF_ATTR_CONT) != 0){ SET_TFCONT( ctx->wState); }else{ SET_TFNCONT( ctx->wState); } if( (attr & TF_ATTR_SUPPRESS_FUSEN) != 0){ SET_TFSUPPR( ctx->wState); }else{ SET_TFNSUPPR( ctx->wState); } if( src != 0){ if( (attr & TF_ATTR_CONT) != 0){ SET_TFCONT( ctx->wState); }else{ SET_TFNCONT( ctx->wState); } ctx->srcs.xs.pubCurSrc = ( UB *)src; ctx->srcs.xs.wSrcLen = slen; }else{ ctx->srcs.xs.pubCurSrc = pubNStr; ctx->srcs.xs.wSrcLen = 0; } }else{ /* Consecutive processing */ ctx->srcs.xs.wSrcP = 0; if( ctx->srcs.xs.wStoredLen != 0){ if( slen < ctx->srcs.xs.wWantLen){ wErr = E_PAR; goto err_ret; } if( src == NULL){ wErr = E_PAR; goto err_ret; } for( wI = 0; ((wI < ctx->srcs.xs.wWantLen )&&( src[ wI])); wI++){ ctx->srcs.xs.pubBuf[ ctx->srcs.xs.wStoredLen + wI] = src[ wI]; } if( wI < ctx->srcs.xs.wWantLen){ wErr = E_PAR; goto err_ret; } ctx->srcs.xs.wStoredLen += wI; ctx->srcs.xs.wWantLen = 0; src += wI; slen -= wI; ctx->srcs.xs.pubCurSrc = ctx->srcs.xs.pubBuf; ctx->srcs.xs.wSrcLen = ctx->srcs.xs.wStoredLen; }else{ ctx->srcs.xs.pubCurSrc = ( UB *)src; ctx->srcs.xs.wSrcLen = slen; } } } while( 1){ if(( ctx->isEnd( ctx)) || ( ctx->srcs.xs.wSrcLen <= ctx->srcs.xs.wSrcP)){ if( ctx->srcs.xs.pubCurSrc == ctx->srcs.xs.pubBuf){ ctx->srcs.xs.wStoredLen = 0; ctx->srcs.xs.pubCurSrc = ( UB *)src; ctx->srcs.xs.wSrcLen = slen; continue; } break; } wLen = ctx->chgfunc( ctx, &tcTmp, &( ctx->srcs.xs.pubCurSrc)[ ctx->srcs.xs.wSrcP], ctx->srcs.xs.wSrcLen - ctx->srcs.xs.wSrcP, &wZenHan, &wWantLen); if( wLen < 0){ if(( wLen == E_NOEXS) /* In the case of insufficient data */ && (IS_TFCONT( ctx->wState) != 0)){ for( wI = 0; wI < (ctx->srcs.xs.wSrcLen - ctx->srcs.xs.wSrcP); wI++){ ctx->srcs.xs.pubBuf[ wI] = ( ctx->srcs.xs.pubCurSrc)[ ctx->srcs.xs.wSrcP + wI]; } ctx->srcs.xs.wStoredLen = wI; ctx->srcs.xs.wWantLen = wWantLen; goto dir_ret; } wErr = E_PAR; goto err_ret; } if(IS_TFSUPPR( ctx->wState) == 0){ if( IS_TFZEN( ctx->wState)){ if( IS_TFHAN( wZenHan) != 0){ SET_TFHAN( ctx->wState); if( dst != 0){ for( wI = 0; ( wI < N_FUSEN) && ( wDstP < *dlenp); wI++){ dst[ wDstP++] = puhHanFusen[ wI]; } if( wI < N_FUSEN){ for( wJ = 0; (wI + wJ) < N_FUSEN; wJ++){ ctx->srcs.xs.ptcBuf[ wJ] = puhHanFusen[ wI + wJ]; } ctx->srcs.xs.wStoredTCLen = wJ; ctx->srcs.xs.wStoredTCP = 0; *dlenp = wDstP; /* Conversion finished partially */ SET_TF_RUN( ctx); wErr = 1; goto err_ret; } }else{ wDstP += N_FUSEN; } } }else{ if( IS_TFZEN( wZenHan)){ SET_TFZEN( ctx->wState); if( dst != 0){ for( wI = 0; ( wI < N_FUSEN) && ( wDstP < *dlenp); wI++){ dst[ wDstP++] = puhZenFusen[ wI]; } if( wI < N_FUSEN){ for( wJ = 0; (wI + wJ) < N_FUSEN; wJ++){ ctx->srcs.xs.ptcBuf[ wJ] = puhZenFusen[ wI + wJ]; } ctx->srcs.xs.wStoredTCLen = wJ; ctx->srcs.xs.wStoredTCP = 0; *dlenp = wDstP; /* Conversion finished partially */ SET_TF_RUN( ctx); wErr = 1; goto err_ret; } }else{ wDstP += N_FUSEN; } } } } if( dst != 0){ if( wDstP < *dlenp){ dst[ wDstP] = tcTmp; }else{ /* Conversion finished partially */ SET_TF_RUN( ctx); wErr = 1; goto err_ret; } } wDstP++; ctx->srcs.xs.wSrcP += wLen; } if(( IS_TFCONT( ctx->wState) == 0) && dst &&( wDstP < *dlenp)){ dst[ wDstP] = TNULL; }dir_ret: *dlenp = wDstP; SET_TF_NOTRUN( ctx); return 0;err_ret: return wErr;}/* * Convert TRON codes into various character codes * Implement basic functions only */EXPORT WER tf_tcstostr(TF_CTX ctx, const TC *src, W slen, TLANG lang, UW attr, UB *dst, W *dlenp){ W wErr, wLen, wDstP = 0, wI; UB pubX[TSD_TTS_PUB_2]; TC tcTC; if(( ctx == NULL )||( ctx->chgfunc == NULL )||( dlenp == NULL)){ wErr = E_PAR; goto err_ret; } if( (attr & ~(UW)( TF_ATTR_HAN | TF_ATTR_CONT | TF_ATTR_START | TF_ATTR_IGNORE_FUSEN)) != 0){ wErr = E_PAR; goto err_ret; } if( (attr & TF_ATTR_START) != 0){ if( (attr & TF_ATTR_HAN) != 0){ SET_TFHAN( ctx->wState); }else{ SET_TFZEN( ctx->wState); } if( (attr & TF_ATTR_IGNORE_FUSEN) != 0){ SET_TFIGNO( ctx->wState); }else{ SET_TFNIGNO( ctx->wState); } ctx->uhLang = TSC_SYS; } if( src != 0){ if( (attr & TF_ATTR_CONT) != 0){ SET_TFCONT( ctx->wState); }else{ SET_TFNCONT( ctx->wState); } } if( lang && src ){ ctx->uhLang = lang; }tf_restart: if( IS_TF_RUN( ctx) != 0){ if( (attr & TF_ATTR_START) != 0){ if( src != 0){ SET_TF_TC_TC( ctx); SET_TF_NOTRUN( ctx); goto tf_restart; } }else{ if( src != 0){ wErr = E_PAR; goto err_ret; } } }else{ if( (attr & TF_ATTR_START) != 0){ /* First call */ vClrCtxStat( ctx); if( (attr & TF_ATTR_HAN) != 0){ SET_TFHAN( ctx->wState); }else{ SET_TFZEN( ctx->wState); } if( (attr & TF_ATTR_IGNORE_FUSEN) != 0){ SET_TFIGNO( ctx->wState); }else{ SET_TFNIGNO( ctx->wState); } } if( src != 0){ if( (attr & TF_ATTR_CONT) != 0){ SET_TFCONT( ctx->wState); }else{ SET_TFNCONT( ctx->wState); } ctx->srcs.tcs.wSrcP = 0; ctx->srcs.tcs.ptcCurSrc = src; ctx->srcs.tcs.wSrcEnd = ( slen < 0)? MAX_SLEN: slen; }else{ ctx->srcs.tcs.ptcCurSrc = ( TC *)pubNStr; ctx->srcs.tcs.wSrcEnd = 0; } } while( ctx->srcs.tcs.wSrcP < ctx->srcs.tcs.wSrcEnd){ tcTC = ( ctx->srcs.tcs.ptcCurSrc)[ ctx->srcs.tcs.wSrcP]; switch( ctx->srcs.tcs.wTCState){ case TF_TC_TC: if( tcTC == TNULL){ goto loop_end; } if(( tcTC & TC_ESC) == TC_ESC){ ctx->srcs.tcs.tcEsc = tcTC; SET_TF_TC_NO_OR_LAR( ctx); ctx->srcs.tcs.wSrcP++; continue; } if(( tcTC & TC_LANG) == TC_LANG){ ctx->uhLang = (tcTC & TSD_TTS_MSK_0XFF); ctx->srcs.tcs.wSrcP++; continue; } wLen = ctx->chgfunc( ctx, pubX, tcTC, ctx->wState); if( wLen < 0){ wErr = E_PAR; goto err_ret; } if( dst != 0){ if( *dlenp < (wDstP + wLen)){ SET_TF_RUN( ctx); *dlenp = wDstP; wErr = 1; goto dir_ret; } for( wI = 0; wI < wLen; wI++){ dst[ wDstP + wI] = pubX[ wI]; } } ctx->srcs.tcs.wSrcP++; wDstP += wLen; break; case TF_TC_NO_OR_LAR: if( tcTC == TSD_TTS_VAL_0XFFFF){ ctx->srcs.tcs.wSrcP++; SET_TF_TC_LAR0( ctx); break; } SET_TF_TC_NORM( ctx); break; case TF_TC_LAR0: ctx->srcs.tcs.wSrcP++; ctx->srcs.tcs.wSegSize = (W)tcTC; SET_TF_TC_LAR1( ctx); break; case TF_TC_LAR1: ctx->srcs.tcs.wSrcP++; ctx->srcs.tcs.wSegSize = ((UW)ctx->srcs.tcs.wSegSize | (tcTC << TSD_TTS_SFT_16)); SET_TF_TC_SKIP( ctx); break; case TF_TC_SKIP: ctx->srcs.tcs.wSrcP++; ctx->srcs.tcs.wSegSize -= SUB_SEG_SZ; if( ctx->srcs.tcs.wSegSize <= 0){ SET_TF_TC_TC( ctx); } break; case TF_TC_NORM: ctx->srcs.tcs.wSrcP++; ctx->srcs.tcs.wSegSize = (W)tcTC; if( IS_TFIGNO( ctx->wState) != 0){ SET_TF_TC_SKIP( ctx); break; } if( ctx->srcs.tcs.tcEsc == ( TC_ESC | TS_TFONT)){ SET_TF_TC_SUBID( ctx); }else{ SET_TF_TC_SKIP( ctx); } break; case TF_TC_SUBID: ctx->srcs.tcs.wSrcP++;#if BIGENDIAN if( (tcTC & TSD_TTS_MSK_0XFF) == TT_RATIO_SID){#else if((( tcTC >> TSD_TTS_SFT_8 )& TSD_TTS_MSK_0XFF) == TT_RATIO_SID){#endif SET_TF_TC_RATIO( ctx); ctx->srcs.tcs.wRCnt = 0; break; } ctx->srcs.tcs.wSegSize -= TSD_TTS_VAL_2; SET_TF_TC_SKIP( ctx); break; case TF_TC_RATIO: ctx->srcs.tcs.wSrcP++; if( ctx->srcs.tcs.wRCnt == 1){ if( tcTC == TSD_TTS_PHF_0X0102){ SET_TFHAN( ctx->wState); }else{ SET_TFZEN( ctx->wState); } SET_TF_TC_TC( ctx); break; } ctx->srcs.tcs.wRCnt++; break; default: wErr = E_SYS; goto err_ret; } }loop_end: if( (IS_TFCONT( ctx->wState) == 0) && dst &&( wDstP < *dlenp )){ dst[ wDstP] = 0; } *dlenp = wDstP; SET_TF_NOTRUN( ctx); return 0;dir_ret:err_ret: return( wErr);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -