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

📄 oemio.cpp

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

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <limits.h>
#include <mbctype.h>
#include "msc.h"
#include "oemio.h"


// Unicode 偲 OEM 偵懳墳偟偨僗僩儕乕儉擖弌椡張棟

// \n 仺 \r\n 曄姺傗 CTRL-Z 傊偺懳墳偼
// ANSI/OEM 僗僩儕乕儉側傜儔僀僽儔儕乕偵張棟偝偣傞
// Unicode 僗僩儕乕儉側傜帺慜偱張棟偡傞


// fwide () 偺懳墳忬嫷
//    "r", "r+" 偼懳墳
//    "w", "w+" 偼懳墳
//    "a", "a+" 偼枹懳墳
//    stdin, stdout, stderr 偺僐儞僜乕儖擖弌椡偼懳墳乮壗傕偟側偄乯
//    stdout, stderr 偺儕僟僀儗僋僩 ">" 偼懳墳
//    stdout, stderr 偺捛壛儕僟僀儗僋僩 ">>" 偼懳墳
//    stdin 偺儕僟僀儗僋僩 "<" 偼懳墳
//    僷僀僾偼懳墳


// 儊儌
//
// fopen() 偼 "r+", "a+" 偱僼傽僀儖傪奐偔偲偒丄僥僉僗僩僼傽僀儖側傜丄僼傽僀儖廔抂偺暥帤傪挷傋丄
// ^Z 側傜丄偦傟傪彍嫀偡傞丅僼傽僀儖傪奐偔偲偒偵 "b" 偑巜掕偝傟偰偄傟偽丄偙偺摦嶌傪夞旔偱偒傞丅
// 偁偲偱 _setmode() 偱儌乕僪傪曄峏偡傞偐偳偆偐偼丄偙偺摦嶌偵娭學偑側偄丅Unicode 偼俀僶僀僩栚
// 偵 ^Z 偑偔傞偙偲偑偁傞偺偱丄 "r+", "a+" 偺巊梡偵偼拲堄偡傞昁梫偑偁傞丅


#define IS_DETECT_SURROGATE   0



//////////////////////////////
//        撪晹僨乕僞        //
//////////////////////////////
#if defined _MSC_VER || defined __DMC__ || defined __WATCOMC__ || defined __GNUC__


#define OEM_STREAM_UNICODE_MODE  0x00010000
#define OEM_STREAM_TEXT_MODE     0x00020000
#define OEM_STREAM_BOM_MODE      0x00040000     // only for pipe


#define get_unicode_mode(stream)       ( stream->_flag & OEM_STREAM_UNICODE_MODE )
#define set_unicode_mode(stream,mode)  ( (mode) ? ( stream->_flag |= OEM_STREAM_UNICODE_MODE ) : ( stream->_flag &= ~ OEM_STREAM_UNICODE_MODE ) )

#define get_text_mode(stream)          ( stream->_flag & OEM_STREAM_TEXT_MODE )
#define set_text_mode(stream,mode)     ( (mode) ? ( stream->_flag |= OEM_STREAM_TEXT_MODE ) : ( stream->_flag &= ~ OEM_STREAM_TEXT_MODE ) )

#define get_bom_mode(stream)           ( stream->_flag & OEM_STREAM_BOM_MODE )
#define set_bom_mode(stream,mode)      ( (mode) ? ( stream->_flag |= OEM_STREAM_BOM_MODE ) : ( stream->_flag &= ~ OEM_STREAM_BOM_MODE ) )


#else // defined _MSC_VER


// not supported, dummy

static int get_unicode_mode ( FILE *stream ) { return 0 ; }
static int set_unicode_mode ( FILE *stream, int mode ) { return 0 ; }

static int get_text_mode ( FILE *stream ) { return 0 ; }
static int set_text_mode ( FILE *stream, int mode ) { return 0 ; }

static int get_bom_mode ( FILE *stream ) { return 0 ; }
static int set_bom_mode ( FILE *stream, int mode ) { return 0 ; }


#endif // else of defined _MSC_VER



//////////////////////////////
//        撪晹僨乕僞        //
//////////////////////////////


#if defined _MSC_VER
#define is_read_stream(stream)   ( stream->_flag & ( _IOREAD | _IORW ) )
#else
#define is_read_stream(stream)   0
#endif



//////////////////////////////
//         嫟捠娭悢         //
//////////////////////////////



static int fsetmode_unicode ( FILE *stream, int mode ) ;
static int fgetmode_unicode ( FILE *stream ) ;
static int fgetwc_straight ( FILE *stream ) ;

// 偹傫偺偨傔 FILE_TYPE_REMOTE (0x8000) 傪偼偢偡
#define GetFileType(h)   ( GetFileType ( h ) & ~ FILE_TYPE_REMOTE )



