Skip to content

Commit

Permalink
cleanup wipfc code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalak committed Jun 23, 2018
1 parent 57c7f1d commit 54ee447
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 37 deletions.
70 changes: 33 additions & 37 deletions bld/wipfc/cpp/ipffile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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 );
}
1 change: 1 addition & 0 deletions bld/wipfc/cpp/ipffile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 54ee447

Please sign in to comment.