aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/mem.cpp
blob: 22a3339e274e501829f3906f85fda0b101cd6a76 (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
63
64
65
#include "mem.h"

#include <util/generic/yexception.h>

TMemoryInput::TMemoryInput() noexcept
    : Buf_(nullptr)
    , Len_(0)
{
}

TMemoryInput::TMemoryInput(const void* buf, size_t len) noexcept
    : Buf_((const char*)buf)
    , Len_(len)
{
}

TMemoryInput::TMemoryInput(const TStringBuf buf) noexcept
    : Buf_(buf.data())
    , Len_(buf.size())
{
}

TMemoryInput::~TMemoryInput() = default;

size_t TMemoryInput::DoNext(const void** ptr, size_t len) {
    len = Min(Len_, len);

    *ptr = Buf_;
    Len_ -= len;
    Buf_ += len;
    return len;
}

void TMemoryInput::DoUndo(size_t len) {
    Len_ += len;
    Buf_ -= len;
}

TMemoryOutput::~TMemoryOutput() = default;

size_t TMemoryOutput::DoNext(void** ptr) {
    Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted"));
    *ptr = Buf_;
    size_t bufferSize = End_ - Buf_;
    Buf_ = End_;

    return bufferSize;
}

void TMemoryOutput::DoUndo(size_t len) {
    Buf_ -= len;
}

void TMemoryOutput::DoWrite(const void* buf, size_t len) {
    char* end = Buf_ + len;
    Y_ENSURE(end <= End_, TStringBuf("memory output stream exhausted"));

    memcpy(Buf_, buf, len);
    Buf_ = end;
}

void TMemoryOutput::DoWriteC(char c) {
    Y_ENSURE(Buf_ < End_, TStringBuf("memory output stream exhausted"));
    *Buf_++ = c;
}