// 僗僩儕乕儉偺 Unicode 儌乕僪傪愝掕/庢摼偡傞
// mode 偑 晧悢 側傜 Ansi 儌乕僪偵
//         惓悢 側傜 Unicode 儌乕僪偵
//         0 側傜 尰嵼偺儌乕僪傪庢摼
// 栠傝抣偼丄Ansi 儌乕僪側傜晧悢傪曉偡
//           Unicode 儌乕僪側傜惓悢傪曉偡
// 撉傒崬傒偱 mode 偑 0 埲奜側傜丄mode 傪柍帇偟丄僼傽僀儖愭摢偐傜敾抐偡傞
// 僼傽僀儖偺愭摢偱偺傒巜掕偱偒傞乮偦傟埲奜側傜壗傕偟側偄乯
int fwide ( FILE *stream, int mode ) {

   int result ;

   if ( mode ) result = fsetmode_unicode ( stream, ( mode > 0 ) ? 1 : 0 ) ;
   else        result = fgetmode_unicode ( stream ) ;

   return result ? 1 : -1 ;
}



// 僗僩儕乕儉傪 Unicode 儌乕僪傪愝掕偡傞
// mode 偑 0 埲奜側傜 Unicode 儌乕僪偵丄0 側傜 Ansi 儌乕僪偵
// Unicode 儌乕僪側傜 0 埲奜傪丄Ansi 儌乕僪側傜 0 傪曉偡
static int fsetmode_unicode ( FILE *stream, int mode ) {

   if ( fisatty ( stream ) ) return IsNT () ;
   if ( stream->_base != stream->_ptr ) return get_unicode_mode ( stream ) ;


   // "r", "r+", "w+"
   if ( is_read_stream ( stream ) ) {

      HANDLE hFile = fget_osfhandle ( stream ) ;
      if ( hFile == INVALID_HANDLE_VALUE ) return 0 ;

      // pipe
      if ( GetFileType ( hFile ) == FILE_TYPE_PIPE ) {

         wchar_t wsz [ 1 ] ;
         unsigned long dwByte ;
         int result ;

         while ( ( result = PeekNamedPipe ( hFile, wsz, sizeof(wchar_t), & dwByte, NULL, NULL ) ) && ! dwByte ) Sleep ( 100 ) ;

         if ( result && dwByte == sizeof(wchar_t) && *wsz == BYTE_ORDER_MARK ) mode = 1 ;
         else                                                                  mode = 0 ;

      }
      // file
      else {

         if ( ftell64 ( stream ) ) return get_unicode_mode ( stream ) ;
         fseek64 ( stream, 0, SEEK_SET ) ;

         int oldmode = fsetmode ( stream, _O_BINARY ) ;

         int c = fgetwc_straight ( stream ) ;
         if ( c == BYTE_ORDER_MARK ) mode = 1 ;
         else if ( c != EOF )        mode = 0 ;

         fseek64 ( stream, 0, SEEK_SET ) ;
         fsetmode ( stream, oldmode ) ;
      }

   }


   int oldmode = get_unicode_mode ( stream ) ;
   set_unicode_mode ( stream, mode ) ;
   mode = get_unicode_mode ( stream ) ;

   if ( mode != oldmode ) {

      if ( mode ) {
         if ( fsetmode ( stream, _O_BINARY ) == _O_TEXT ) {
            set_text_mode ( stream, 1 ) ;
            set_bom_mode ( stream, 1 ) ;
         }
         else {
            set_text_mode ( stream, 0 ) ;
            set_bom_mode ( stream, 0 ) ;
         }
      }
      else {
         if ( get_text_mode ( stream ) ) fsetmode ( stream, _O_TEXT ) ;
         set_unicode_mode ( stream, 0 ) ;
         set_text_mode ( stream, 0 ) ;
         set_bom_mode ( stream, 0 ) ;
      }

   }


   return mode ;
}



// 僗僩儕乕儉偺 Unicode 儌乕僪傪庢摼偡傞
// Unicode 儌乕僪側傜 0 埲奜傪丄Ansi 儌乕僪側傜 0 傪曉偡
static int fgetmode_unicode ( FILE *stream ) {

   if ( fisatty ( stream ) ) return IsNT () ;

   return get_unicode_mode ( stream ) ;
}



//////////////////////////////
//      弌椡儖乕僥傿儞      //
//////////////////////////////



