⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chnconv_exe.cpp

📁 This software performs code conversion of Chinese characters, including GB2312/GBK and BIG5. It a
💻 CPP
📖 第 1 页 / 共 5 页
字号:


// 僄儔乕側傜 0 傪曉偡
int GetCodePageFromTypeAndCode ( int nType, int nCode ) {

   switch ( nType ) {
      case TYPE_GB :     return CP_CHINESE_SIMPLIFIED ;
      case TYPE_BIG5 :   return CP_CHINESE_TRADITIONAL ;
      case TYPE_PSEUDO : if ( nCode >= 0 && nCode < AVAILABLE_CODE_LIST_NELEM ) return ( AvailableCodeList [ nCode ] ).nCodePage ;
                         else                                                   return 0 ;
      case TYPE_HZ :     return CP_HZ_CHINESE_SIMPLIFIED ;
      default :          return 0 ;
   }
}



////////////////////////////////////////////
//               攔懠張棟                 //
////////////////////////////////////////////



// nValue 偑 0 傑偨偼惓悢側傜丄抣傪奿擺
// nValue 偺慜偺抣傪曉偡
int SetSemaphore ( int nValue ) {

   static volatile long nSemaphore ;

   if ( nValue < 0 ) return nSemaphore ;

   return InterlockedExchange ( & nSemaphore, nValue ) ;
}



////////////////////////////////////////////
//            僐儞僶乕僞婲摦              //
////////////////////////////////////////////



int CallChnconv ( HWND hWnd, volatile int *pIsAborted ) ;



int WINAPI ConversionThread ( void *pConversionThreadInfo ) {

   // 僙儅僼僅傪庢摼
   // 偙傟埲崀丄UnlockSemaphore() 傑偱 return 偟側偄
   if ( LockSemaphore () ) return 1 ;

   HWND hWnd = ( (CONVERSIONTHREADINFO*) pConversionThreadInfo )->hWnd ;
   volatile int *pIsAborted = ( (CONVERSIONTHREADINFO*) pConversionThreadInfo )->pIsAborted ;

   // 僪儔僢僌傾儞僪僪儘僢僾傪嬛巭
   for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) {
      DragAcceptFiles ( GetDlgItem ( hWnd, IDC_EDIT_SRC_MIN + nCount ), FALSE ) ;
      DragAcceptFiles ( GetDlgItem ( hWnd, IDC_EDIT_DST_MIN + nCount ), FALSE ) ;
   }

   // 僄僨傿僢僩僐儞僩儘乕儖偺曇廤傪嬛巭
   for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) {
      Dlg_Edit_SetReadOnly ( hWnd, IDC_EDIT_SRC_MIN + nCount, TRUE ) ;
      Dlg_Edit_SetReadOnly ( hWnd, IDC_EDIT_DST_MIN + nCount, TRUE ) ;
   }

   // 幚峴娭悢傪屇傃弌偡
   if ( DEBUG_SEMAPHORE ) {
      for ( int nCount = 10 ; nCount && ! *pIsAborted ; nCount -- ) {
         Dlg_Wm_SetTextFormatA ( hWnd, IDC_EDIT_DST_MIN + SRC_DST_FILE_NELEM - 1, "%d", nCount ) ;
         Sleep ( 1000 ) ;
      }
      Dlg_Wm_SetTextA ( hWnd, IDC_EDIT_DST_MIN + SRC_DST_FILE_NELEM - 1, "" ) ;
      MessageBeep ( MB_OK ) ;
   }
   else {
      CallChnconv ( hWnd, pIsAborted ) ;
   }

   // 僄僨傿僢僩僐儞僩儘乕儖偺曇廤傪嵞奐
   for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) {
      Dlg_Edit_SetReadOnly ( hWnd, IDC_EDIT_SRC_MIN + nCount, FALSE ) ;
      Dlg_Edit_SetReadOnly ( hWnd, IDC_EDIT_DST_MIN + nCount, FALSE ) ;
   }

   // 僪儔僢僌傾儞僪僪儘僢僾傪嵞奐
   for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) {
      DragAcceptFiles ( GetDlgItem ( hWnd, IDC_EDIT_SRC_MIN + nCount ), TRUE ) ;
      DragAcceptFiles ( GetDlgItem ( hWnd, IDC_EDIT_DST_MIN + nCount ), TRUE ) ;
   }

   // 僙儅僼僅傪夝曻
   UnlockSemaphore () ;

   return 0 ;
}



