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

📄 chnconv_dll_old.cpp

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

#include <windows.h>
#include <stdio.h>
#include "msc.h"
#include "codepage.h"
#include "storechr.h"
#include "stream.h"
#include "stringex.h"

#include "conv_err.h"
#include "chnconv.h"



static volatile long nSemaphore ;            // 婲摦偝傟偰偄傟偽 1
#define LockSemaphore()    InterlockedExchange ( & nSemaphore, 1 )   // if success, return 0
#define UnlockSemaphore()  InterlockedExchange ( & nSemaphore, 0 )

static CHNCONV_USERDEF *AllocCurrentUserDefinition ( int nSrcCodePage, int nDstCodePage, size_t *pLength ) ;



////////////////////////////////////////////
//               媽娭悢 (1)               //
////////////////////////////////////////////



static CHNCONVEXINFO Info ;
static unsigned long dwConversionModeTm ;



int WINAPI ChnconvA ( const char *szSrcFileName, const char *szDstFileName, int nSrcCodePage, int nDstCodePage, unsigned long dwConversionMode ) {

   if ( LockSemaphore () ) return SetChnconvLastError ( CONV_ERROR_SEMAPHORE ) ;

   Info.cbSize = sizeof(CHNCONVEXINFO) ;
   Info.dwConversionMode = dwConversionMode ;

   if ( nSrcCodePage == CP_JAPANESE_TM ) dwConversionMode |= dwConversionModeTm ;

   RESERVEDINFO ReservedInfo = { sizeof(RESERVEDINFO) } ;
   ReservedInfo.dwMode = ( dwConversionMode & CONVMODE_TM_NO_OUTPUT_RTFFORM ) ? RESERVEDMODE_NO_START_END : 0 ;
   Info.pReserved = (void*) & ReservedInfo ;

   CHNCONV_USERDEF *pSrcToDst = AllocCurrentUserDefinition ( nSrcCodePage, nDstCodePage, & Info.nNumberOfSrcToDst ) ;
   Info.pSrcToDst = pSrcToDst ;
   CHNCONV_USERDEF *pSrcToUc = AllocCurrentUserDefinition ( nSrcCodePage, CP_UTF16, & Info.nNumberOfSrcToUc ) ;
   Info.pSrcToUc = pSrcToUc ;

   int nResult = ChnconvExA ( szSrcFileName, szDstFileName, nSrcCodePage, nDstCodePage, & Info ) ;

   free ( pSrcToDst ) ;
   free ( pSrcToUc ) ;

   UnlockSemaphore () ;
   return nResult ;
}



int WINAPI ChnconvW ( const wchar_t *szSrcFileName, const wchar_t *szDstFileName, int nSrcCodePage, int nDstCodePage, unsigned long dwConversionMode ) {

   if ( LockSemaphore () ) return SetChnconvLastError ( CONV_ERROR_SEMAPHORE ) ;

   Info.cbSize = sizeof(CHNCONVEXINFO) ;
   Info.dwConversionMode = dwConversionMode ;

   if ( nSrcCodePage == CP_JAPANESE_TM ) dwConversionMode |= dwConversionModeTm ;

   RESERVEDINFO ReservedInfo = { sizeof(RESERVEDINFO) } ;
   ReservedInfo.dwMode = ( dwConversionMode & CONVMODE_TM_NO_OUTPUT_RTFFORM ) ? RESERVEDMODE_NO_START_END : 0 ;
   Info.pReserved = (void*) & ReservedInfo ;

   CHNCONV_USERDEF *pSrcToDst = AllocCurrentUserDefinition ( nSrcCodePage, nDstCodePage, & Info.nNumberOfSrcToDst ) ;
   Info.pSrcToDst = pSrcToDst ;
   CHNCONV_USERDEF *pSrcToUc = AllocCurrentUserDefinition ( nSrcCodePage, CP_UTF16, & Info.nNumberOfSrcToUc ) ;
   Info.pSrcToUc = pSrcToUc ;

   int nResult = ChnconvExW ( szSrcFileName, szDstFileName, nSrcCodePage, nDstCodePage, & Info ) ;

   free ( pSrcToDst ) ;
   free ( pSrcToUc ) ;

   UnlockSemaphore () ;
   return nResult ;
}



