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

📄 oemio.cpp

📁 This software performs code conversion of Chinese characters, including GB2312/GBK and BIG5. It a
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// 栠傝抣偼 fwrite 偲摨偠乮暥帤悢傪曉偡乯
size_t __cdecl oem_fwritews ( const wchar_t *string, size_t length, FILE *stream ) {

   if ( get_unicode_mode ( stream ) && ! get_text_mode ( stream ) ) return fwrite ( string, sizeof(wchar_t), length, stream ) / sizeof(wchar_t) ;

   return fwritews_internal ( string, length, stream ) ;
}



// 栠傝抣偼 fputws 偲摨偠
int __cdecl oem_fputws ( const wchar_t *string, FILE *stream ) {

   size_t length = wcslen ( string ) ;

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

   return 0 ;
}



// 栠傝抣偼 wprintf 偲摨偠
int __cdecl oem_wprintf ( const wchar_t *format,... ) {

   va_list args ;
   va_start ( args, format ) ;

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

   va_end ( args ) ;
   return retvalue ;
}



// 栠傝抣偼 fwprintf 偲摨偠
int __cdecl oem_fwprintf ( FILE *stream, const wchar_t *format,... ) {

   va_list args ;
   va_start ( args, format ) ;

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

   va_end ( args ) ;
   return retvalue ;
}



