From 98c4038df36fb73601c58ccd885d1c2d3703cf6e Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 8 Sep 2021 08:26:54 +0200 Subject: Factor "buffered streambuf" interface from fdbuf to bufstreambuf Also rename fdbuf to fdstreambuf for consistency. --- libbutl/bufstreambuf.hxx | 67 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 libbutl/bufstreambuf.hxx (limited to 'libbutl/bufstreambuf.hxx') diff --git a/libbutl/bufstreambuf.hxx b/libbutl/bufstreambuf.hxx new file mode 100644 index 0000000..a49b2d0 --- /dev/null +++ b/libbutl/bufstreambuf.hxx @@ -0,0 +1,67 @@ +// file : libbutl/bufstreambuf.hxx -*- C++ -*- +// license : MIT; see accompanying LICENSE file + +#pragma once + +#include // uint64_t +#include + +#include + +namespace butl +{ + // A buffered streambuf interface that exposes its buffer for direct scan + // and provides a notion of logical position. See fdstreambuf for background + // and motivation. + // + class LIBBUTL_SYMEXPORT bufstreambuf: public std::basic_streambuf + { + public: + using base = std::basic_streambuf; + + using int_type = base::int_type; + using traits_type = base::traits_type; + + using pos_type = base::pos_type; // std::streampos + using off_type = base::off_type; // std::streamoff + + public: + explicit + bufstreambuf (std::uint64_t pos = 0): off_ (pos) {} + + virtual + ~bufstreambuf (); + + // basic_streambuf input interface. + // + public: + + // Direct access to the get area. Use with caution. + // + using base::gptr; + using base::egptr; + using base::gbump; + + // Return the (logical) position of the next byte to be read. + // + // Note that on Windows when reading in the text mode the logical position + // may differ from the physical file descriptor position due to the CRLF + // character sequence translation. See the fdstreambuf::seekoff() + // implementation for more background on this issue. + // + std::uint64_t + tellg () const {return off_ - (egptr () - gptr ());} + + // basic_streambuf output interface. + // + public: + + // Return the (logical) position of the next byte to be written. + // + std::uint64_t + tellp () const {return off_ + (pptr () - pbase ());} + + protected: + std::uint64_t off_; + }; +} -- cgit v1.1