www.pudn.com > 智能内码识别,支持屏幕取词翻译的程序.zip > GETCODE.CPP
//各种内码输入法
#include "stdafx.h"
#include "cspublic.h"
#include "csinput.h"
#include "getcode.h"
static unsigned char c1 , c2 ; //记录翻页位置
int nPages , nPageStep ; //记录翻页次数和最后一次翻页的步长
//把16进制字符转换成10进制整型
int CharToInt( char c )
{
if( c>='0' && c<='9' ) //是数字
return( c-'0' ) ;
if( c>='a' && c<='f' ) //是16进制数
return( c-'a'+10 ) ;
if( c>='A' && c<='F' )
return( c-'A'+10 ) ;
return -1 ;
}
//-------------------------------------------------------------------------------------------------------//
//判断GB内码是否合法
BOOL JudgeGBCode( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1: //左边第一位
n =CharToInt( lpcsInput[0] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 2: //左边第二位
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( (n>=0xa1 && n<=0xa9) || (n>=0xb0 && n<=0xf7) )
return 1 ;
return 0 ;
case 3: //左边第三位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( !((n>=0xa1 && n<=0xa9) || (n>=0xb0 && n<=0xf7)) )
return 0 ;
n =CharToInt( lpcsInput[2] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 4: //左边第四位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( !((n>=0xa1 && n<=0xa9) || (n>=0xb0 && n<=0xf7)) )
return 0 ;
n =CharToInt( lpcsInput[2] )*16+CharToInt( lpcsInput[3] ) ;
if( n>=0xa1 && n<=0xfe )
return 1 ;
return 0 ;
}
return 0 ;
}
//根据c1,c2,得到GB码的下一个代码,c1,c2必须正确
BOOL GetNextGBCode( void )
{
if( c2==0xfe ) //c2已经到头
{
if( c1==0xf7 ) //c1也已经到头
return 0 ;
c2 =0xa1 ;
if( c1==0xa9 )
c1 =0xb0 ;
else
c1++ ;
}
else
c2++ ;
return 1 ;
}
//根据c1,c2,得到GB码的上一个代码,c1,c2必须正确
BOOL GetPriorGBCode( void )
{
if( c2==0xa1 ) //c2已经到头
{
if( c1==0xa1 ) //c1也已经到头
return 0 ;
c2 =0xfe ;
if( c1==0xb0 )
c1 =0xa9 ;
else
c1-- ;
}
else
c2-- ;
return 1 ;
}
//初始GB的c1,c2
void InitGBC1C2( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1:
n =CharToInt( lpcsInput[0] ) ;
if( n==0xa )
c1 =0xa1 ;
else
c1 =n*16 ;
c2 =0xa1 ;
break ;
case 2:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
c2 =0xa1 ;
break ;
case 3:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
n =CharToInt( lpcsInput[2] ) ;
if( n==0xa )
c2 =0xa1 ;
else
c2 =n*16 ;
break ;
}
}
//-----------------------------------------------------------------------------------------------//
//判断BIG5内码是否合法
BOOL JudgeBig5Code( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1: //左边第一位
n =CharToInt( lpcsInput[0] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 2: //左边第二位
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n>=0xa1 && n<=0xf9)
return 1 ;
return 0 ;
case 3: //左边第三位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0xa1 || n>0xf9)
return 0 ;
n =CharToInt( lpcsInput[2] ) ;
if( n>=0x4 && n<=0xf )
return 1 ;
return 0 ;
case 4: //左边第四位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0xa1 || n>0xf9)
return 0 ;
n =CharToInt( lpcsInput[2] )*16+CharToInt( lpcsInput[3] ) ;
if( n>=0x40 && n<=0xfe )
return 1 ;
return 0 ;
}
return 0 ;
}
//根据c1,c2,得到BIG5码的下一个代码,c1,c2必须正确
BOOL GetNextBig5Code( void )
{
if( c2==0xfe ) //c2已经到头
{
if( c1==0xf9 ) //c1也已经到头
return 0 ;
c2 =0x40 ;
c1++ ;
}
else
c2++ ;
return 1 ;
}
//根据c1,c2,得到BIG5码的上一个代码,c1,c2必须正确
BOOL GetPriorBig5Code( void )
{
if( c2==0x40 ) //c2已经到头
{
if( c1==0xa1 ) //c1也已经到头
return 0 ;
c2 =0xfe ;
c1-- ;
}
else
c2-- ;
return 1 ;
}
//初始BIG5的c1,c2
void InitBig5C1C2( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1:
n =CharToInt( lpcsInput[0] ) ;
if( n==0xa )
c1 =0xa1 ;
else
c1 =n*16 ;
c2 =0x40 ;
break ;
case 2:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
c2 =0x40 ;
break ;
case 3:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
n =CharToInt( lpcsInput[2] ) ;
c2 =n*16 ;
break ;
}
}
//-----------------------------------------------------------------------------------------//
//判断SHIFT-JIS内码是否合法
BOOL JudgeShiftJisCode( LPCSTR lpcsInput , int nLenOfInput )
{
int n , n1 ;
switch( nLenOfInput )
{
case 1: //左边第一位
n =CharToInt( lpcsInput[0] ) ;
if( n>=0x8 && n<=0xe )
return 1 ;
return 0 ;
case 2: //左边第二位
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n>=0x81 && n<=0xea)
return 1 ;
return 0 ;
case 3: //左边第三位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0x81 || n>0xea)
return 0 ;
n =CharToInt( lpcsInput[2] ) ;
if( n>=0x4 && n<=0xf )
return 1 ;
return 0 ;
case 4: //左边第四位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0x81 || n>0xea)
return 0 ;
n1 =CharToInt( lpcsInput[2] )*16+CharToInt( lpcsInput[3] ) ;
if( n==0xea )
{
if( n1!=0x7f && n1>=0x40 && n1<=0xa2 )
return 1 ;
return 0 ;
}
if( n1!=0x7f && n1>=0x40 && n1<=0xfc )
return 1 ;
return 0 ;
}
return 0 ;
}
//根据c1,c2,得到SHIFT-JIS码的下一个代码,c1,c2必须正确
BOOL GetNextShiftJisCode( void )
{
if( c2==0xfc ) //c2已经到头
{
if( c1==0xea ) //c1也已经到头
return 0 ;
c2 =0x40 ;
c1++ ;
}
else
{
if( c1==0xea && c2==0xa2 ) //已经到头
return 0 ;
c2++ ;
if( c2==0x7f )
c2++ ; //跳过0x7f
}
return 1 ;
}
//根据c1,c2,得到Shift-Jis码的上一个代码,c1,c2必须正确
BOOL GetPriorShiftJisCode( void )
{
if( c2==0x40 ) //c2已经到头
{
if( c1==0x81 ) //c1也已经到头
return 0 ;
c2 =0xfc ;
c1-- ;
}
else
c2-- ;
return 1 ;
}
//初始Shift-Jis的c1,c2
void InitShiftJisC1C2( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1:
n =CharToInt( lpcsInput[0] ) ;
if( n==0x8 )
c1 =0x81 ;
else
c1 =n*16 ;
c2 =0x40 ;
break ;
case 2:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
c2 =0x40 ;
break ;
case 3:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
n =CharToInt( lpcsInput[2] ) ;
c2 =n*16 ;
break ;
}
}
//------------------------------------------------------------------------------------------------//
//判断EUC-JIS内码是否合法
BOOL JudgeEucJisCode( LPCSTR lpcsInput , int nLenOfInput )
{
int n , n1 ;
switch( nLenOfInput )
{
case 1: //左边第一位
n =CharToInt( lpcsInput[0] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 2: //左边第二位
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n>=0xa1 && n<=0xf4 )
return 1 ;
return 0 ;
case 3: //左边第三位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0xa1 || n>0xf4 )
return 0 ;
n =CharToInt( lpcsInput[2] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 4: //左边第四位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0xa1 && n>0xf4 )
return 0 ;
n1 =CharToInt( lpcsInput[2] )*16+CharToInt( lpcsInput[3] ) ;
if( n==0xf4 )
{
if( n1>=0xa1 && n1<=0xa4 )
return 1 ;
return 0 ;
}
if( n>=0xa1 && n<=0xfe )
return 1 ;
return 0 ;
}
return 0 ;
}
//根据c1,c2,得到EUC-JIS码的下一个代码,c1,c2必须正确
BOOL GetNextEucJisCode( void )
{
if( c2==0xfe ) //c2已经到头
{
if( c1==0xf4 ) //c1也已经到头
return 0 ;
c2 =0xa1 ;
c1++ ;
}
else
{
if( c1==0xf4 && c2==0xa4 ) //已经到头
return 0 ;
c2++ ;
}
return 1 ;
}
//根据c1,c2,得到EUC-JIS码的上一个代码,c1,c2必须正确
BOOL GetPriorEucJisCode( void )
{
if( c2==0xa1 ) //c2已经到头
{
if( c1==0xa1 ) //c1也已经到头
return 0 ;
c2 =0xfe ;
c1-- ;
}
else
c2-- ;
return 1 ;
}
//初始EUC-JIS的c1,c2
void InitEucJisC1C2( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1:
n =CharToInt( lpcsInput[0] ) ;
if( n==0xa )
c1 =0xa1 ;
else
c1 =n*16 ;
c2 =0xa1 ;
break ;
case 2:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
c2 =0xa1 ;
break ;
case 3:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
n =CharToInt( lpcsInput[2] ) ;
if( n==0xa )
c2 =0xa1 ;
else
c2 =n*16 ;
break ;
}
}
//-----------------------------------------------------------------------------------------------//
//判断KSC5601内码是否合法
BOOL JudgeKsc5601Code( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1: //左边第一位
n =CharToInt( lpcsInput[0] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 2: //左边第二位
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n>=0xa1 && n<=0xfd)
return 1 ;
return 0 ;
case 3: //左边第三位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0xa1 || n>0xfd)
return 0 ;
n =CharToInt( lpcsInput[2] ) ;
if( n>=0xa && n<=0xf )
return 1 ;
return 0 ;
case 4: //左边第四位
//先判断第一字节
n =CharToInt( lpcsInput[0] )*16+CharToInt( lpcsInput[1] ) ;
if( n<0xa1 || n>0xfd)
return 0 ;
n =CharToInt( lpcsInput[2] )*16+CharToInt( lpcsInput[3] ) ;
if( n>=0xa1 && n<=0xfe )
return 1 ;
return 0 ;
}
return 0 ;
}
//根据c1,c2,得到KSC5601码的下一个代码,c1,c2必须正确
BOOL GetNextKsc5601Code( void )
{
if( c2==0xfe ) //c2已经到头
{
if( c1==0xfd ) //c1也已经到头
return 0 ;
c2 =0xa1 ;
c1++ ;
}
else
c2++ ;
return 1 ;
}
//根据c1,c2,得到KSC5601码的上一个代码,c1,c2必须正确
BOOL GetPriorKsc5601Code( void )
{
if( c2==0xa1 ) //c2已经到头
{
if( c1==0xa1 ) //c1也已经到头
return 0 ;
c2 =0xfe ;
c1-- ;
}
else
c2-- ;
return 1 ;
}
//初始KSC5601的c1,c2
void InitKsc5601C1C2( LPCSTR lpcsInput , int nLenOfInput )
{
int n ;
switch( nLenOfInput )
{
case 1:
n =CharToInt( lpcsInput[0] ) ;
if( n==0xa )
c1 =0xa1 ;
else
c1 =n*16 ;
c2 =0xa1 ;
break ;
case 2:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
c2 =0xa1 ;
break ;
case 3:
c1 =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
n =CharToInt( lpcsInput[2] ) ;
if( n==0xa )
c2 =0xa1 ;
else
c2 =n*16 ;
break ;
}
}
//-----------------------------------------------------------------------------------------------//
//内码输入法向下翻页
//返回0:出错或者输入字符不合法
//返回1:只可以向下翻页
//返回2:只可以向上翻页
//返回3:可以向下也可以向上翻页
//返回4:不能进行翻页
int PageNext( LPCSTR lpcsInput , int nLenOfInput , //输入串和长度
LPSTR lpsOutput , LPINT lpnLenOfOutput ) //输出串和长度
{
int n , i , n1 ;
switch( nLenOfInput )
{
case 1: //只输入了一个字符,除了该字符都可以改变
n =CharToInt( lpcsInput[0] ) ; //得到已经输入的字符值
for( i=0 ; i<10 ; i++ )
{
lpsOutput[i*4] =i+'0' ;
//返回这个编码
lpsOutput[i*4+1] =c1 ;
lpsOutput[i*4+2] =c2 ;
if( i<9 )
lpsOutput[i*4+3] =' ' ;
//得到下一个编码
if( !GetNextCode() ) //已经没有下一个编码了
{
*lpnLenOfOutput =(i+1)*4-1 ;
nPages++ ;
nPageStep =i+1 ;
return 2 ; //只可以向前翻页
}
//判断下一个编码是否合法
if( (c1/16) != n ) //不合法
{
*lpnLenOfOutput =(i+1)*4-1 ;
nPages++ ;
nPageStep =i+1 ;
return 2 ; //只可以向前翻页
}
}
*lpnLenOfOutput =i*4-1 ;
nPages++ ;
nPageStep =i ;
return 3 ;
case 2: //只输入了两个字符,除了这两个字符都可以改变
n =c1 ; //不能改变第一个字节
for( i=0 ; i<10 ; i++ )
{
lpsOutput[i*4] =i+'0' ;
//返回这个编码
lpsOutput[i*4+1] =c1 ;
lpsOutput[i*4+2] =c2 ;
if( i<9 )
lpsOutput[i*4+3] =' ' ;
//得到下一个编码
if( !GetNextCode() ) //已经没有下一个编码了
{
*lpnLenOfOutput =(i+1)*4-1 ;
nPages++ ;
nPageStep =i+1 ;
return 2 ; //只可以向前翻页
}
//判断下一个编码是否合法
if( c1 != n ) //不合法
{
*lpnLenOfOutput =(i+1)*4-1 ;
nPages++ ;
nPageStep =i+1 ;
return 2 ; //只可以向前翻页
}
}
*lpnLenOfOutput =i*4-1 ;
nPages++ ;
nPageStep =i ;
return 3 ;
case 3: //只输入了三个字符,除了这三个字符都可以改变
n =c1 ; //不能改变第一个字节
n1 =CharToInt( lpcsInput[2] ) ; //不能改变第三位数
for( i=0 ; i<10 ; i++ )
{
lpsOutput[i*4] =i+'0' ;
//返回这个编码
lpsOutput[i*4+1] =c1 ;
lpsOutput[i*4+2] =c2 ;
if( i<9 )
lpsOutput[i*4+3] =' ' ;
//得到下一个编码
if( !GetNextCode() ) //已经没有下一个编码了
{
*lpnLenOfOutput =(i+1)*4-1 ;
nPages++ ;
nPageStep =i+1 ;
return 2 ; //只可以向前翻页
}
//判断下一个编码是否合法
if( c1 != n || (c2/16!=n1) ) //不合法
{
*lpnLenOfOutput =(i+1)*4-1 ;
nPages++ ;
nPageStep =i+1 ;
return 2 ; //只可以向前翻页
}
}
*lpnLenOfOutput =i*4-1 ;
nPages++ ;
nPageStep =i ;
return 3 ;
}
return 0 ;
}
//判断内码是否合法
BOOL JudgeCode( LPCSTR lpcsInput , int nLenOfInput )
{
switch( GetInputCode() ) //判断当前的输入内码
{
case 0: //GB
case 2: //HZ
return JudgeGBCode( lpcsInput , nLenOfInput ) ;
case 1: //BIG5
return JudgeBig5Code( lpcsInput , nLenOfInput ) ;
case 3: //SHIFT-JIS
return JudgeShiftJisCode( lpcsInput , nLenOfInput ) ;
case 4: //EUC-JIS
return JudgeEucJisCode( lpcsInput , nLenOfInput ) ;
case 5: //KSC5601
return JudgeKsc5601Code( lpcsInput , nLenOfInput ) ;
}
return 0 ;
}
//根据c1,c2,得到下一个代码,c1,c2必须正确
BOOL GetNextCode( void )
{
switch( GetInputCode() ) //判断当前的输入内码
{
case 0: //GB
case 2: //HZ
return GetNextGBCode() ;
case 1: //BIG5
return GetNextBig5Code() ;
case 3: //SHIFT-JIS
return GetNextShiftJisCode() ;
case 4: //EUC-JIS
return GetNextEucJisCode() ;
case 5: //KSC5601
return GetNextKsc5601Code() ;
}
return 0 ;
}
//根据c1,c2,得到前一个代码
BOOL GetPriorCode( void )
{
switch( GetInputCode() ) //判断当前的输入内码
{
case 0: //GB
case 2: //HZ
return GetPriorGBCode() ;
case 1: //BIG5
return GetPriorBig5Code() ;
case 3: //SHIFT-JIS
return GetPriorShiftJisCode() ;
case 4: //EUC-JIS
return GetPriorEucJisCode() ;
case 5: //KSC5601
return GetPriorKsc5601Code() ;
}
return 0 ;
}
//初始c1,c2
void InitC1C2( LPCSTR lpcsInput , int nLenOfInput )
{
switch( GetInputCode() ) //判断当前的输入内码
{
case 0: //GB
case 2: //HZ
InitGBC1C2( lpcsInput , nLenOfInput ) ;
return ;
case 1: //BIG5
InitBig5C1C2( lpcsInput , nLenOfInput ) ;
return ;
case 3: //SHIFT-JIS
InitShiftJisC1C2( lpcsInput , nLenOfInput ) ;
return ;
case 4: //EUC-JIS
InitEucJisC1C2( lpcsInput , nLenOfInput ) ;
return ;
case 5: //KSC5601
InitKsc5601C1C2( lpcsInput , nLenOfInput ) ;
return ;
}
}
//--------------------------------------------------------------------------------------------------------//
#ifdef __cplusplus
extern "C" {
#endif
//根据输入的英文内码,得到汉字
//nFlag=0,从头开始查,设置翻页指针
//nFlag=3,向下翻页,改变翻页指针
//nFlag=4,向上翻页
//返回0:出错或者输入字符不合法
//返回1:只可以向下翻页
//返回2:只可以向上翻页
//返回3:可以向下也可以向上翻页
//返回4:不能进行翻页
int __export FAR PASCAL GetHzOfCode(
LPCSTR lpcsInput , int nLenOfInput , //输入串和长度
LPSTR lpsOutput , LPINT lpnLenOfOutput , //输出串和长度
int nFlag )
{
if( nFlag==3 ) //向下翻页
return PageNext( lpcsInput , nLenOfInput , lpsOutput , lpnLenOfOutput ) ;
if( nFlag==4 ) //向上翻页
{
for( int i=0 ; i<10+nPageStep ; i++ ) //往前翻两页
GetPriorCode() ;
nPages -=2 ; //减少两页
//往下翻一页
PageNext( lpcsInput , nLenOfInput , lpsOutput , lpnLenOfOutput ) ;
if( !nPages ) //一页没翻
return 1 ; //只可以向下翻页
return 3 ; //可以向上也可以向下翻页
}
//剩余的就是从头开始查
//判断输入内码是否合法
if( !JudgeCode( lpcsInput , nLenOfInput ) ) //不合法
return 0 ;
if( nLenOfInput==4 ) //长度为4,直接返回
{
lpsOutput[0] ='1' ;
lpsOutput[1] =CharToInt( lpcsInput[0] )*16 + CharToInt( lpcsInput[1] ) ;
lpsOutput[2] =CharToInt( lpcsInput[2] )*16 + CharToInt( lpcsInput[3] ) ;
*lpnLenOfOutput =3 ;
return 1 ;
}
//确定初始的c1,c2
InitC1C2( lpcsInput , nLenOfInput ) ;
//赋值
for( int i=0 ; i<10 ; i++ )
{
lpsOutput[i*4] =i+'0' ;
lpsOutput[i*4+1] =c1 ;
lpsOutput[i*4+2] =c2++ ;
if( i<9 )
lpsOutput[i*4+3] =' ' ;
}
*lpnLenOfOutput =39 ;
nPages =0 ; //初始翻过的页
return 1 ;
}
#ifdef __cplusplus
}
#endif