From 54ee447c368627d83919adb0dcb6b563b8bd8220 Mon Sep 17 00:00:00 2001 From: Jiri Malak Date: Sat, 23 Jun 2018 23:03:16 +0200 Subject: [PATCH] cleanup wipfc code --- bld/wipfc/cpp/ipffile.cpp | 70 ++++++++++++++++++--------------------- bld/wipfc/cpp/ipffile.hpp | 1 + 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/bld/wipfc/cpp/ipffile.cpp b/bld/wipfc/cpp/ipffile.cpp index 93ae157c2c..e68ee3c7b6 100644 --- a/bld/wipfc/cpp/ipffile.cpp +++ b/bld/wipfc/cpp/ipffile.cpp @@ -40,41 +40,37 @@ IpfFile::IpfFile( const std::wstring* fname ) : IpfData(), fileName ( fname ), { std::string buffer; wtomb_string( *fname, buffer ); - if(( stream = std::fopen( buffer.c_str(), "rb" ) ) == 0) + if( (stream = std::fopen( buffer.c_str(), "rb" )) == 0 ) { throw FatalIOError( ERR_OPEN, *fileName ); + } } /*****************************************************************************/ //Read a character //Returns EOB if end-of-file reached std::wint_t IpfFile::get() { -#if defined( __UNIX__ ) || defined( __APPLE__ ) - std::wint_t ch( 0 ); + std::wint_t ch; + if( ungotten ) { ch = ungottenChar; ungotten = false; + } else { + ch = read_wchar(); + } + if( ch == L'\r' ) { + ch = read_wchar(); } - else - ch = std::fgetwc( stream ); - if( ch == L'\r' ) - ch = std::fgetwc( stream ); -#else - //can't use OW's fgetwc because it always reads 2 bytes in binary mode - std::wint_t ch( readMBChar() ); - if( ch == L'\r' ) - ch = readMBChar(); -#endif incCol(); if( ch == L'\n' ) { incLine(); resetCol(); - } - else if( ch == WEOF ) { + } else if( ch == WEOF ) { ch = EOB; - if( !std::feof( stream ) ) + if( !std::feof( stream ) ) { throw FatalIOError( ERR_READ, *fileName ); + } } - return ch; + return( ch ); } /*****************************************************************************/ void IpfFile::unget( wchar_t ch ) @@ -83,30 +79,30 @@ void IpfFile::unget( wchar_t ch ) ungottenChar = ch; ungotten = true; decCol(); - if( ch == L'\n' ) + if( ch == L'\n' ) { decLine(); + } } /*****************************************************************************/ -#if !defined( __UNIX__ ) && !defined( __APPLE__ ) -std::wint_t IpfFile::readMBChar() +std::wint_t IpfFile::read_wchar() { - wchar_t ch = 0; - if( ungotten ) { - ch = ungottenChar; - ungotten = false; - } else { - char mbc[ MB_LEN_MAX ]; - if( std::fread( &mbc[0], sizeof( char ), 1, stream ) != 1 ) - return WEOF; - if( _ismbblead( mbc[0] ) ) { - if( std::fread( &mbc[1], sizeof( char ), 1, stream ) != 1 ) { - return WEOF; - } - } - if( mbtow_char( &ch, mbc, MB_CUR_MAX ) < 0 ) { - throw FatalError( ERR_T_CONV ); + wchar_t ch; + +#if defined( __UNIX__ ) || defined( __APPLE__ ) + // TODO! read MBCS character and convert it to UNICODE by mbtow_char + ch = std::fgetwc( stream ); +#else + char mbc[ MB_LEN_MAX ]; + if( std::fread( &mbc[0], sizeof( char ), 1, stream ) != 1 ) + return( WEOF ); + if( _ismbblead( mbc[0] ) ) { + if( std::fread( &mbc[1], sizeof( char ), 1, stream ) != 1 ) { + return( WEOF ); } } - return ch; -} + if( mbtow_char( &ch, mbc, MB_CUR_MAX ) < 0 ) { + throw FatalError( ERR_T_CONV ); + } #endif + return( ch ); +} diff --git a/bld/wipfc/cpp/ipffile.hpp b/bld/wipfc/cpp/ipffile.hpp index 37d8e45294..b15b01c4ee 100644 --- a/bld/wipfc/cpp/ipffile.hpp +++ b/bld/wipfc/cpp/ipffile.hpp @@ -62,6 +62,7 @@ class IpfFile : public IpfData { private: IpfFile( const IpfFile& rhs ); //no copy IpfFile& operator=( const IpfFile& rhs ); //no assignment + std::wint_t read_wchar(); const std::wstring* fileName; std::FILE* stream; wchar_t ungottenChar;