// 栠傝抣偼 vfwprintf 偲摨偠
int __cdecl oem_vfwprintf ( FILE *stream, const wchar_t *format, va_list args ) {

   if ( get_unicode_mode ( stream ) && ! get_text_mode ( stream ) ) return vfwprintf ( stream, format, args ) ;

   int retvalue = EOF ;

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

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

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

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

   if ( fwritews_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 vscwprintf ( const wchar_t *format, va_list args ) {

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

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

   fclose ( stdnul ) ;
   return retvalue ;
}
#endif



//////////////////////////////
//         撪晹娭悢         //
//////////////////////////////



static int fputwc_straight ( int c, FILE *stream ) ;
static inline void fseterror ( FILE *stream ) ;



// 弌椡偟偨暥帤悢傪曉偡
static size_t fwrites_internal ( const char *string, size_t count, FILE *stream ) {

   size_t count_saved = count ;

   for ( ; count ; string ++, count -- ) {

      wchar_t wsz [ 1 ] ;
      int length = 1 ;

      if ( isascii ( *string ) ) {     // 崅懍壔偺偨傔
         wsz [ 0 ] = (unsigned char) *string ;
      }
      else {
         if ( ismbblead ( (unsigned char) *string ) && *( string + 1 ) ) length = 2 ;
         if ( ! MultiByteToWideChar ( CP_ACP, 0, string, length, wsz, 1 ) ) break ;
      }

      if ( ! fwritews_internal ( wsz, 1, stream ) ) break ;
      if ( length > 1 ) string ++, count -- ;
   }

   return count_saved - count ;
}



// 弌椡偟偨暥帤悢傪曉偡
static size_t fwritews_internal ( const wchar_t *string, size_t count, FILE *stream ) {

   int codepage = CP_ACP ;

   if ( fisatty ( stream ) ) {
      if ( IsNT () ) {

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

         unsigned long dwDummy ;

         if ( GetConsoleMode ( hConsole, & dwDummy ) ) {

            fflush ( stream ) ;
            size_t count_saved = count ;

            while ( count ) {
               unsigned long dwCharToWrite = ( count == (size_t) (unsigned char) count ) ? (unsigned char) count : ( UCHAR_MAX + 1 ) ;
               unsigned long dwCharWritten = 0 ;
               if ( ! WriteConsoleW ( hConsole, string, dwCharToWrite, & dwCharWritten, NULL ) || ! dwCharWritten || count < dwCharWritten ) {
                  fseterror ( stream ) ;
                  break ;
               }
               count -= dwCharWritten ;
               string += dwCharWritten ;
            }

            return count_saved - count ;
         }

      }
      codepage = GetConsoleOutputCP () ;
   }


   if ( get_unicode_mode ( stream ) ) {

      size_t count_saved = count ;
      int is_output_bom = 0 ;
      int is_text_mode = get_text_mode ( stream ) ;

      if ( is_text_mode && stream->_base == stream->_ptr ) {

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

         // pipe
         if ( GetFileType ( hFile ) == FILE_TYPE_PIPE ) {
            if ( get_bom_mode ( stream ) ) is_output_bom = 1 ;
            set_bom_mode ( stream, 0 ) ;
         }
         // file
         else {
            if ( ! ftell64 ( stream ) ) is_output_bom = 1 ;
         }

      }

      if ( is_output_bom && fputwc_straight ( BYTE_ORDER_MARK, stream ) < 0 ) return 0 ;

      for ( ; count ; string ++, count -- ) {
         int c = *string ;
         if ( is_text_mode ) {
            if ( c == BYTE_ORDER_MARK || c == WEOF ) continue ;
            if ( c == '\n' ) fputwc_straight ( '\r', stream ) ;
         }
         if ( fputwc_straight ( c, stream ) < 0 ) break ;
      }

      return count_saved - count ;
   }


   size_t count_saved = count ;

   for ( ; count ; string ++, count -- ) {

      char sz [ MB_LEN_MAX ] ;
      int length ;

      if ( isascii ( *string ) ) {     // 崅懍壔偺偨傔
         sz [ 0 ] = (unsigned char) *string ;
         length = 1 ;
      }
      else if ( IS_DETECT_SURROGATE && IsHighSurrogate ( *string ) ) {
         if ( ! ( length = WideCharToMultiByte ( codepage, 0, string, 2, sz, MB_LEN_MAX, NULL, NULL ) ) ) break ;
         if ( length == 2 && sz [ 0 ] == '?' && sz [ 1 ] == '?' ) length = 1 ;
         string ++, count -- ;
      }
      else {
         if ( ! ( length = WideCharToMultiByte ( codepage, 0, string, 1, sz, MB_LEN_MAX, NULL, NULL ) ) ) break ;
      }

      for ( int i = 0 ; i < length ; i ++ ) {
         if ( fputc ( (unsigned char) sz [ i ], stream ) < 0 ) goto EXIT_LOOP ;
      }
   }

   EXIT_LOOP :

   return count_saved - count ;
}



//////////////////////////////
//      擖椡儖乕僥傿儞      //
//////////////////////////////



// 僥僉僗僩儌乕僪   亄 Ansi 儌乕僪   丗CR+LF仺LF, CTRL-Z 偱僼傽僀儖廔抂
//                  亄 Console 儌乕僪丗CR+LF仺LF, 峴摢偺 CTRL-Z 偱僼傽僀儖廔抂
//                  亄 Unicode 儌乕僪丗CR+LF仺LF, 忢偵 BOM 傪柍帇
// 僶僀僫儕乕儌乕僪 亄 Ansi 儌乕僪   丗CR+LF仺儅儅, CTRL-Z 偼儅儅
//                  亄 Console 儌乕僪丗CR+LF仺儅儅, 峴摢偺 CTRL-Z 偱僼傽僀儖廔抂
//                  亄 Unicode 儌乕僪丗CR+LF仺儅儅, BOM 偼儅儅



static size_t freads_internal ( char *string, size_t count, int stop_char, FILE *stream ) ;
static size_t freadws_internal ( wchar_t *string, size_t count, int stop_char, FILE *stream ) ;
static int fgetc_internal ( FILE *stream, int is_unicode_mode, int codepage ) ;
static int fgetwc_internal ( FILE *stream, int is_unicode_mode, int codepage ) ;

#define CTRLZ     0x1A

#undef fgets
#undef fgetws



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



// 栠傝抣偼 fread 偲摨偠乮僶僀僩悢傪曉偡乯
// 偨偩偟僥僉僗僩儌乕僪傑偨偼俀僶僀僩娐嫬偱偼侾乣俀暥帤彮側偄偙偲偁傞
// 僶僢僼傽嫬奅偱俀僶僀僩暥帤傪俀偮偵妱傞偙偲偼側偄
// 僥僉僗僩儌乕僪偱偼僰儖暥帤傪嶍彍偡傞
size_t __cdecl oem_freads ( char *string, size_t count, FILE *stream ) {
   return freads_internal ( string, count, EOF, stream ) ;
}



// 栠傝抣偼 fgets 偲摨偠
// 偨偩偟僥僉僗僩儌乕僪傑偨偼俀僶僀僩娐嫬偱偼侾乣俀暥帤彮側偄偙偲偁傞
// 僶僢僼傽嫬奅偱俀僶僀僩暥帤傪俀偮偵妱傞偙偲偼側偄
// 僥僉僗僩儌乕僪偱偼僰儖暥帤傪嶍彍偡傞
char *__cdecl oem_fgets ( char *string, int count, FILE *stream ) {

   if ( count <= 0 ) return NULL ;

   size_t retvalue = freads_internal ( string, count - 1, '\n', stream ) ;
   string [ retvalue ] = 0 ;

   if ( ! retvalue && feof ( stream ) ) return NULL ;
   return string ;
}



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



// 栠傝抣偼 fread 偲摨偠乮儚乕僪悢傪曉偡乯
// 偨偩偟僥僉僗僩儌乕僪偱偼侾暥帤彮側偄偙偲偁傞
// 僥僉僗僩儌乕僪偱偼僰儖暥帤傪嶍彍偡傞
size_t __cdecl oem_freadws ( wchar_t *string, size_t count, FILE *stream ) {
   return freadws_internal ( string, count, EOF, stream ) ;
}



// 栠傝抣偼 fgetws 偲摨偠
// 偨偩偟僥僉僗僩儌乕僪偱偼侾暥帤彮側偄偙偲偁傞
// 僥僉僗僩儌乕僪偱偼僰儖暥帤傪嶍彍偡傞
wchar_t *__cdecl oem_fgetws ( wchar_t *string, int count, FILE *stream ) {

   if ( count <= 0 ) return NULL ;

   size_t retvalue = freadws_internal ( string, count - 1, '\n', stream ) ;
   string [ retvalue ] = 0 ;

   if ( ! retvalue && feof ( stream ) ) return NULL ;
   return string ;
}



//////////////////////////////
//         撪晹娭悢         //
//////////////////////////////



#define getmbcp _getmbcp   // if single-byte cp, then return 0
static inline void fseteof ( FILE *stream ) ;



// 擖椡偟偨暥帤悢傪曉偡
static size_t freads_internal ( char *string, size_t count, int stop_char, FILE *stream ) {

   int is_unicode_mode = get_unicode_mode ( stream ) ;
   int is_text_mode = is_unicode_mode ? get_text_mode ( stream ) : 0 ;

   int codepage = CP_ACP ;
   if ( fisatty ( stream ) && GetACP () != GetConsoleCP () ) codepage = GetConsoleCP () ;


   char *current = string ;
   char *end = string + count ;
   int is_first_in_line = 1 ;


   // 僐儞僜乕儖側傜堦帪揑偵僶僀僫儕乕儌乕僪偵
   int old_mode = 0 ;
   if ( fisatty ( stream ) ) {
      old_mode = fsetmode ( stream, _O_BINARY ) ;
      if ( old_mode == _O_TEXT ) is_text_mode = 1 ;
   }


   // \r 傑偨偼 trail byte 傪彂偒崬傓応崌偵旛偊偰
   if ( is_text_mode ) end -- ;
   if ( getmbcp () ) end -- ;

⌨️ 快捷键说明

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