📄 conv_opt_gaiji.cpp
字号:
// conv_opt_gaiji.cpp
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include "msc.h"
#include "codepage.h"
#include "filename.h"
#include "stringex.h"
#include "chnconv.h"
#include "chnconv_cp.h"
#include "conv_opt_gaiji.h"
#include "conv_loaddll.h"
////////////////////////////////////////
// 奜帤僼傽僀儖 //
////////////////////////////////////////
static int ReadGaijiFileA ( int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) ;
static int ReadGaijiFileW ( int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) ;
static int ReadGaijiFileInternal ( FILE *Fin, int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) ;
// 婲摦帪偵奜帤掕媊僼傽僀儖傪撉傒崬傓
// 幐攕偟偨傜 -1 傪曉偡
// 惉岟偟偨傜 1 傪曉偡
// 僼傽僀儖偑懚嵼偟側偄応崌丄偁傞偄偼丄奜帤偑巜掕偝傟偰偄側偄応崌偼 0 傪曉偡
int ReadGaijiFile ( int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) {
if ( IsNT () ) return ReadGaijiFileW ( nSrcCodePage, nDstCodePage, pInfo, pErrorLineNumber ) ;
else return ReadGaijiFileA ( nSrcCodePage, nDstCodePage, pInfo, pErrorLineNumber ) ;
}
static int ReadGaijiFileA ( int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) {
char szFileName [ MAX_PATH ] ;
GetModuleFileNameA ( NULL, szFileName, MAX_PATH ) ;
ChangeFileNameA ( szFileName, GAIJI_FILENAME_A ) ;
FILE *Fin = fopen ( szFileName, "rb" ) ;
if ( ! Fin ) return 0 ;
int nResult = ReadGaijiFileInternal ( Fin, nSrcCodePage, nDstCodePage, pInfo, pErrorLineNumber ) ;
fclose ( Fin ) ;
return nResult ;
}
static int ReadGaijiFileW ( int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) {
wchar_t szFileName [ MAX_PATH ] ;
GetModuleFileNameW ( NULL, szFileName, MAX_PATH ) ;
ChangeFileNameW ( szFileName, GAIJI_FILENAME_W ) ;
FILE *Fin = wfopen ( szFileName, L"rb" ) ;
if ( ! Fin ) return 0 ;
int nResult = ReadGaijiFileInternal ( Fin, nSrcCodePage, nDstCodePage, pInfo, pErrorLineNumber ) ;
fclose ( Fin ) ;
return nResult ;
}
static int ReadGaijiFileInternal ( FILE *Fin, int nSrcCodePage, int nDstCodePage, GAIJIINFO *pInfo, int *pErrorLineNumber ) {
int nResult = 0 ;
int C ;
int IsIgnore = 0 ;
int nCount ;
int nLineNumber = 1 ;
CHNCONV_USERDEF *pSrcToDst = NULL ;
CHNCONV_USERDEF *pSrcToUc = NULL ;
size_t nSrcToDst = 0 ;
size_t nSrcToUc = 0 ;
size_t nSizeOfSrcToDst = 0 ;
size_t nSizeOfSrcToUc = 0 ;
if ( nSrcCodePage == CP_HZ_CHINESE_SIMPLIFIED ) nSrcCodePage = CP_CHINESE_SIMPLIFIED ;
while ( ( C = fgetc ( Fin ) ) != EOF ) {
if ( C == '\n' ) {
nLineNumber ++ ;
if ( nLineNumber == INT_MAX ) goto CAUGHT_ERROR_MEMORY ;
}
if ( C == '\r' || C == '\n' ) IsIgnore = 0 ;
if ( C == ';' ) IsIgnore = 1 ;
if ( IsIgnore || isascii ( C ) && isspace ( C ) ) continue ;
char szSrcCodePage [ 0x10 ] ;
char szDstCodePage [ 0x10 ] ;
char szSrcChar [ 0x10 ] ;
char szDstChar [ 0x10 ] ;
int nSrcCodePageCurrent ;
int nDstCodePageCurrent ;
int cSrcChar ;
int cDstChar ;
// 擖椡暥帤偺撉傒崬傒
nCount = 0 ;
while ( 1 ) {
szSrcCodePage [ nCount ++ ] = C ;
if ( nCount >= sizeof(szSrcCodePage)/sizeof(char) ) goto CAUGHT_ERROR_FORMAT ;
if ( ( C = fgetc ( Fin ) ) == EOF ) goto CAUGHT_ERROR_FORMAT ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) break ;
}
szSrcCodePage [ nCount ] = 0 ;
while ( C == 0x20 || C == 0x09 ) C = fgetc ( Fin ) ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) goto CAUGHT_ERROR_FORMAT ;
nCount = 0 ;
while ( 1 ) {
szSrcChar [ nCount ++ ] = C ;
if ( nCount >= sizeof(szSrcChar)/sizeof(char) ) goto CAUGHT_ERROR_FORMAT ;
if ( ( C = fgetc ( Fin ) ) == EOF ) goto CAUGHT_ERROR_FORMAT ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) break ;
}
szSrcChar [ nCount ] = 0 ;
while ( C == 0x20 || C == 0x09 ) C = fgetc ( Fin ) ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) goto CAUGHT_ERROR_FORMAT ;
// 弌椡暥帤偺撉傒崬傒
nCount = 0 ;
while ( 1 ) {
szDstCodePage [ nCount ++ ] = C ;
if ( nCount >= sizeof(szDstCodePage)/sizeof(char) ) goto CAUGHT_ERROR_FORMAT ;
if ( ( C = fgetc ( Fin ) ) == EOF ) goto CAUGHT_ERROR_FORMAT ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) break ;
}
szDstCodePage [ nCount ] = 0 ;
while ( C == 0x20 || C == 0x09 ) C = fgetc ( Fin ) ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) goto CAUGHT_ERROR_FORMAT ;
nCount = 0 ;
while ( 1 ) {
szDstChar [ nCount ++ ] = C ;
if ( nCount >= sizeof(szDstChar)/sizeof(char) ) goto CAUGHT_ERROR_FORMAT ;
if ( ( C = fgetc ( Fin ) ) == EOF ) goto CAUGHT_ERROR_FORMAT ;
if ( isascii ( C ) && isspace ( C ) || C == ';' ) break ;
}
szDstChar [ nCount ] = 0 ;
ungetc ( C, Fin ) ;
// 僐乕僪儁乕僕柤偺夝愅
for ( nCount = 0 ; nCount < CODEPAGE_NAME_LIST_NELEM ; nCount ++ ) {
if ( ! stricmp ( szSrcCodePage, CodePageNameList [ nCount ].szCodeName ) ) {
nSrcCodePageCurrent = CodePageNameList [ nCount ].nCodePage ;
break ;
}
}
if ( ! ( nCount < CODEPAGE_NAME_LIST_NELEM ) ) goto CAUGHT_ERROR_FORMAT ;
for ( nCount = 0 ; nCount < CODEPAGE_NAME_LIST_NELEM ; nCount ++ ) {
if ( ! stricmp ( szDstCodePage, CodePageNameList [ nCount ].szCodeName ) ) {
nDstCodePageCurrent = CodePageNameList [ nCount ].nCodePage ;
break ;
}
}
if ( ! stricmp ( szDstCodePage, "UC" ) ) nDstCodePageCurrent = CP_UTF16 ;
else if ( ! ( nCount < CODEPAGE_NAME_LIST_NELEM ) ) goto CAUGHT_ERROR_FORMAT ;
if ( nSrcCodePageCurrent == nDstCodePageCurrent ) goto CAUGHT_ERROR_FORMAT ;
// 暥帤僐乕僪偺夝愅
char *szString ;
cSrcChar = strtoul ( szSrcChar, & szString, 0x10 ) ;
if ( *szString ) goto CAUGHT_ERROR_FORMAT ;
cDstChar = strtoul ( szDstChar, & szString, 0x10 ) ;
if ( *szString ) goto CAUGHT_ERROR_FORMAT ;
// 儊儌儕乕傪妋曐偟偰抣傪奿擺
if ( pInfo && nSrcCodePageCurrent == nSrcCodePage ) {
if ( nDstCodePageCurrent == nDstCodePage ) {
if ( nSizeOfSrcToDst <= nSrcToDst ) {
nSizeOfSrcToDst += 0x20 ;
if ( nSizeOfSrcToDst >= INT_MAX ) goto CAUGHT_ERROR_MEMORY ;
CHNCONV_USERDEF *pNew = (CHNCONV_USERDEF*) realloc ( pSrcToDst, nSizeOfSrcToDst * sizeof(CHNCONV_USERDEF) ) ;
if ( ! pNew ) goto CAUGHT_ERROR_MEMORY ;
pSrcToDst = pNew ;
}
( pSrcToDst [ nSrcToDst ] ).nSrc = cSrcChar ;
( pSrcToDst [ nSrcToDst ] ).nDst = cDstChar ;
nSrcToDst ++ ;
}
if ( nDstCodePageCurrent == CP_UTF16 ) {
if ( nSizeOfSrcToUc <= nSrcToUc ) {
nSizeOfSrcToUc += 0x20 ;
if ( nSizeOfSrcToUc >= INT_MAX ) goto CAUGHT_ERROR_MEMORY ;
CHNCONV_USERDEF *pNew = (CHNCONV_USERDEF*) realloc ( pSrcToUc, nSizeOfSrcToUc * sizeof(CHNCONV_USERDEF) ) ;
if ( ! pNew ) goto CAUGHT_ERROR_MEMORY ;
pSrcToUc = pNew ;
}
( pSrcToUc [ nSrcToUc ] ).nSrc = cSrcChar ;
( pSrcToUc [ nSrcToUc ] ).nDst = cDstChar ;
nSrcToUc ++ ;
}
}
nResult = 1 ;
}
if ( pErrorLineNumber ) *pErrorLineNumber = 0 ;
if ( pInfo ) {
pInfo->pSrcToDst = pSrcToDst ;
pInfo->nNumberOfSrcToDst = nSrcToDst ;
pInfo->pSrcToUc = pSrcToUc ;
pInfo->nNumberOfSrcToUc = nSrcToUc ;
}
return nResult ;
CAUGHT_ERROR_MEMORY :
if ( pErrorLineNumber ) *pErrorLineNumber = 0 ;
free ( pSrcToDst ) ;
free ( pSrcToUc ) ;
return -1 ;
CAUGHT_ERROR_FORMAT :
if ( pErrorLineNumber ) *pErrorLineNumber = nLineNumber ;
free ( pSrcToDst ) ;
free ( pSrcToUc ) ;
return -1 ;
}
int FreeGaijiInfo ( GAIJIINFO *pInfo ) {
free ( pInfo->pSrcToDst ) ;
free ( pInfo->pSrcToUc ) ;
return 0 ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -