From c7ec29b2d6a66700933ede6ae2371e1d54744d67 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Tue, 30 May 2017 19:05:43 +0300 Subject: Add ifdstream::read_text() and ifdstream::read_binary() --- libbutl/fdstream.hxx | 12 ++++++++++++ libbutl/fdstream.ixx | 23 +++++++++++++++++++++++ tests/cpfile/driver.cxx | 9 +-------- tests/fdstream/driver.cxx | 3 +-- tests/openssl/driver.cxx | 5 ++--- tests/process/driver.cxx | 12 +++--------- 6 files changed, 42 insertions(+), 22 deletions(-) diff --git a/libbutl/fdstream.hxx b/libbutl/fdstream.hxx index a8f203d..771d242 100644 --- a/libbutl/fdstream.hxx +++ b/libbutl/fdstream.hxx @@ -5,6 +5,7 @@ #ifndef LIBBUTL_FDSTREAM_HXX #define LIBBUTL_FDSTREAM_HXX +#include #include #include #include @@ -398,6 +399,17 @@ namespace butl auto_fd release (); // Note: no skipping. bool is_open () const {return buf_.is_open ();} + // Read the textual stream. The stream is supposed not to contain the null + // character. + // + std::string + read_text (); + + // Read the binary stream. + // + std::vector + read_binary (); + private: bool skip_ = false; }; diff --git a/libbutl/fdstream.ixx b/libbutl/fdstream.ixx index a877699..5181903 100644 --- a/libbutl/fdstream.ixx +++ b/libbutl/fdstream.ixx @@ -3,6 +3,7 @@ // license : MIT; see accompanying LICENSE file #include +#include namespace butl { @@ -118,6 +119,28 @@ namespace butl return buf_.release (); } + inline std::string ifdstream:: + read_text () + { + std::string s; + + // Note that the eof check is important: if the stream is at eof (empty + // file) then getline() will fail. + // + if (peek () != traits_type::eof ()) + butl::getline (*this, s, '\0'); // Hidden by istream::getline(). + + return s; + } + + inline std::vector ifdstream:: + read_binary () + { + std::vector v (std::istreambuf_iterator (*this), + std::istreambuf_iterator ()); + return v; + } + // ofdstream // inline ofdstream:: diff --git a/tests/cpfile/driver.cxx b/tests/cpfile/driver.cxx index 6fa1567..3a19322 100644 --- a/tests/cpfile/driver.cxx +++ b/tests/cpfile/driver.cxx @@ -22,14 +22,7 @@ static string from_file (const path& f) { ifdstream ifs (f, ios::binary); - string s; - - // Note that the eof check is important: if the stream is at eof (empty - // file) then getline() will fail. - // - if (ifs.peek () != ifdstream::traits_type::eof ()) - getline (ifs, s, '\0'); - + string s (ifs.read_text ()); ifs.close (); // Not to miss failed close of the underlying file descriptor. return s; } diff --git a/tests/fdstream/driver.cxx b/tests/fdstream/driver.cxx index c8748b1..41a39b0 100644 --- a/tests/fdstream/driver.cxx +++ b/tests/fdstream/driver.cxx @@ -37,8 +37,7 @@ static const string text3 ("ABCDEF\r\nXYZ"); static string from_stream (ifdstream& is) { - string s; - getline (is, s, '\0'); + string s (is.read_text ()); is.close (); // Not to miss failed close of the underlying file descriptor. return s; } diff --git a/tests/openssl/driver.cxx b/tests/openssl/driver.cxx index 769c77e..b0e37ae 100644 --- a/tests/openssl/driver.cxx +++ b/tests/openssl/driver.cxx @@ -22,10 +22,9 @@ try { openssl os (nullfd, path ("-"), 2, path ("openssl"), "rand", 128); - vector r - ((istreambuf_iterator (os.in)), istreambuf_iterator ()); - + vector r (os.in.read_binary ()); os.in.close (); + return os.wait () && r.size () == 128 ? 0 : 1; } catch (const system_error& e) diff --git a/tests/process/driver.cxx b/tests/process/driver.cxx index b154bce..4694d5b 100644 --- a/tests/process/driver.cxx +++ b/tests/process/driver.cxx @@ -94,9 +94,7 @@ exec (const path& p, cwd, args.data (), pr, bin_mode (move (pr3.out_fd)).get (), -2); ifdstream is (bin_mode (move (pr3.in_ofd))); - - o = vector ( - (istreambuf_iterator (is)), istreambuf_iterator ()); + o = is.read_binary (); r = pr2.wait () && r; r = pr3.wait () && r; @@ -104,9 +102,7 @@ exec (const path& p, else { ifdstream is (bin_mode (move (pr.in_ofd))); - - o = vector ( - (istreambuf_iterator (is)), istreambuf_iterator ()); + o = is.read_binary (); } if (err) @@ -125,9 +121,7 @@ exec (const path& p, if (err && !out) { ifdstream is (bin_mode (move (pr.in_efd))); - - vector e - ((istreambuf_iterator (is)), istreambuf_iterator ()); + vector e (is.read_binary ()); r = in == e && r; } -- cgit v1.1