aboutsummaryrefslogtreecommitdiff
path: root/libbutl
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-05-30 19:05:43 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-05-31 00:51:43 +0300
commitc7ec29b2d6a66700933ede6ae2371e1d54744d67 (patch)
tree119ff80f333892422e99cd352c99702f0ae7eb7c /libbutl
parent95ff8f359cfc2189bd4d7e02e15373027d2bda32 (diff)
Add ifdstream::read_text() and ifdstream::read_binary()
Diffstat (limited to 'libbutl')
-rw-r--r--libbutl/fdstream.hxx12
-rw-r--r--libbutl/fdstream.ixx23
2 files changed, 35 insertions, 0 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::