intptr_t WINAPI ChnconvString ( const char *szSrcBuffer, char *szDstBuffer, int nSrcCodePage, int nDstCodePage, unsigned long dwConversionMode, size_t nMaxLen ) {

   if ( LockSemaphore () ) return SetChnconvLastError ( CONV_ERROR_SEMAPHORE ) ;

   Info.cbSize = sizeof(CHNCONVEXINFO) ;
   Info.dwConversionMode = dwConversionMode ;

   if ( nSrcCodePage == CP_JAPANESE_TM ) dwConversionMode |= dwConversionModeTm ;

   RESERVEDINFO ReservedInfo = { sizeof(RESERVEDINFO) } ;
   ReservedInfo.dwMode = ( dwConversionMode & CONVMODE_TM_NO_OUTPUT_RTFFORM ) ? RESERVEDMODE_NO_START_END : 0 ;
   Info.pReserved = (void*) & ReservedInfo ;

   CHNCONV_USERDEF *pSrcToDst = AllocCurrentUserDefinition ( nSrcCodePage, nDstCodePage, & Info.nNumberOfSrcToDst ) ;
   Info.pSrcToDst = pSrcToDst ;
   CHNCONV_USERDEF *pSrcToUc = AllocCurrentUserDefinition ( nSrcCodePage, CP_UTF16, & Info.nNumberOfSrcToUc ) ;
   Info.pSrcToUc = pSrcToUc ;

   intptr_t nResult = ChnconvStringEx ( szSrcBuffer, szDstBuffer, nSrcCodePage, nDstCodePage, & Info, nMaxLen ) ;

   free ( pSrcToDst ) ;
   free ( pSrcToUc ) ;

   UnlockSemaphore () ;
   return nResult ;
}



int WINAPI ChnconvChar ( int C, int nSrcCodePage, int nDstCodePage, unsigned long dwConversionMode ) {

   if ( LockSemaphore () ) return SetChnconvLastError ( CONV_ERROR_SEMAPHORE ) ;

   Info.cbSize = sizeof(CHNCONVEXINFO) ;
   Info.dwConversionMode = dwConversionMode ;

   int nResult = ChnconvCharEx ( C, nSrcCodePage, nDstCodePage, & Info ) ;

   UnlockSemaphore () ;
   return nResult ;
}



int WINAPI IsAvailableCodePage ( int nSrcCodePage, int nDstCodePage ) {
   return IsChnconvValidCodePage ( nSrcCodePage, nDstCodePage ) ;
}



////////////////////////////////////////////
//               媽娭悢 (2)               //
////////////////////////////////////////////



static CHNFONTINFO FontInfo ;

static volatile long IsFreeBufferProcRegistered ;
void __cdecl FreeBuffer ( void ) ;



// GB 偺弌椡僼僅儞僩柤傪愝掕
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 1 傪曉偡
int WINAPI SetGbFontName ( const char *szFont, const char *szGothicFont ) {

   if ( LockSemaphore () ) return 1 ;

   int nResult = 0 ;

   if ( ! szFont && ! szGothicFont ) {
      free ( (char*) FontInfo.pGbFont ) ;
      free ( (char*) FontInfo.pGbGothicFont ) ;
   }
   else {

      if ( szFont ) {
         free ( (char*) FontInfo.pGbFont ) ;
         FontInfo.pGbFont = strdup ( szFont ) ;
         if ( ! FontInfo.pGbFont ) nResult = 1 ;
      }

      if ( szGothicFont ) {
         free ( (char*) FontInfo.pGbGothicFont ) ;
         FontInfo.pGbGothicFont = strdup ( szGothicFont ) ;
         if ( ! FontInfo.pGbGothicFont ) nResult = 1 ;
      }
   }

   FontInfo.cbSize = sizeof(CHNFONTINFO) ;
   Info.pFontInfo = & FontInfo ;

   if ( ! InterlockedExchange ( & IsFreeBufferProcRegistered, TRUE ) ) atexit ( FreeBuffer ) ;

   UnlockSemaphore () ;
   return nResult ;
}