// 僥僉僗僩儌乕僪   亄 Ansi 儌乕僪   丗LF仺CR+LF
//                  亄 Console 儌乕僪丗LF仺CR+LF
//                  亄 Unicode 儌乕僪丗LF仺CR+LF, 愭摢偵 BOM 傪弌椡
// 僶僀僫儕乕儌乕僪 亄 Ansi 儌乕僪   丗LF仺儅儅
//                  亄 Console 儌乕僪丗LF仺儅儅
//                  亄 Unicode 儌乕僪丗LF仺儅儅, 愭摢偵 BOM 傪弌椡偣偢



#if ! defined _MSC_VER || _MSC_VER < 1300
static int __cdecl vscprintf ( const char *format, va_list args ) ;
static int __cdecl vscwprintf ( const wchar_t *format, va_list args ) ;
#else
#define vscprintf _vscprintf
#define vscwprintf _vscwprintf
#endif


static size_t fwrites_internal ( const char *string, size_t count, FILE *stream ) ;
static size_t fwritews_internal ( const wchar_t *string, size_t count, FILE *stream ) ;

#define TMP_BUFFER_SIZE    ( MAX_PATH + 0x10 )


#undef printf
#undef fprintf
#undef vfprintf
#undef fputs

#undef wprintf
#undef fwprintf
#undef vfwprintf
#undef fputws



//////////////////////////////
//          ANSI斉          //
//////////////////////////////



// 栠傝抣偼 fwrite 偲摨偠乮暥帤悢傪曉偡乯
size_t __cdecl oem_fwrites ( const char *string, size_t length, FILE *stream ) {

   if ( fisatty ( stream ) ? GetACP () == GetConsoleOutputCP () : ! get_unicode_mode ( stream ) ) return fwrite ( string, sizeof(char), length, stream ) / sizeof(char) ;

   return fwrites_internal ( string, length, stream ) ;
}



// 栠傝抣偼 fputs 偲摨偠
int __cdecl oem_fputs ( const char *string, FILE *stream ) {

   size_t length = strlen ( string ) ;

   if ( oem_fwrites ( string, length, stream ) != length ) return EOF ;

   return 0 ;
}



// 栠傝抣偼 printf 偲摨偠
int __cdecl oem_printf ( const char *format,... ) {

   va_list args ;
   va_start ( args, format ) ;

   int retvalue = oem_vfprintf ( stdout, format, args ) ;

   va_end ( args ) ;
   return retvalue ;
}



// 栠傝抣偼 fprintf 偲摨偠
int __cdecl oem_fprintf ( FILE *stream, const char *format,... ) {

   va_list args ;
   va_start ( args, format ) ;

   int retvalue = oem_vfprintf ( stream, format, args ) ;

   va_end ( args ) ;
   return retvalue ;
}



// 栠傝抣偼 vfprintf 偲摨偠
int __cdecl oem_vfprintf ( FILE *stream, const char *format, va_list args ) {

   if ( fisatty ( stream ) ? GetACP () == GetConsoleOutputCP () : ! get_unicode_mode ( stream ) ) return vfprintf ( stream, format, args ) ;

   int retvalue = EOF ;

   va_list args1 ;
   va_list args2 ;
   va_copy ( args1, args ) ;
   va_copy ( args2, args ) ;

   char buffer [ TMP_BUFFER_SIZE ] ;
   char *string = NULL ;
   int result ;

   if ( ( result = vsnprintf ( buffer, TMP_BUFFER_SIZE, format, args ) ) < 0 || result > TMP_BUFFER_SIZE - 1 ) {

      int length ;
      if ( ( length = vscprintf ( format, args1 ) ) < 0 ) goto CAUGHT_ERROR ;
      if ( ! ( string = (char*) malloc ( ( length + 1 ) * sizeof(char) ) ) ) goto CAUGHT_ERROR ;
      if ( ( result = vsnprintf ( string, length + 1, format, args2 ) ) < 0 || result > length ) goto CAUGHT_ERROR ;
   }

   if ( fwrites_internal ( string ? string : buffer, result, stream ) == (size_t) result ) retvalue = result ;

   CAUGHT_ERROR :
   free ( string ) ;
   va_end ( args1 ) ;
   va_end ( args2 ) ;
   return retvalue ;
}



#if ! defined _MSC_VER || _MSC_VER < 1300
// Printf曄姺偺弌椡偵昁梫側僶僢僼傽偺挿偝傪庢摼
// 暥帤悢傪曉偡乮廔抂偺 NULL 傪娷傑側偄乯
static int __cdecl vscprintf ( const char *format, va_list args ) {

   FILE *stdnul = fopen ( "NUL", "wb" ) ;
   if ( ! stdnul ) return EOF ;

   int retvalue = vfprintf ( stdnul, format, args ) ;

   fclose ( stdnul ) ;
   return retvalue ;
}
#endif



//////////////////////////////
//         UNICODE斉        //
//////////////////////////////



⌨️ 快捷键说明

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