summaryrefslogtreecommitdiff
path: root/src/BStream.cc
blob: 89e38b0d4da4f2e43413c8e0670ce07eff191f9c (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "BStream.h"
#include "Buffer.h"

static const int s_blockSize = 16 * 1024;

Balau::BStream::BStream(const IO<Handle> & h) : m_h(h), m_buffer((uint8_t *) malloc(s_blockSize)), m_availBytes(0), m_cursor(0), m_passThru(false) {
    Assert(m_h->canRead());
    m_name.set("Stream(%s)", m_h->getName());
    if ((m_h.isA<Buffer>()) || (m_h.isA<BStream>()))
        m_passThru = true;
}

void Balau::BStream::close() throw (Balau::GeneralException) {
    if (!m_detached)
        m_h->close();
    free(m_buffer);
    m_availBytes = 0;
    m_cursor = 0;
}

bool Balau::BStream::isClosed() { return m_closed || m_h->isClosed(); }
bool Balau::BStream::isEOF() { return (m_availBytes == 0) && m_h->isEOF(); }
bool Balau::BStream::canRead() { return true; }
const char * Balau::BStream::getName() { return m_name.to_charp(); }
off_t Balau::BStream::getSize() { return m_h->getSize(); }

ssize_t Balau::BStream::read(void * _buf, size_t count) throw (Balau::GeneralException) {
    if (m_passThru)
        return m_h->read(_buf, count);
    uint8_t * buf = (uint8_t *) _buf;
    size_t copied = 0;
    size_t toCopy = count;

    if (m_availBytes != 0) {
        if (toCopy > m_availBytes)
            toCopy = m_availBytes;
        memcpy(buf, m_buffer + m_cursor, toCopy);
        count -= toCopy;
        m_cursor += toCopy;
        m_availBytes -= toCopy;
        copied = toCopy;
        buf += toCopy;
        toCopy = count;
    }

    if (count == 0)
        return copied;

    if (count >= s_blockSize)
        return m_h->read(buf, count) + copied;

    m_cursor = 0;
    Assert(m_availBytes == 0);
    ssize_t r = m_h->read(m_buffer, s_blockSize);
    Assert(r >= 0);
    m_availBytes = r;

    if (toCopy > m_availBytes)
        toCopy = m_availBytes;
    if (toCopy == 0)
        return 0;
    memcpy(buf, m_buffer, toCopy);
    m_cursor += toCopy;
    m_availBytes -= toCopy;
    copied += toCopy;

    return copied;
}

int Balau::BStream::peekNextByte() {
    m_passThru = false;
    if (m_availBytes == 0) {
        uint8_t b;
        ssize_t r = read(&b, 1);
        if (!r)
            return -1;
        Assert(r == 1);
        Assert(m_cursor > 0);
        Assert(m_availBytes < s_blockSize);
        m_cursor--;
        m_availBytes++;
    }

    return m_buffer[m_cursor];
}

Balau::String Balau::BStream::readString(bool putNL) {
    peekNextByte();
    uint8_t * cr, * lf, * nl;
    String ret;
    size_t chunkSize = 0;

    cr = (uint8_t *) memchr(m_buffer + m_cursor, '\r', m_availBytes);
    lf = (uint8_t *) memchr(m_buffer + m_cursor, '\n', m_availBytes);
    if (cr && lf) {
        nl = cr;
        if (lf < cr)
            nl = lf;
    } else if (!cr) {
        nl = lf;
    } else {
        nl = cr;
    }
    while (!nl) {
        chunkSize = m_availBytes;
        ret += String((const char *) m_buffer + m_cursor, chunkSize);
        m_availBytes -= chunkSize;
        m_cursor += chunkSize;
        if (isClosed() || isEOF())
            return ret;
        peekNextByte();
        Assert(m_cursor == 0);
        cr = (uint8_t *) memchr(m_buffer, '\r', m_availBytes);
        lf = (uint8_t *) memchr(m_buffer, '\n', m_availBytes);
        if (cr && lf) {
            nl = cr;
            if (lf < cr)
                nl = lf;
        } else if (!cr) {
            nl = lf;
        } else {
            nl = cr;
        }
    }

    chunkSize = nl - (m_buffer + m_cursor);
    ret += String((const char *) m_buffer + m_cursor, chunkSize);

    m_availBytes -= chunkSize;
    m_cursor += chunkSize;

    char b;
    read(&b, 1);
    if (putNL)
        ret += String(&b, 1);

    if ((b == '\r') && (peekNextByte() == '\n')) {
        read(&b, 1);
        if (putNL)
            ret += String(&b, 1);
    }

    return ret;
}