int CallChnconvA ( HWND hWnd, volatile int *pIsAborted, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) ;
int CallChnconvW ( HWND hWnd, volatile int *pIsAborted, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) ;
int TestFileNameA ( HWND hWnd, int nCount ) ;
int TestFileNameW ( HWND hWnd, int nCount ) ;



// 僼傽僀儖柤傪庢摼偟僐儞僶乕僞傪婲摦
int CallChnconv ( HWND hWnd, volatile int *pIsAborted ) {


   // 惉岟儊僢僙乕僕偺弶婜壔
   for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) SetStatusIcon ( hWnd, nCount, STATUS_ICON_READY ) ;


   // 擖椡僐乕僪傪専嵏
   int nSrcCodePage = GetCodePageFromTypeAndCode ( nSrcType, nSrcCode ) ;
   int nDstCodePage = GetCodePageFromTypeAndCode ( nDstType, nDstCode ) ;

   // 擖椡僐乕僪偲弌椡僐乕僪偑摨偠側傜僄儔乕
   if ( nSrcCodePage == nDstCodePage ) {
      if ( IsNT () ) MessageBoxW ( hWnd, GetStringW ( IDS_CANNOTSAMECODE ), szCommandNameW, MB_OK ) ;
      else           MessageBoxA ( hWnd, GetStringA ( IDS_CANNOTSAMECODE ), szCommandNameA, MB_OK ) ;
      return 1 ;
   }


   // n 斣栚偺僼傽僀儖柤傪専嵏
   for ( int nCount = 0 ; nCount < SRC_DST_FILE_NELEM ; nCount ++ ) {
      int nResult ;
      if ( IsNT () ) nResult = TestFileNameW ( hWnd, nCount ) ;
      else           nResult = TestFileNameA ( hWnd, nCount ) ;
      if ( nResult ) return 1 ;
   }


   // DLL儌僕儏乕儖傪庢摼
   if ( LoadChnconvDll () ) {
      if ( IsNT () ) MessageBoxW ( hWnd, GetStringW ( IDS_DLLERROR ), szCommandNameW, MB_OK ) ;
      else           MessageBoxA ( hWnd, GetStringA ( IDS_DLLERROR ), szCommandNameA, MB_OK ) ;
      return -1 ;
   }


   CHNCONVEXINFO Info = { sizeof(CHNCONVEXINFO) } ;
   CHNFONTINFO FontInfo = { sizeof(CHNFONTINFO) } ;
   Info.pFontInfo = & FontInfo ;

   RESERVEDINFO ReservedInfo = { sizeof(RESERVEDINFO) } ;
   Info.pReserved = (void*) & ReservedInfo ;


   // 僆僾僔儑儞傪愝掕
   Info.dwConversionMode = TranslateConversionMode ( nSrcCodePage, nDstCodePage ) ;
   Info.dwAdditionalMode = TranslateAdditionalMode ( nSrcCodePage, nDstCodePage ) ;

   // 僼僅儞僩僒僀僘
   Info.nFontSize = nTmFontSize ;
   if ( ! Info.nFontSize ) Info.nFontSize = -1 ;

   // 僼僅儞僩柤傪愝掕
   if ( nDstCodePage == CP_CHINESE_SIMPLIFIED || nDstCodePage == CP_CHINESE_TRADITIONAL ) {
      if ( *szGbFont )           FontInfo.pGbFont = szGbFont ;
      if ( *szGbGothicFont )     FontInfo.pGbGothicFont = szGbGothicFont ;
      if ( *szBig5Font )         FontInfo.pBig5Font = szBig5Font ;
      if ( *szBig5GothicFont )   FontInfo.pBig5GothicFont = szBig5GothicFont ;
      if ( *szPinyinFont )       FontInfo.pPinyinFont = szPinyinFont ;
      if ( *szPinyinGothicFont ) FontInfo.pPinyinGothicFont = szPinyinGothicFont ;
   }

   // 奜帤掕媊偺撉傒崬傒
   GAIJIINFO GaijiInfo = { 0 } ;
   int nGaijiErrorLineNumber ;
   if ( ReadGaijiFile ( nSrcCodePage, nDstCodePage, & GaijiInfo, & nGaijiErrorLineNumber ) < 0 ) {
      if ( IsNT () ) {
         wchar_t szBuffer [ MESSAGE_BUFFER_SIZE ] ;
         if ( nGaijiErrorLineNumber ) swprintf ( szBuffer, GetStringW ( IDS_GAIJIFILE_ERROR ), GAIJI_FILENAME_W, nGaijiErrorLineNumber ) ;
         else                         swprintf ( szBuffer, L"Reading %s -- %s", GAIJI_FILENAME_W, GetStringW ( IDS_NOT_ENOUGH_MEMORY ) ) ;
         MessageBoxW ( hWnd, szBuffer, szCommandNameW, MB_OK ) ;
      }
      else {
         char szBuffer [ MESSAGE_BUFFER_SIZE ] ;
         if ( nGaijiErrorLineNumber ) sprintf ( szBuffer, GetStringA ( IDS_GAIJIFILE_ERROR ), GAIJI_FILENAME_A, nGaijiErrorLineNumber ) ;
         else                         sprintf ( szBuffer, "Reading %s -- %s", GAIJI_FILENAME_A, GetStringA ( IDS_NOT_ENOUGH_MEMORY ) ) ;
         MessageBoxA ( hWnd, szBuffer, szCommandNameA, MB_OK ) ;
      }
      return -1 ;
   }
   Info.pSrcToDst          = GaijiInfo.pSrcToDst ;
   Info.nNumberOfSrcToDst  = GaijiInfo.nNumberOfSrcToDst ;
   Info.pSrcToUc           = GaijiInfo.pSrcToUc ;
   Info.nNumberOfSrcToUc   = GaijiInfo.nNumberOfSrcToUc ;


   int nResult ;
   if ( IsNT () ) nResult = CallChnconvW ( hWnd, pIsAborted, nSrcCodePage, nDstCodePage, & Info ) ;
   else           nResult = CallChnconvA ( hWnd, pIsAborted, nSrcCodePage, nDstCodePage, & Info ) ;

   FreeGaijiInfo ( & GaijiInfo ) ;
   return nResult ;
}



