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

📄 oemio.cpp

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

   while ( current < end ) {

      int c ;

      if ( ( c = fgetc_internal ( stream, is_unicode_mode, codepage ) ) < 0 ) break ;

      if ( c == CTRLZ && is_first_in_line && fisatty ( stream ) ) {
         while ( ( c = fgetc_internal ( stream, is_unicode_mode, codepage ) ) >= 0 && c != '\n' ) ;
         fseteof ( stream ) ;
         break ;
      }

      if ( is_text_mode ) {
         if ( c == '\r' ) {
            c = fgetc_internal ( stream, is_unicode_mode, codepage ) ;
            if ( c != '\n' ) *current ++ = '\r' ;
            if ( c < 0 ) break ;
         }
         if ( c == 0 ) continue ;
      }

      if ( c > 0x100 ) {
         *current ++ = (unsigned char) ( c >> 8 ) ;
         *current ++ = (unsigned char) c ;
      }
      else {
         *current ++ = c ;
      }

      if ( c == '\n' ) is_first_in_line = 1 ;
      else             is_first_in_line = 0 ;

      if ( c == stop_char ) break ;
   }


   if ( fisatty ( stream ) ) fsetmode ( stream, old_mode ) ;
   return current - string ;
}



// 擖椡偟偨暥帤悢傪曉偡
static size_t freadws_internal ( wchar_t *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 () ;


   wchar_t *current = string ;
   wchar_t *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 傪彂偒崬傓応崌偵旛偊偰
   if ( is_text_mode ) end -- ;


   while ( current < end ) {

      int c ;

      if ( ( c = fgetwc_internal ( stream, is_unicode_mode, codepage ) ) < 0 ) break ;

      if ( c == CTRLZ && is_first_in_line && fisatty ( stream ) ) {
         while ( ( c = fgetwc_internal ( stream, is_unicode_mode, codepage ) ) >= 0 && c != '\n' ) ;
         fseteof ( stream ) ;
         break ;
      }

      if ( is_text_mode ) {
         if ( c == '\r' ) {
            c = fgetwc_internal ( stream, is_unicode_mode, codepage ) ;
            if ( c != '\n' ) *current ++ = '\r' ;
            if ( c < 0 ) break ;
         }
         if ( c == 0 ) continue ;
         if ( c == BYTE_ORDER_MARK || c == WEOF ) continue ;
      }

      *current ++ = c ;

      if ( c == '\n' ) is_first_in_line = 1 ;
      else             is_first_in_line = 0 ;

      if ( c == stop_char ) break ;
   }


   if ( fisatty ( stream ) ) fsetmode ( stream, old_mode ) ;
   return current - string ;
}



// 惉岟偟偨傜暥帤傪丄幐攕偟偨傜 EOF 傪曉偡
static int fgetc_internal ( FILE *stream, int is_unicode_mode, int codepage ) {

   if ( ! is_unicode_mode && codepage == CP_ACP ) {

      int c ;
      if ( ( c = fgetc ( stream ) ) < 0 ) return EOF ;

      if ( ismbblead ( c ) ) {
         int c2 ;
         if ( ( c2 = fgetc ( stream ) ) < 0 ) return EOF ;
         c = ( c << 8 ) + c2 ;
      }

      return c ;
   }
   else {

      char sz [ MB_LEN_MAX ] ;
      wchar_t wsz [ 1 ] ;
      int c ;

      while ( ( c = fgetwc_internal ( stream, is_unicode_mode, codepage ) ) == BYTE_ORDER_MARK || c == WEOF ) ;

      if ( c < 0 ) return EOF ;

      if ( isascii ( c ) ) return c ;     // 崅懍壔偺偨傔

      wsz [ 0 ] = c ;

      int size = WideCharToMultiByte ( CP_ACP, 0, wsz, 1, sz, MB_LEN_MAX, NULL, NULL ) ;
      if ( ! size ) return EOF ;

      if ( size == 2 ) {
         int c1 = (unsigned char) *sz ;
         int c2 = (unsigned char) *( sz + 1 ) ;
         return ( c1 << 8 ) + c2 ;
      }
      else {
         return (unsigned char) *sz ;
      }
   }
}



// 惉岟偟偨傜暥帤傪丄幐攕偟偨傜 EOF 傪曉偡
static int fgetwc_internal ( FILE *stream, int is_unicode_mode, int codepage ) {

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

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

         unsigned long dwDummy ;

         if ( GetConsoleMode ( hConsole, & dwDummy ) ) {

            wchar_t wsz [ 1 ] ;
            unsigned long dwCharRead = 0 ;

            if ( ReadConsoleW ( hConsole, wsz, 1, & dwCharRead, NULL ) && dwCharRead ) return *wsz ;

            fseterror ( stream ) ;
            return EOF ;
         }

      }
   }

   if ( is_unicode_mode ) {

      return fgetwc_straight ( stream ) ;

   }
   else {

      char sz [ MB_LEN_MAX ] ;
      wchar_t wsz [ 1 ] ;
      int length ;
      int c ;

      if ( ( c = fgetc ( stream ) ) < 0 ) return EOF ;

      if ( isascii ( c ) ) return c ;     // 崅懍壔偺偨傔

      if ( IsDBCSLeadByteEx ( codepage, c ) ) {
         sz [ 0 ] = c ;
         if ( ( c = fgetc ( stream ) ) < 0 ) return EOF ;
         sz [ 1 ] = c ;
         length = 2 ;
      }
      else {
         sz [ 0 ] = c ;
         length = 1 ;
      }

      int size = MultiByteToWideChar ( codepage, 0, sz, length, wsz, 1 ) ;
      if ( ! size ) return EOF ;

      return *wsz ;
   }
}



