summaryrefslogtreecommitdiff
path: root/include/mbgl/util/char_array_buffer.hpp
blob: 177f00547723f01efde7bf4d810737fc5dc9036b (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
#pragma once

#include <streambuf>

namespace mbgl {
namespace util {

// ref https://artofcode.wordpress.com/2010/12/12/deriving-from-stdstreambuf/

class CharArrayBuffer : public std::streambuf
{
public:
    CharArrayBuffer(char const* data, std::size_t size)
        : begin_(data), end_(data + size), current_(data) {}

private:
    int_type underflow() final {
        if (current_ == end_) {
            return traits_type::eof();
        }
        return traits_type::to_int_type(*current_);
    }

    int_type uflow() final {
        if (current_ == end_) {
            return traits_type::eof();
        }
        return traits_type::to_int_type(*current_++);
    }

    int_type pbackfail(int_type ch) final {
        if (current_ == begin_ || (ch != traits_type::eof() && ch != current_[-1])) {
            return traits_type::eof();
        }
        return traits_type::to_int_type(*--current_);
    }

    std::streamsize showmanyc() final {
        return end_ - current_;
    }

    pos_type seekoff(off_type off, std::ios_base::seekdir dir, std::ios_base::openmode) final {
        if (dir == std::ios_base::beg) current_ = std::min(begin_ + off, end_);
        else if (dir == std::ios_base::cur) current_ = std::min(current_ + off, end_);
        else current_ = std::max(end_ - off, begin_); // dir == std::ios_base::end
        return pos_type(off_type(current_ - begin_));
    }

    char const * const begin_;
    char const * const end_;
    char const * current_;
};

} // namespace util
} // namespace mbgl