// 俶斣栚偺僼傽僀儖柤傪専嵏乮ANSI斉乯
// 僄儔乕側傜儊僢僙乕僕傪昞帵偟 0 埲奜傪曉偡
int TestFileNameA ( HWND hWnd, int nFileNumber ) {

   char szSrcFileName [ MAX_PATH ] ;
   char szDstFileName [ MAX_PATH ] ;

   Dlg_Wm_GetTextA ( hWnd, IDC_EDIT_SRC_MIN + nFileNumber, szSrcFileName, MAX_PATH ) ;
   Dlg_Wm_GetTextA ( hWnd, IDC_EDIT_DST_MIN + nFileNumber, szDstFileName, MAX_PATH ) ;
   UnquoteFileNameA ( szSrcFileName ) ;
   UnquoteFileNameA ( szDstFileName ) ;

   int nResult = 0 ;
   if ( ! *szSrcFileName && *szDstFileName ) nResult = 1 ;
   if ( *szSrcFileName && ! *szDstFileName ) nResult = 2 ;
   if ( nResult ) {
      if ( nResult == 1 ) MessageBoxFormatA ( hWnd, szCommandNameA, MB_OK, GetStringA ( IDS_SPECIFYSRCFILE ), nFileNumber + 1 ) ;
      else                MessageBoxFormatA ( hWnd, szCommandNameA, MB_OK, GetStringA ( IDS_SPECIFYDSTFILE ), nFileNumber + 1 ) ;
      return 1 ;
   }

   return 0 ;
}



// 俶斣栚偺僼傽僀儖柤傪専嵏乮UNICODE斉乯
// 僄儔乕側傜儊僢僙乕僕傪昞帵偟 0 埲奜傪曉偡
int TestFileNameW ( HWND hWnd, int nFileNumber ) {

   wchar_t szSrcFileName [ MAX_PATH ] ;
   wchar_t szDstFileName [ MAX_PATH ] ;

   Dlg_Wm_GetTextW ( hWnd, IDC_EDIT_SRC_MIN + nFileNumber, szSrcFileName, MAX_PATH ) ;
   Dlg_Wm_GetTextW ( hWnd, IDC_EDIT_DST_MIN + nFileNumber, szDstFileName, MAX_PATH ) ;
   UnquoteFileNameW ( szSrcFileName ) ;
   UnquoteFileNameW ( szDstFileName ) ;

   int nResult = 0 ;
   if ( ! *szSrcFileName && *szDstFileName ) nResult = 1 ;
   if ( *szSrcFileName && ! *szDstFileName ) nResult = 2 ;
   if ( nResult ) {
      if ( nResult == 1 ) MessageBoxFormatW ( hWnd, szCommandNameW, MB_OK, GetStringW ( IDS_SPECIFYSRCFILE ), nFileNumber + 1 ) ;
      else                MessageBoxFormatW ( hWnd, szCommandNameW, MB_OK, GetStringW ( IDS_SPECIFYDSTFILE ), nFileNumber + 1 ) ;
      return 1 ;
   }

   return 0 ;
}



