📄 chnconv_exe.cpp
字号:
else {
SetStatusIcon ( hWnd, nCount, STATUS_ICON_SUCCESS ) ;
nCountSuccess ++ ;
}
}
// 僞僀僩儖傪栠偡
SetMainDialogTitle ( hWnd, 0 ) ;
if ( ! IsNoMessage ) {
if ( nCountSuccess || nCountFailure ) sprintf ( strend ( szMessageBuffer ), GetStringA ( IDS_CONVERTEDFILE ), nCountSuccess ) ;
else strcpy ( szMessageBuffer, GetStringA ( IDS_DIDNOTCONVERTFILE ) ) ;
MessageBoxA ( hWnd, szMessageBuffer, szCommandNameA, MB_OK ) ;
}
return 0 ;
}
// 僼傽僀儖柤傪庢摼偟僐儞僶乕僞傪婲摦乮UNICODE斉乯
int CallChnconvW ( HWND hWnd, volatile int *pIsAborted, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) {
wchar_t szSrcFileName [ MAX_PATH ] ;
wchar_t szDstFileName [ MAX_PATH ] ;
wchar_t szMessageBuffer [ MESSAGE_BUFFER_SIZE ] ;
int nCountSuccess = 0 ;
int nCountFailure = 0 ;
// 僞僀僩儖傪曄峏
// 偙傟埲崀丄僞僀僩儖傪栠偡傑偱 return 偟側偄
SetMainDialogTitle ( hWnd, 1 ) ;
// 儊僢僙乕僕僶僢僼傽偺弶婜壔
*szMessageBuffer = 0 ;
// 僼傽僀儖柤傪庢摼偟曄姺傪幚峴
for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) {
Dlg_Wm_GetTextW ( hWnd, IDC_EDIT_SRC_MIN + nCount, szSrcFileName, MAX_PATH ) ;
Dlg_Wm_GetTextW ( hWnd, IDC_EDIT_DST_MIN + nCount, szDstFileName, MAX_PATH ) ;
UnquoteFileNameW ( szSrcFileName ) ;
UnquoteFileNameW ( szDstFileName ) ;
if ( ! *szSrcFileName ) continue ;
SetStatusIcon ( hWnd, nCount, STATUS_ICON_NOW ) ;
// 昁梫側傜偽弌椡僼傽僀儖柤偵奼挘巕傪晅壛
if ( ! IsNoAddExtension && ! IsFileExistW ( szDstFileName ) ) {
wchar_t *szSrcExtension = GetExtensionW ( szSrcFileName ) ;
wchar_t *szDstExtension = GetExtensionW ( szDstFileName ) ;
if ( szSrcExtension && ! wcsicmp ( szSrcExtension, L"RTF" ) || IsNecChineseCodePage ( nSrcCodePage ) ) {
if ( ! szDstExtension || wcsicmp ( szDstExtension, L"RTF" ) ) {
wcscat ( szDstFileName, L".rtf" ) ;
Dlg_Wm_SetTextW ( hWnd, IDC_EDIT_DST_1 + nCount, szDstFileName ) ;
Dlg_MoveCaretToEnd ( hWnd, IDC_EDIT_DST_1 + nCount ) ;
}
}
else {
if ( ! szDstExtension || ( nDstCodePage & CP_DEFINE_CODEPAGE ) != CP_JAPANESE && wcsicmp ( szDstExtension, L"TXT" ) || ! wcsicmp ( szDstExtension, L"RTF" ) ) {
wcscat ( szDstFileName, L".txt" ) ;
Dlg_Wm_SetTextW ( hWnd, IDC_EDIT_DST_1 + nCount, szDstFileName ) ;
Dlg_MoveCaretToEnd ( hWnd, IDC_EDIT_DST_1 + nCount ) ;
}
}
}
// 曄姺傪幚峴
int nResult ;
if ( *pIsAborted ) nResult = CONV_ERROR_ABORTED ;
else nResult = ExecChnconvW ( hWnd, szSrcFileName, szDstFileName, nSrcCodePage, nDstCodePage, pInfo ) ;
if ( nResult ) {
SetStatusIcon ( hWnd, nCount, STATUS_ICON_FAILURE ) ;
nCountFailure ++ ;
if ( nResult < 0 ) break ;
if ( ! IsNoMessage ) {
wchar_t *szTail = wcsend ( szMessageBuffer ) ;
wchar_t *Auguments [] = { (wchar_t*) ( nCount + 1 ), GetChnconvErrorMessageW ( nResult, nSrcCodePage ), 0 } ;
FormatMessageW ( FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, GetStringW ( IDS_CHNCONVERROR ), 0, 0, szTail, (int) ( MESSAGE_BUFFER_SIZE - ( szTail - szMessageBuffer ) ), (va_list*) Auguments ) ;
}
}
else {
SetStatusIcon ( hWnd, nCount, STATUS_ICON_SUCCESS ) ;
nCountSuccess ++ ;
}
}
// 僞僀僩儖傪栠偡
SetMainDialogTitle ( hWnd, 0 ) ;
if ( ! IsNoMessage ) {
if ( nCountSuccess || nCountFailure ) swprintf ( wcsend ( szMessageBuffer ), GetStringW ( IDS_CONVERTEDFILE ), nCountSuccess ) ;
else wcscpy ( szMessageBuffer, GetStringW ( IDS_DIDNOTCONVERTFILE ) ) ;
MessageBoxW ( hWnd, szMessageBuffer, szCommandNameW, MB_OK ) ;
}
return 0 ;
}
// CHNCONV 傪屇傃弌偡乮ANSI斉乯
// 栠傝抣偼丄CHNCONV 偺曉傝抣傪偦偺傑傑曉偡
int ExecChnconvA ( HWND hWnd, char *szSrcFileName, char *szDstFileName, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) {
char szRealSrcFileName [ MAX_PATH ] ;
char szRealDstFileName [ MAX_PATH ] ;
char szTempFileName [ MAX_PATH ] ;
char szCurrentFolder [ MAX_PATH ] ;
int nExitCode ;
// 杮摉偺僼傽僀儖柤傪庢摼
GetSpecialFolderPathA ( hWnd, szCurrentFolder, CSIDL_PERSONAL, 0 ) ;
SetCurrentDirectoryA ( szCurrentFolder ) ;
GetFullPathNameExA ( szSrcFileName, szRealSrcFileName, MAX_PATH ) ;
strcpy ( szCurrentFolder, szRealSrcFileName ) ;
ChangeFileNameA ( szCurrentFolder, NULL ) ;
SetPathSeparatorA ( szCurrentFolder, ADD_SEPARATOR_ONLY_ROOT ) ;
SetCurrentDirectoryA ( szCurrentFolder ) ;
GetFullPathNameExA ( szDstFileName, szRealDstFileName, MAX_PATH ) ;
// 僇儗儞僩僨傿儗僋僩儕傪栠偡
GetModuleFileNameA ( NULL, szCurrentFolder, MAX_PATH ) ;
ChangeFileNameA ( szCurrentFolder, NULL ) ;
SetPathSeparatorA ( szCurrentFolder, ADD_SEPARATOR_ONLY_ROOT ) ;
SetCurrentDirectoryA ( szCurrentFolder ) ;
// 僼傽僀儖傾僋僙僗尃偺専嵏
if ( ! IsFileReadableA ( szRealSrcFileName ) ) return CONV_ERROR_READ ;
if ( ! IsFileWritableA ( szRealDstFileName ) ) return CONV_ERROR_WRITE ;
// 曄姺傪幚峴
if ( IsFileExistA ( szRealDstFileName ) ) {
if ( GetTempFileNameExA ( szTempFileHeaderA, szTempFileName ) ) return CONV_ERROR_UNKNOWN ;
nExitCode = ChnconvExA ( szRealSrcFileName, szTempFileName, nSrcCodePage, nDstCodePage, pInfo ) ;
if ( ! nExitCode && IsFileExistA ( szRealDstFileName ) ) {
nExitCode = CONV_ERROR_ABORTED ;
if ( IsNoConfirm || MessageBoxFormatA ( hWnd, szCommandNameA, MB_OKCANCEL, GetStringA ( IDS_ASKOVERWRITE ), szDstFileName ) == IDOK ) {
nExitCode = CONV_ERROR_WRITE ;
if ( IsFileWritableA ( szRealDstFileName ) ) {
if ( IsNoRecycle ) DeleteFileA ( szRealDstFileName ) ;
else ShellDeleteFileA ( NULL, szRealDstFileName, 0 ) ;
if ( MoveFileA ( szTempFileName, szRealDstFileName ) ) nExitCode = 0 ;
}
}
}
if ( IsFileExistA ( szTempFileName ) ) DeleteFileA ( szTempFileName ) ;
}
else {
nExitCode = ChnconvExA ( szRealSrcFileName, szRealDstFileName, nSrcCodePage, nDstCodePage, pInfo ) ;
if ( nExitCode && IsFileExistA ( szRealDstFileName ) ) DeleteFileA ( szRealDstFileName ) ;
}
if ( ! nExitCode ) ShellChangeNotifyA ( szRealDstFileName ) ;
#ifndef _MT
if ( dwConversionMode & CONVMODE_PEEKMESSAGE ) {
MSG Msg ;
while ( PeekMessageA ( & Msg, NULL, 0, 0, PM_REMOVE ) ) {
TranslateMessage ( & Msg ) ;
DispatchMessageA ( & Msg ) ;
}
}
#endif
return nExitCode ;
}
// CHNCONV 傪屇傃弌偡乮UNICODE斉乯
// 栠傝抣偼丄CHNCONV 偺曉傝抣傪偦偺傑傑曉偡
int ExecChnconvW ( HWND hWnd, wchar_t *szSrcFileName, wchar_t *szDstFileName, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) {
wchar_t szRealSrcFileName [ MAX_PATH ] ;
wchar_t szRealDstFileName [ MAX_PATH ] ;
wchar_t szTempFileName [ MAX_PATH ] ;
wchar_t szCurrentFolder [ MAX_PATH ] ;
int nExitCode ;
// 杮摉偺僼傽僀儖柤傪庢摼
GetSpecialFolderPathW ( hWnd, szCurrentFolder, CSIDL_PERSONAL, 0 ) ;
SetCurrentDirectoryW ( szCurrentFolder ) ;
GetFullPathNameExW ( szSrcFileName, szRealSrcFileName, MAX_PATH ) ;
wcscpy ( szCurrentFolder, szRealSrcFileName ) ;
ChangeFileNameW ( szCurrentFolder, NULL ) ;
SetPathSeparatorW ( szCurrentFolder, ADD_SEPARATOR_ONLY_ROOT ) ;
SetCurrentDirectoryW ( szCurrentFolder ) ;
GetFullPathNameExW ( szDstFileName, szRealDstFileName, MAX_PATH ) ;
// 僇儗儞僩僨傿儗僋僩儕傪栠偡
GetModuleFileNameW ( NULL, szCurrentFolder, MAX_PATH ) ;
ChangeFileNameW ( szCurrentFolder, NULL ) ;
SetPathSeparatorW ( szCurrentFolder, ADD_SEPARATOR_ONLY_ROOT ) ;
SetCurrentDirectoryW ( szCurrentFolder ) ;
// 僼傽僀儖傾僋僙僗尃偺専嵏
if ( ! IsFileReadableW ( szRealSrcFileName ) ) return CONV_ERROR_READ ;
if ( ! IsFileWritableW ( szRealDstFileName ) ) return CONV_ERROR_WRITE ;
// 曄姺傪幚峴
if ( IsFileExistW ( szRealDstFileName ) ) {
if ( GetTempFileNameExW ( szTempFileHeaderW, szTempFileName ) ) return CONV_ERROR_UNKNOWN ;
nExitCode = ChnconvExW ( szRealSrcFileName, szTempFileName, nSrcCodePage, nDstCodePage, pInfo ) ;
if ( ! nExitCode && IsFileExistW ( szRealDstFileName ) ) {
nExitCode = CONV_ERROR_ABORTED ;
if ( IsNoConfirm || MessageBoxFormatW ( hWnd, szCommandNameW, MB_OKCANCEL, GetStringW ( IDS_ASKOVERWRITE ), szDstFileName ) == IDOK ) {
nExitCode = CONV_ERROR_WRITE ;
if ( IsFileWritableW ( szRealDstFileName ) ) {
if ( IsNoRecycle ) DeleteFileW ( szRealDstFileName ) ;
else ShellDeleteFileW ( NULL, szRealDstFileName, 0 ) ;
if ( MoveFileW ( szTempFileName, szRealDstFileName ) ) nExitCode = 0 ;
}
}
}
if ( IsFileExistW ( szTempFileName ) ) DeleteFileW ( szTempFileName ) ;
}
else {
nExitCode = ChnconvExW ( szRealSrcFileName, szRealDstFileName, nSrcCodePage, nDstCodePage, pInfo ) ;
if ( nExitCode && IsFileExistW ( szRealDstFileName ) ) DeleteFileW ( szRealDstFileName ) ;
}
if ( ! nExitCode ) ShellChangeNotifyW ( szRealDstFileName ) ;
#ifndef _MT
if ( dwConversionMode & CONVMODE_PEEKMESSAGE ) {
MSG Msg ;
while ( PeekMessageW ( & Msg, NULL, 0, 0, PM_REMOVE ) ) {
TranslateMessage ( & Msg ) ;
DispatchMessageW ( & Msg ) ;
}
}
#endif
return nExitCode ;
}
// GetChnconvErrorMessage 偺壓惪偗娭悢
int GetStringIdFromChnconvError ( int nError, int nSrcCodePage ) {
switch ( nError ) {
case CONV_ERROR_READ : return IDS_CONV_ERROR_READ ;
case CONV_ERROR_WRITE : return IDS_CONV_ERROR_WRITE ;
case CONV_ERROR_FORMAT :
case CONV_ERROR_BRACE : return ( nSrcCodePage==CP_JAPANESE_TM ) ? IDS_CONV_ERROR_TMFORMAT : IDS_CONV_ERROR_FORMAT ;
case CONV_ERROR_MEMORY : return IDS_CONV_ERROR_MEMORY ;
case CONV_ERROR_CODEPAGE : return IDS_CONV_ERROR_CODEPAGE ;
case CONV_ERROR_ABORTED : return IDS_CONV_ERROR_ABORTED ;
default : return IDS_CONV_ERROR_UNKNOWN ;
}
}
// CHNCONV 偺僄儔乕抣偵傆偝傢偟偄暥帤楍傪曉偡乮ANSI斉乯
char *GetChnconvErrorMessageA ( int nError, int nSrcCodePage ) {
static char szBuffer [ SHORTSTRING_MAX ] ;
LoadStringExA ( GetStringIdFromChnconvError ( nError, nSrcCodePage ), szBuffer, MAX_PATH ) ;
return szBuffer ;
}
// CHNCONV 偺僄儔乕抣偵傆偝傢偟偄暥帤楍傪曉偡乮UNICODE斉乯
wchar_t *GetChnconvErrorMessageW ( int nError, int nSrcCodePage ) {
static wchar_t szBuffer [ SHORTSTRING_MAX ] ;
LoadStringExW ( GetStringIdFromChnconvError ( nError, nSrcCodePage ), szBuffer, MAX_PATH ) ;
return szBuffer ;
}
////////////////////////////////////////////
// 僿儖僾昞帵 //
////////////////////////////////////////////
INT_PTR CALLBACK HelpDialogProc ( HWND hWnd, unsigned int dwMessage, WPARAM wParam, LPARAM lParam ) ;
int GetChnconvDllVersion ( void ) ;
int DisplayHelp ( HWND hWnd ) {
// 忣曬僂傿儞僪僂傪昞帵
if ( IsNT () ) DialogBoxExW ( IDD_DIALOG_HELP, hWnd, HelpDialogProc ) ;
else DialogBoxExA ( IDD_DIALOG_HELP, hWnd, HelpDialogProc ) ;
return 0 ;
}
// 僿儖僾昞帵僟僀傾儘僌
INT_PTR CALLBACK HelpDialogProc ( HWND hWnd, unsigned int dwMessage, WPARAM wParam, LPARAM lParam ) {
int nDllVersion ;
int nGaijiFileStatus ;
int nGaijiErrorLineNumber ;
switch ( dwMessage ) {
case WM_INITDIALOG :
MoveWindowToCenter ( hWnd ) ;
// 僞僀僩儖傪愝掕
if ( IsNT () ) Wm_SetTextFormatW ( hWnd, L"%s %s", szCommandNameW, GetVersionCopyrightW () ) ;
else Wm_SetTextFormatA ( hWnd, "%s %s", szCommandNameA, GetVersionCopyrightA () ) ;
nDllVersion = GetChnconvDllVersion () ;
nGaijiFileStatus = ReadGaijiFile ( 0, 0, NULL, & nGaijiErrorLineNumber ) ;
if ( IsNT () ) {
wchar_t szBuffer [ MESSAGE_BUFFER_SIZE ] ;
swprintf ( szBuffer, GetStringW ( IDS_DISPLAYEXEVERSION ), nVersion / 100, nVersion % 100 ) ;
if ( nDllVersion ) swprintf ( wcsend ( szBuffer ), GetStringW ( IDS_DISPLAYDLLVERSION ), nDllVersion / 100, nDllVersion % 100 ) ;
else wcscat ( szBuffer, GetStringW ( IDS_DISPLAYDLLVERSIONERROR ) ) ;
if ( nGaijiFileStatus > 0 ) swpri
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -