aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/IO/StdIStreamFromMemory.cpp
blob: 3242a7e6383beb5ab8e7deaed3c3353b702a8d8a (plain) (blame)
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
#include <IO/StdIStreamFromMemory.h>

namespace DB
{

StdIStreamFromMemory::MemoryBuf::MemoryBuf(char * begin_, size_t size_)
    : begin(begin_)
    , size(size_)
{
    this->setg(begin, begin, begin + size);
}

StdIStreamFromMemory::MemoryBuf::int_type StdIStreamFromMemory::MemoryBuf::underflow()
{
    if (gptr() < egptr())
            return traits_type::to_int_type(*gptr());
    return traits_type::eof();
}

StdIStreamFromMemory::MemoryBuf::pos_type
StdIStreamFromMemory::MemoryBuf::seekoff(off_type off, std::ios_base::seekdir way,
                 std::ios_base::openmode mode)
{
    bool out_mode = (std::ios_base::out & mode) != 0;
    if (out_mode)
        return off_type(-1);

    off_type ret(-1);

    if (way == std::ios_base::beg)
        ret = 0;
    else if (way == std::ios_base::cur)
        ret = gptr() - begin;
    else if (way == std::ios_base::end)
        ret = size;

    if (ret == off_type(-1))
        return ret;

    ret += off;
    if (!(ret >= 0 && size_t(ret) <= size))
        return off_type(-1);

    this->setg(begin, begin + ret, begin + size);

    return pos_type(ret);
}

StdIStreamFromMemory::MemoryBuf::pos_type StdIStreamFromMemory::MemoryBuf::seekpos(pos_type sp,
                 std::ios_base::openmode mode)
{
    return seekoff(off_type(sp), std::ios_base::beg, mode);
}

StdIStreamFromMemory::StdIStreamFromMemory(char * begin_, size_t size_)
    : std::iostream(nullptr)
    , mem_buf(begin_, size_)
{
    init(&mem_buf);
}

}