aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/multi.cpp
blob: 5866bad44a76acef5f48ac7406fe64da513824cf (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
#include "null.h"
#include "multi.h"

TMultiInput::TMultiInput(IInputStream* f, IInputStream* s) noexcept 
    : C_(f)
    , N_(s)
{
}

TMultiInput::~TMultiInput() = default;

size_t TMultiInput::DoRead(void* buf, size_t len) {
    const size_t ret = C_->Read(buf, len);

    if (ret) {
        return ret;
    }

    C_ = N_;
    N_ = &Cnull;

    return C_->Read(buf, len);
}

size_t TMultiInput::DoReadTo(TString& st, char ch) {
    size_t ret = C_->ReadTo(st, ch);
    if (ret == st.size() + 1) { // found a symbol, not eof
        return ret;
    }

    C_ = N_;
    N_ = &Cnull;

    if (ret == 0) {
        ret += C_->ReadTo(st, ch);
    } else {
        TString tmp;
        ret += C_->ReadTo(tmp, ch);
        st += tmp;
    }

    return ret;
}

size_t TMultiInput::DoSkip(size_t len) {
    const size_t ret = C_->Skip(len);

    if (ret) {
        return ret;
    }

    C_ = N_;
    N_ = &Cnull;

    return C_->Skip(len);
}