aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libbutl/fdstream.hxx12
-rw-r--r--libbutl/fdstream.ixx23
-rw-r--r--tests/cpfile/driver.cxx9
-rw-r--r--tests/fdstream/driver.cxx3
-rw-r--r--tests/openssl/driver.cxx5
-rw-r--r--tests/process/driver.cxx12
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 <vector>
#include <string>
#include <istream>
#include <ostream>
@@ -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<char>
+ 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 <cassert>
+#include <iterator>
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<char> ifdstream::
+ read_binary ()
+ {
+ std::vector<char> v (std::istreambuf_iterator<char> (*this),
+ std::istreambuf_iterator<char> ());
+ 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<char> r
- ((istreambuf_iterator<char> (os.in)), istreambuf_iterator<char> ());
-
+ vector<char> 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<char> (
- (istreambuf_iterator<char> (is)), istreambuf_iterator<char> ());
+ 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<char> (
- (istreambuf_iterator<char> (is)), istreambuf_iterator<char> ());
+ 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<char> e
- ((istreambuf_iterator<char> (is)), istreambuf_iterator<char> ());
+ vector<char> e (is.read_binary ());
r = in == e && r;
}