From f4dec89dc35a06584d3a9216f89a97ddb87c3b8c Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 15 Nov 2018 03:53:55 -0800 Subject: [PATCH 1/2] mac fc::io::readsome fix --- src/io/fstream.cpp | 14 ++++++++++++-- tests/io/stream_tests.cpp | 12 ++++++------ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/io/fstream.cpp b/src/io/fstream.cpp index 042791d..e42dbe2 100644 --- a/src/io/fstream.cpp +++ b/src/io/fstream.cpp @@ -65,11 +65,21 @@ namespace fc { const boost::filesystem::path& bfp = file; my->ifs.open( bfp, std::ios::binary ); } + size_t ifstream::readsome( char* buf, size_t len ) { auto s = size_t(my->ifs.readsome( buf, len )); if( s <= 0 ) { - read( buf, 1 ); - s = 1; + try + { + read( buf, len ); + s = len; + } + catch (const fc::eof_exception& eof) + { + s = my->ifs.gcount(); + if (s == 0) + throw eof; + } } return s; } diff --git a/tests/io/stream_tests.cpp b/tests/io/stream_tests.cpp index c6779bb..e3f3dc8 100644 --- a/tests/io/stream_tests.cpp +++ b/tests/io/stream_tests.cpp @@ -112,11 +112,11 @@ BOOST_AUTO_TEST_CASE(fstream_test) BOOST_CHECK_THROW( in2.readsome( buf, 3, 0 ), fc::eof_exception ); { - out.flush(); + out.close(); std::fstream test( outf.path().to_native_ansi_path(), std::fstream::in ); - BOOST_CHECK_EQUAL( 11u, test.readsome( (&(*buf)), 11 ) ); + test.read( (&(*buf)), 11 ); BOOST_CHECK_EQUAL( "Hello world", std::string( (&(*buf)), 11 ) ); - BOOST_CHECK_EQUAL( 0u, test.readsome( (&(*buf)), 11 ) ); + BOOST_CHECK(!test.read( (&(*buf)), 11 )); test.close(); } @@ -166,11 +166,11 @@ BOOST_AUTO_TEST_CASE(buffered_fstream_test) BOOST_CHECK_THROW( bin2.readsome( buf, 3, 0 ), fc::eof_exception ); { - bout.flush(); + bout.close(); std::fstream test( outf.path().to_native_ansi_path(), std::fstream::in ); - BOOST_CHECK_EQUAL( 11u, test.readsome( (&(*buf)), 11 ) ); + test.read( (&(*buf)), 11 ); BOOST_CHECK_EQUAL( "Hello world", std::string( (&(*buf)), 11 ) ); - BOOST_CHECK_EQUAL( 0u, test.readsome( (&(*buf)), 11 ) ); + BOOST_CHECK(!test.read( (&(*buf)), 11 )); test.close(); } } From 84318f8d2856b69554b68b44608df502c211287e Mon Sep 17 00:00:00 2001 From: John Jones Date: Thu, 15 Nov 2018 13:46:07 -0800 Subject: [PATCH 2/2] readsome instead of read --- src/io/fstream.cpp | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/io/fstream.cpp b/src/io/fstream.cpp index e42dbe2..5ebf790 100644 --- a/src/io/fstream.cpp +++ b/src/io/fstream.cpp @@ -68,17 +68,13 @@ namespace fc { size_t ifstream::readsome( char* buf, size_t len ) { auto s = size_t(my->ifs.readsome( buf, len )); - if( s <= 0 ) { - try + if( s <= 0 ) + { + read( buf, 1 ); + s = 1; + if (len > 1) { - read( buf, len ); - s = len; - } - catch (const fc::eof_exception& eof) - { - s = my->ifs.gcount(); - if (s == 0) - throw eof; + s += size_t(my->ifs.readsome( &buf[1], len - 1)); } } return s;