aboutsummaryrefslogtreecommitdiff
path: root/libbutl/bufstreambuf.hxx
blob: a49b2d049542187227ea1a80a48eabf72529d1c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// file      : libbutl/bufstreambuf.hxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#pragma once

#include <cstdint>   // uint64_t
#include <streambuf>

#include <libbutl/export.hxx>

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<char>
  {
  public:
    using base = std::basic_streambuf<char>;

    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_;
  };
}