int ExecChnconvA ( HWND hWnd, char *szSrcFileName, char *szDstFileName, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) ;
int ExecChnconvW ( HWND hWnd, wchar_t *szSrcFileName, wchar_t *szDstFileName, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) ;
char *GetChnconvErrorMessageA ( int nError, int nSrcCodePage ) ;
wchar_t *GetChnconvErrorMessageW ( int nError, int nSrcCodePage ) ;

#define CP_DEFINE_CODEPAGE    0x0000FFFF     // 僐乕僪儁乕僕偺掕媊
#define CP_DEFINE_PSEUDOCODE  0x000F0000     // 媅帡拞崙岅偺掕媊



// 僼傽僀儖柤傪庢摼偟僐儞僶乕僞傪婲摦乮ANSI斉乯
int CallChnconvA ( HWND hWnd, volatile int *pIsAborted, int nSrcCodePage, int nDstCodePage, const CHNCONVEXINFO *pInfo ) {

   char szSrcFileName [ MAX_PATH ] ;
   char szDstFileName [ MAX_PATH ] ;
   char 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_GetTextA ( hWnd, IDC_EDIT_SRC_MIN + nCount, szSrcFileName, MAX_PATH ) ;
      Dlg_Wm_GetTextA ( hWnd, IDC_EDIT_DST_MIN + nCount, szDstFileName, MAX_PATH ) ;
      UnquoteFileNameA ( szSrcFileName ) ;
      UnquoteFileNameA ( szDstFileName ) ;
      if ( ! *szSrcFileName ) continue ;

      SetStatusIcon ( hWnd, nCount, STATUS_ICON_NOW ) ;

      // 昁梫側傜偽弌椡僼傽僀儖柤偵奼挘巕傪晅壛
      if ( ! IsNoAddExtension && ! IsFileExistA ( szDstFileName ) ) {
         char *szSrcExtension = GetExtensionA ( szSrcFileName ) ;
         char *szDstExtension = GetExtensionA ( szDstFileName ) ;
         if ( szSrcExtension && ! stricmp ( szSrcExtension, "RTF" ) || IsNecChineseCodePage ( nSrcCodePage ) ) {
            if ( ! szDstExtension || stricmp ( szDstExtension, "RTF" ) ) {
               strcat ( szDstFileName, ".rtf" ) ;
               Dlg_Wm_SetTextA ( hWnd, IDC_EDIT_DST_1 + nCount, szDstFileName ) ;
               Dlg_MoveCaretToEnd ( hWnd, IDC_EDIT_DST_1 + nCount ) ;
            }
         }
         else {
            if ( ! szDstExtension || ( nDstCodePage & CP_DEFINE_CODEPAGE ) != CP_JAPANESE && stricmp ( szDstExtension, "TXT" ) || ! stricmp ( szDstExtension, "RTF" ) ) {
               strcat ( szDstFileName, ".txt" ) ;
               Dlg_Wm_SetTextA ( 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 = ExecChnconvA ( hWnd, szSrcFileName, szDstFileName, nSrcCodePage, nDstCodePage, pInfo ) ;
      if ( nResult ) {
         SetStatusIcon ( hWnd, nCount, STATUS_ICON_FAILURE ) ;
         nCountFailure ++ ;
         if ( nResult < 0 ) break ;
         if ( ! IsNoMessage ) {
            char *szTail = strend ( szMessageBuffer ) ;
            char *Auguments [] = { (char*) ( nCount + 1 ), GetChnconvErrorMessageA ( nResult, nSrcCodePage ), 0 } ;
            FormatMessageA ( FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, GetStringA ( IDS_CHNCONVERROR ), 0, 0, szTail, (int) ( MESSAGE_BUFFER_SIZE - ( szTail - szMessageBuffer ) ), (va_list*) Auguments ) ;
         }
      }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -