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

📄 conv_opt.cpp

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

#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#include "msc.h"
#include "access.h"
#include "filename.h"
#include "res.h"
#include "resource.h"
#include "shfolder.h"
#include "stringex.h"

#if defined RTFCONV_EXE || defined RTFCONV_COM || defined SETUP_R
#include "rtfconv.h"
#include "rtfconv_opt.h"
#include "uniblock.h"
#endif

#if defined CHNCONV_EXE || defined CHNCONV_COM || defined SETUP_C
#include "chnconv.h"
#include "chnconv_opt.h"
#include "codepage.h"
#endif

#include "conv_opt.h"


// for CHNCONV.EXE/COM and RTFCONV.EXE/COM



////////////////////////////////////////
//       婲摦帪偺僆僾僔儑儞愝掕       //
////////////////////////////////////////



static int ReadOptionA ( void ) ;
static int ReadOptionW ( void ) ;
static int ReadOptionFile ( FILE *Fin ) ;
void __cdecl FreeFontNameArray ( void ) ;



// 婲摦帪偵僆僾僔儑儞偺庢摼
// 僼傽僀儖偑尒偮偐傜側偗傟偽 1 埲忋傪曉偡
// 儊儌儕乕偑妋曐偱偒側偗傟偽 晧悢傪曉偡
// 惉岟偟偨傜 0 傪曉偡
int ReadOption ( void ) {

   int nResult ;

   if ( IsNT () ) nResult = ReadOptionW () ;
   else           nResult = ReadOptionA () ;


#if defined RTFCONV_EXE || defined CHNCONV_EXE
   if ( nSavedSrcType >= 0 && nSavedSrcType < SRC_TYPE_NELEM ) nSrcType = nSavedSrcType ;
   if ( nSavedDstType >= 0 && nSavedDstType < DST_TYPE_NELEM ) nDstType = nSavedDstType ;

   for ( int nCount = 0 ; nCount < AVAILABLE_CODE_LIST_NELEM ; nCount ++ ) {
      if ( nSavedSrcCode == ( AvailableCodeList [ nCount ] ).nCodePage ) nSrcCode = nCount ;
      if ( nSavedDstCode == ( AvailableCodeList [ nCount ] ).nCodePage ) nDstCode = nCount ;
   }
#endif

#if defined RTFCONV_EXE
   if ( IsNoJis83New && IsNoJis83Old ) IsNoJis83Old = 0 ;
#endif

#if defined CHNCONV_EXE || defined CHNCONV_COM
   if ( ! IsValidCodePage ( CP_CHINESE_SIMPLIFIED ) || ! IsValidCodePage ( CP_CHINESE_TRADITIONAL ) ) {
      IsGb2big5N = 0 ;
      IsBig52gbN = 0 ;
   }
#endif

#if defined CHNCONV_EXE
   if ( nSavedDstCode == CP_JAPANESE_TM ) nDstCode = 0 ;
#endif


   return nResult ;
}



static int ReadOptionA ( void ) {

   char szFileNameBuffer [ MAX_PATH ] ;

   const char *szFileName = szConfigFileNameA ;

   if ( ! szFileName ) {

#if ! defined SETUP_R && ! defined SETUP_C
      GetModuleFileNameA ( NULL, szFileNameBuffer, MAX_PATH ) ;
      ChangeFileNameA ( szFileNameBuffer, CFG_FILENAME_A ) ;
#else
      strcpy ( szFileNameBuffer, szInstallFolderA ) ;
      SetPathSeparatorA ( szFileNameBuffer, ADD_SEPARATOR_ALWAYS ) ;
      strcat ( szFileNameBuffer, CFG_FILENAME_A ) ;
#endif

      szFileName = szFileNameBuffer ;
   }


#if defined RTFCONV_EXE || defined CHNCONV_EXE || defined SETUP_R || defined SETUP_C
   if ( ! IsFileWritableA ( szFileName ) ) IsOptionReadOnly = 1 ;
#endif


   FILE *Fin = fopen ( szFileName, "rb" ) ;
   if ( ! Fin ) return 1 ;

   int nResult = ReadOptionFile ( Fin ) ;

   fclose ( Fin ) ;

   return nResult ;
}



static int ReadOptionW ( void ) {

   wchar_t szFileNameBuffer [ MAX_PATH ] ;

   const wchar_t *szFileName = szConfigFileNameW ;

   if ( ! szFileName ) {

      if ( GetSpecialFolderPathW ( NULL, szFileNameBuffer, CSIDL_APPDATA, 0 ) ) return 1 ;

      wcscat ( szFileNameBuffer, L"\\Harigaya" ) ;

      SetPathSeparatorW ( szFileNameBuffer, ADD_SEPARATOR_ALWAYS ) ;
      wcscat ( szFileNameBuffer, CFG_FILENAME_W ) ;

      szFileName = szFileNameBuffer ;
   }


#if defined RTFCONV_EXE || defined CHNCONV_EXE || defined SETUP_R || defined SETUP_C
   if ( ! IsFileWritableW ( szFileName ) ) IsOptionReadOnly = 1 ;
#endif


   FILE *Fin = wfopen ( szFileName, L"rb" ) ;
   if ( ! Fin ) return 1 ;

   int nResult = ReadOptionFile ( Fin ) ;

   fclose ( Fin ) ;

   return nResult ;
}



static int GetOptionInteger ( char *szString, const char *szName, int *pValue ) ;
static int GetOptionStringA ( char *szString, const char *szName, char *szValue, int nMaxLen ) ;
static int GetOptionWideString ( char *szString, const char *szName, wchar_t *szValue, int nMaxLen ) ;
static int GetOptionBinary ( char *szString, const char *szName, unsigned short *pValue, int nLen ) ;



// 僆僾僔儑儞僼傽僀儖偺撉傒崬傒
// 儊儌儕乕傪妋曐偱偒側偄偲偒偼丄晧悢傪曉偡
// 惉岟偟偨傜 0 傪曉偡
static int ReadOptionFile ( FILE *Fin ) {


   char szLineBuffer [ OPTION_FILE_LINE_BUFFER ] ;


   while ( fgets ( szLineBuffer, OPTION_FILE_LINE_BUFFER, Fin ) ) {


      // 堦峴偑挿偡偓傞偲偒
      if ( ! strchr ( szLineBuffer, '\n' ) ) {
         while ( 1 ) {
            int C = fgetc ( Fin ) ;
            if ( C == '\n' || C == EOF ) break ;
         }
      }


      // 惍悢抣偺庢摼
      for ( int nCount = 0 ; nCount < OPTION_INTEGER_LIST_NELEM ; nCount ++ ) {

         int *pValue = ( OptionIntegerList [ nCount ] ).nValue ;

         if ( ! GetOptionInteger ( szLineBuffer, ( OptionIntegerList [ nCount ] ).szName, pValue ) ) {

#if defined RTFCONV_EXE || defined CHNCONV_EXE || defined SETUP_R || defined SETUP_C
            if ( pValue == & nSavedSrcType || pValue == & nSavedDstType ) IsSaveCode = 1 ;
            if ( pValue == & nSavedSrcCode || pValue == & nSavedDstCode ) IsSaveCode = 1 ;
            if ( pValue == & nSavedWindowX || pValue == & nSavedWindowY ) IsSaveWindow = 1 ;
#endif
#if defined RTFCONV_EXE || defined RTFCONV_COM || defined SETUP_R
            if ( pValue == & nUnmappableCharFormat && nUnmappableCharFormat >= UNMAPPABLE_CHAR_FORMAT_LIST_NELEM ) nUnmappableCharFormat = 0 ;
#endif
#if defined CHNCONV_EXE || defined CHNCONV_COM || defined SETUP_C
            if ( pValue == & IsLinshiDaimaToCjkEx && *pValue ) IsCjkExToLinshiDaima = 0 ;
            if ( pValue == & IsCjkExToLinshiDaima && *pValue ) IsLinshiDaimaToCjkEx = 0 ;
#endif
            goto READ_NEXT_LINE ;
         }
      }


      // 暥帤楍偺庢摼
      for ( int nCount = 0 ; nCount < OPTION_STRING_LIST_A_NELEM ; nCount ++ ) {

         char *szValue = ( OptionStringListA [ nCount ] ).szValue ;

         if ( ! GetOptionStringA ( szLineBuffer, ( OptionStringListA [ nCount ] ).szName, szValue, ( OptionStringListA [ nCount ] ).nMaxSize ) ) {

#if defined RTFCONV_EXE || defined CHNCONV_EXE || defined SETUP_R || defined SETUP_C
            if ( szValue == szBrowsePathA ) IsSavePath = 1 ;
#endif

#if defined RTFCONV_EXE || defined RTFCONV_COM || defined SETUP_R
            if ( szValue == szFontNameArrayDummy ) {

               if ( ! FontNameArray ) {
                  FontNameArray = (char*) malloc ( FONTNAME_ARRAY_SIZE ) ;
                  if ( ! FontNameArray ) return -1 ;
                  atexit ( FreeFontNameArray ) ;
                  memzero ( FontNameArray, FONTNAME_ARRAY_SIZE ) ;
               }

               char *szString ;
               errno = 0 ;
               int nIndex = strtoul ( szFontNameArrayDummy, & szString, 10 ) ;
               if ( ! errno && *szString && nIndex >= 0 && nIndex < UNICODE_BLOCK_NELEM ) {
                  szString ++ ;
                  while ( isascii ( *szString ) && isspace ( *szString ) ) szString ++ ;
                  strlcpy ( FontNameArray + nIndex * LF_FACESIZE, szString, LF_FACESIZE ) ;
               }
            }
#endif
            goto READ_NEXT_LINE ;
         }
      }


#if defined RTFCONV_EXE || defined CHNCONV_EXE || defined SETUP_R || defined SETUP_C
      // 暥帤楍偺庢摼 (UNICODE)
      for ( int nCount = 0 ; nCount < OPTION_STRING_LIST_W_NELEM ; nCount ++ ) {

         wchar_t *szValue = ( OptionStringListW [ nCount ] ).szValue ;

         if ( ! GetOptionWideString ( szLineBuffer, ( OptionStringListW [ nCount ] ).szName, szValue, ( OptionStringListW [ nCount ] ).nMaxSize ) ) {

#if defined RTFCONV_EXE || defined CHNCONV_EXE || defined SETUP_R || defined SETUP_C
            if ( szValue == szBrowsePathW ) IsSavePath = 1 ;
#endif
            goto READ_NEXT_LINE ;
         }
      }
#endif


#if defined RTFCONV_EXE || defined RTFCONV_COM || defined SETUP_R
      // 僶僀僫儕乕偺庢摼
      for ( int nCount = 0 ; nCount < OPTION_BINARY_LIST_NELEM ; nCount ++ ) {

         unsigned short *pValue = ( OptionBinaryList [ nCount ] ).pValue ;

         if ( ! GetOptionBinary ( szLineBuffer, ( OptionBinaryList [ nCount ] ).szName, pValue, ( OptionBinaryList [ nCount ] ).nSize ) ) {

#if ! defined SETUP_R
            if ( pValue == LangIdArray ) IsLangIdUserDefined = 1 ;
#endif
            goto READ_NEXT_LINE ;
         }
      }
#endif

      READ_NEXT_LINE :;
   }


   return 0 ;
}



// 奺峴偐傜悢抣偺僆僾僔儑儞傪庢摼
// 僆僾僔儑儞偺柤慜偑崌抳偟偨傜 0 傪曉偡
static int GetOptionInteger ( char *szString, const char *szName, int *pValue ) {

   if ( ! strheadicmp ( szString, szName ) ) {

      szString += strlen ( szName ) ;

      if ( isascii ( *szString ) && isspace ( *szString ) ) {

         while ( isascii ( *szString ) && isspace ( *szString ) ) szString ++ ;

         errno = 0 ;
         if ( *szString == '-' ) *pValue = strtol ( szString, & szString, 10 ) ;
         else                    *pValue = strtoul ( szString, & szString, 10 ) ;
         while ( errno ) *pValue = 0 ;

         return 0 ;
      }
   }

   return 1 ;
}



⌨️ 快捷键说明

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