// BIG5 偺弌椡僼僅儞僩柤傪愝掕
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 1 傪曉偡
int WINAPI SetBig5FontName ( const char *szFont, const char *szGothicFont ) {

   if ( LockSemaphore () ) return 1 ;

   int nResult = 0 ;

   if ( ! szFont && ! szGothicFont ) {
      free ( (char*) FontInfo.pBig5Font ) ;
      free ( (char*) FontInfo.pBig5GothicFont ) ;
   }
   else {

      if ( szFont ) {
         free ( (char*) FontInfo.pBig5Font ) ;
         FontInfo.pBig5Font = strdup ( szFont ) ;
         if ( ! FontInfo.pBig5Font ) nResult = 1 ;
      }

      if ( szGothicFont ) {
         free ( (char*) FontInfo.pBig5GothicFont ) ;
         FontInfo.pBig5GothicFont = strdup ( szGothicFont ) ;
         if ( ! FontInfo.pBig5GothicFont ) nResult = 1 ;
      }
   }

   FontInfo.cbSize = sizeof(CHNFONTINFO) ;
   Info.pFontInfo = & FontInfo ;

   if ( ! InterlockedExchange ( & IsFreeBufferProcRegistered, TRUE ) ) atexit ( FreeBuffer ) ;

   UnlockSemaphore () ;
   return nResult ;
}



// PINYIN 偺弌椡僼僅儞僩柤傪愝掕
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 1 傪曉偡
int WINAPI SetPinyinFontName ( const char *szFont, const char *szGothicFont ) {

   if ( LockSemaphore () ) return 1 ;

   int nResult = 0 ;

   if ( ! szFont && ! szGothicFont ) {
      free ( (char*) FontInfo.pPinyinFont ) ;
      free ( (char*) FontInfo.pPinyinGothicFont ) ;
   }
   else {

      if ( szFont ) {
         free ( (char*) FontInfo.pPinyinFont ) ;
         FontInfo.pPinyinFont = strdup ( szFont ) ;
         if ( ! FontInfo.pPinyinFont ) nResult = 1 ;
      }

      if ( szGothicFont ) {
         free ( (char*) FontInfo.pPinyinGothicFont ) ;
         FontInfo.pPinyinGothicFont = strdup ( szGothicFont ) ;
         if ( ! FontInfo.pPinyinGothicFont ) nResult = 1 ;
      }
   }

   FontInfo.cbSize = sizeof(CHNFONTINFO) ;
   Info.pFontInfo = & FontInfo ;

   if ( ! InterlockedExchange ( & IsFreeBufferProcRegistered, TRUE ) ) atexit ( FreeBuffer ) ;

   UnlockSemaphore () ;
   return nResult ;
}



// GB 偲 BIG5 偺弌椡僼僅儞僩柤傪愝掕
// 惉岟偟偨傜 0 傪丄幐攕偟偨傜 1 傪曉偡
int WINAPI SetFontName ( const char *szGb, const char *szGbGothic, const char *szBig5, const char *szBig5Gothic ) {

   int nResult = 0 ;

   if ( ! szGb && ! szGbGothic && ! szBig5 && ! szBig5Gothic ) {
      nResult += SetGbFontName ( szGb, szGbGothic ) ;
      nResult += SetBig5FontName ( szBig5, szBig5Gothic ) ;
   }
   else {
      if ( szGb || szGbGothic ) nResult += SetGbFontName ( szGb, szGbGothic ) ;
      if ( szBig5 || szBig5Gothic ) nResult += SetBig5FontName ( szBig5, szBig5Gothic ) ;
   }

   return nResult ;
}



////////////////////////////////////////////
//               媽娭悢 (3)               //
////////////////////////////////////////////



typedef struct {
   int nSrcCodePage ;
   int nDstCodePage ;

⌨️ 快捷键说明

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