#ifdef __BORLANDC__
#define _flag  flags
#define _IOEOF _F_EOF
#define _IOERR _F_ERR
#endif

#ifdef __WATCOMC__
#define _IOEOF _EOF
#define _IOERR _SFERR
#endif



static inline void fseteof ( FILE *stream ) {
   stream->_flag |= _IOEOF ;
}



static inline void fseterror ( FILE *stream ) {
   stream->_flag |= _IOERR ;
}



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



// 惉岟偟偨傜暥帤傪丄幐攕偟偨傜 EOF 傪曉偡
static int fputwc_straight ( int c, FILE *stream ) {
   if ( fputc ( (unsigned char) c, stream ) < 0 ) return EOF ;
   if ( fputc ( (unsigned char)( c >> 8 ), stream ) < 0 ) return EOF ;
   return c ;
}



// 惉岟偟偨傜暥帤傪丄幐攕偟偨傜 EOF 傪曉偡
static int fgetwc_straight ( FILE *stream ) {
   int c1, c2 ;
   if ( ( c1 = fgetc ( stream ) ) < 0 ) return EOF ;
   if ( ( c2 = fgetc ( stream ) ) < 0 ) return EOF ;
   return ( c2 << 8 ) + c1 ;
}



//////////////////////////////////////
//       僐儞僜乕儖偵侾峴昞帵       //
//////////////////////////////////////



// 僐儞僜乕儖偵侾峴偺傒昞帵
// 僐儞僜乕儖偱側偗傟偽壗傕偟側偄
// 昞帵偟偨暥帤悢傪曉偡
int fputs_line ( const char *string, FILE *stream ) {

   if ( ! fisatty ( stream ) ) return 0 ;

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

   CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo ;
   if ( ! GetConsoleScreenBufferInfo ( hConsole, & ConsoleInfo ) ) return 0 ;
   int width = ConsoleInfo.dwSize.X - ConsoleInfo.dwCursorPosition.X ;

   const char *current = string ;
   const char *start = string ;

   while ( *current ) {

      int char_width = 1 ;
      int isdoublebyte = ismbblead ( *current ) ;

      if ( *current == '\n' || *current == '\f' || *current == '\v' ) break ;
      if ( *current == '\t' ) char_width = 8 ;
      if ( *current == '\r' || *current == '\b' || *current == '\a' ) char_width = 0 ;
      if ( isdoublebyte ) char_width = 2 ;

      width -= char_width ;

      if ( width < 1 ) {

         fwrites ( string, current - string, stream ) ;
         string = current ;

         CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo ;
         if ( ! GetConsoleScreenBufferInfo ( hConsole, & ConsoleInfo ) ) break ;
         width = ConsoleInfo.dwSize.X - ConsoleInfo.dwCursorPosition.X ;
         width -= char_width ;

         if ( width < 1 ) break ;
      }

      if ( isdoublebyte && *( current + 1 ) ) current ++ ;
      current ++ ;
   }

   if ( width >= 1 ) fwrites ( string, current - string, stream ) ;

   return (int) ( current - start ) ;
}



// 僐儞僜乕儖偵侾峴偺傒昞帵
// 僐儞僜乕儖偱側偗傟偽壗傕偟側偄
// 昞帵偟偨暥帤悢傪曉偡
int fputws_line ( const wchar_t *string, FILE *stream ) {

   if ( ! fisatty ( stream ) ) return 0 ;

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

   CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo ;
   if ( ! GetConsoleScreenBufferInfo ( hConsole, & ConsoleInfo ) ) return 0 ;
   int width = ConsoleInfo.dwSize.X - ConsoleInfo.dwCursorPosition.X ;

   const wchar_t *current = string ;
   const wchar_t *start = string ;

   while ( *current ) {

      int char_width = 1 ;
      int isdoublebyte = ! isascii ( *current ) ;

      if ( *current == '\n' || *current == '\f' || *current == '\v' ) break ;
      if ( *current == '\t' ) char_width = 8 ;
      if ( *current == '\r' || *current == '\b' || *current == '\a' ) char_width = 0 ;
      if ( isdoublebyte ) char_width = 2 ;

      width -= char_width ;

      if ( width < 1 ) {

         fwritews ( string, current - string, stream ) ;
         string = current ;

         CONSOLE_SCREEN_BUFFER_INFO ConsoleInfo ;
         if ( ! GetConsoleScreenBufferInfo ( hConsole, & ConsoleInfo ) ) break ;
         width = ConsoleInfo.dwSize.X - ConsoleInfo.dwCursorPosition.X ;
         width -= char_width ;

         if ( width < 1 ) break ;
      }

      current ++ ;
   }

   if ( width >= 1 ) fwritews ( string, current - string, stream ) ;

   return (int) ( current - start ) ;
}



⌨️ 快捷键说明

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