ETOC.CPP
上传用户:zhang8947
上传日期:2007-01-08
资源大小:1910k
文件大小:6k
- //英汉翻译
- #include "stdafx.h"
- #include <windowsx.h>
- #include "etoc.h"
- CEtoC OEtoC ; //英汉翻译对象
- //读入库内容
- CEtoC::CEtoC( void )
- {
- hpsLib =NULL ;
- }
- //装入翻译字库
- BOOL CEtoC::LoadDict( void )
- {
- //打开文件
- if( !Open( DICT_NAME , CFile::modeRead ) )
- {
- ::MessageBox( 0 , "Open file error,in LoadDict!" , 0 , MB_OK ) ;
- return 0 ;
- }
- //得到库长度
- DWORD dwLibLen=GetLength() ;
- //分配空间
- hpsLib =(char huge*)GlobalAllocPtr( GMEM_FIXED ,
- dwLibLen ) ;
- if( !hpsLib )
- {
- ::MessageBox( 0 , "GlobalAllocPtr error" , 0 , MB_OK ) ;
- return 0 ;
- }
-
- //读出库内容
- ReadHuge( hpsLib , dwLibLen ) ;
- //关闭文件
- Close() ;
-
- lpLibHead =(LIB_HEAD far*)hpsLib ; //得到库头
- //判断文件是否正确
- if( dwLibLen != lpLibHead->dwLibLen ) //库有问题
- {
- ::MessageBox( 0 , "The etoc.lib is error" , 0 , MB_OK ) ;
- return 0 ;
- }
-
- return 1 ;
- }
- void CEtoC::UnloadDict( void )
- {
- if( hpsLib )
- {
- GlobalFreePtr( hpsLib ) ;
- hpsLib =NULL ;
- }
- }
-
- //英汉翻译,返回0,出错,1完全匹配,2非完全匹配
- int CEtoC::EnglishToChinese( LPCSTR lpcsEnglish ,
- int nLenOfEnglish ,
- LPSTR lpsChinese , int nLenOfChinese ,
- LPINT lpnLenOfResult )
- {
- char sBuff[300] ;
- if( (UINT)nLenOfEnglish <= 0 ||
- (UINT)nLenOfEnglish > lpLibHead->uWordClassNum ) //长度不对
- {
- strcpy( sBuff , "长度错误!" ) ;
- *lpnLenOfResult =min( nLenOfChinese , strlen( sBuff ) ) ;
- _fstrncpy( lpsChinese , sBuff ,
- *lpnLenOfResult ) ;
- return 0 ;
- }
-
- //检查该类是否有词
- if( lpLibHead->dwStartPos[nLenOfEnglish-1] == 0 ||
- lpLibHead->dwWordNum[nLenOfEnglish-1] == 0 )
- {
- strcpy( sBuff , "不好意思,我不会翻译这个词." ) ;
- *lpnLenOfResult =min( nLenOfChinese , strlen( sBuff ) ) ;
- _fstrncpy( lpsChinese , sBuff ,
- *lpnLenOfResult ) ;
- return 0 ;
- }
-
- //检索该英文单词的含义
- DWORD dwItemLen =nLenOfEnglish+5 ; //该词类的项长度
- DWORD dwFirst =0 ;
- DWORD dwLast =lpLibHead->dwWordNum[nLenOfEnglish-1] - 1 ;
- DWORD dwMiddle=0 ;
- DWORD dwMiddlePoint=0 ; //中间词的指针
- WORD_CLASS far *lpWord ;
- int n ;
- //以二分法进行检索
- while( dwFirst <= dwLast )
- {
- dwMiddle =( ( dwFirst+dwLast )/2 ) ;
- dwMiddlePoint =dwMiddle*dwItemLen ;
- n =_fstrnicmp( lpcsEnglish , hpsLib +
- lpLibHead->dwStartPos[nLenOfEnglish-1] +
- dwMiddlePoint , nLenOfEnglish ) ;
- if( !n ) //相等
- {
- lpWord =(WORD_CLASS far*)( hpsLib +
- lpLibHead->dwStartPos[nLenOfEnglish-1] +
- dwMiddlePoint + nLenOfEnglish ) ;
- _fstrncpy( lpsChinese , lpcsEnglish , nLenOfEnglish ) ;
- lpsChinese[nLenOfEnglish] =':' ;
- if( nLenOfEnglish+1+nLenOfChinese < lpWord->bLen ) //缓冲区长度太小
- {
- _fstrncpy( lpsChinese+nLenOfEnglish+1 , hpsLib + lpWord->dwStartPos ,
- nLenOfChinese-nLenOfEnglish-1 ) ;
- *lpnLenOfResult =nLenOfEnglish+1+nLenOfChinese ;
- lpsChinese[*lpnLenOfResult] =' ' ;
- return 0 ;
- }
- else
- {
- _fstrncpy( lpsChinese+nLenOfEnglish+1 , hpsLib + lpWord->dwStartPos ,
- lpWord->bLen ) ;
- *lpnLenOfResult =nLenOfEnglish+1+lpWord->bLen ;
- lpsChinese[*lpnLenOfResult] =' ' ;
- return 1 ;
- }
- }
-
- if( n<0 )
- {
- if( !dwMiddle ) //已经为0了
- break ;
- dwLast =dwMiddle-1 ;
- }
- else
- dwFirst =dwMiddle+1 ;
- }
-
- //没有匹配者,以中间值代替
- //组织输出结果
- char far *lp ;
- lp =(char far*)sBuff ;
- //给英文名字
- _fstrncpy( lp , hpsLib +
- lpLibHead->dwStartPos[nLenOfEnglish-1] +
- dwMiddlePoint , nLenOfEnglish ) ;
- lp +=nLenOfEnglish ;
- *lp++ =':' ;
- //给中文含义
- lpWord =(WORD_CLASS far*)( hpsLib +
- lpLibHead->dwStartPos[nLenOfEnglish-1] +
- dwMiddlePoint + nLenOfEnglish ) ;
- _fstrncpy( lp , hpsLib + lpWord->dwStartPos ,
- lpWord->bLen ) ;
- lp +=lpWord->bLen ;
- *lp =' ' ;
- *lpnLenOfResult =min( nLenOfChinese , strlen( sBuff ) ) ;
- _fstrncpy( lpsChinese , sBuff ,
- *lpnLenOfResult ) ;
- lpsChinese[*lpnLenOfResult] =' ' ;
- // WritePrivateProfileString( "test" , "s" , lpsChinese , "test.ini" ) ;
- return 2 ;
- }
- #ifdef __cplusplus
- extern "C" {
- #endif
- //装入翻译字库
- BOOL __export FAR PASCAL LoadDict( void )
- {
- return OEtoC.LoadDict() ;
- }
- //卸掉翻译字库
- void __export FAR PASCAL UnloadDict( void )
- {
- OEtoC.UnloadDict() ;
- }
- //对外输出的函数
- #define HOU_ZHUI_NUM 6 //后缀数目
- #define BUFF_LEN 100
- BOOL __export FAR PASCAL EnglishToChinese( LPCSTR lpcsEnglish ,
- int nLenOfEnglish ,
- LPSTR lpsChinese , int nLenOfChinese ,
- LPINT lpnLenOfResult )
- {
- char sDelStr[HOU_ZHUI_NUM][10]= //减去某部分
- { "s" , "es" , "ing" , "ing" , "ed" , "ied" } ;
- char sAddStr[HOU_ZHUI_NUM][10]= //减去后,再加上某个部分
- { "" , "" , "" , "e" , "" , "y" } ;
-
- int n =OEtoC.EnglishToChinese( lpcsEnglish , nLenOfEnglish ,
- lpsChinese , nLenOfChinese , lpnLenOfResult ) ;
- if( n==0 || n==1 )
- return (BOOL)n ;
- char sBuff[BUFF_LEN] ;
- int nLen , nDelStr ;
- for( int i=0 ; i<HOU_ZHUI_NUM ; i++ )
- {
- nDelStr =strlen( sDelStr[i] ) ;
- if( nLenOfEnglish<=nDelStr )
- continue ;
- //比较是否有减去的部分
- if( _fstrncmp( lpcsEnglish+nLenOfEnglish-nDelStr , sDelStr[i] , nDelStr ) )
- continue ; //不相等
- nLen =nLenOfEnglish-nDelStr+strlen(sAddStr[i]) ;
- if( nLen>BUFF_LEN )
- continue ;
- _fstrncpy( sBuff , lpcsEnglish , nLenOfEnglish-nDelStr ) ;
- strncpy( sBuff+nLenOfEnglish-nDelStr , sAddStr[i] , strlen(sAddStr[i]) ) ;
- n =OEtoC.EnglishToChinese( sBuff , nLen ,
- lpsChinese , nLenOfChinese , lpnLenOfResult ) ;
- if( n==0 || n==1 )
- return (BOOL)n ;
- }
- return OEtoC.EnglishToChinese( lpcsEnglish , nLenOfEnglish ,
- lpsChinese , nLenOfChinese , lpnLenOfResult ) ;
- }
- #ifdef __cplusplus
- }
- #endif