summaryrefslogtreecommitdiff
path: root/src/BStream.cc
blob: 486c21e48aed9d9282d6ea30e75ce75db86a63db (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
#include "BStream.h"
#include "Buffer.h"

static const int s_blockSize = 16 * 1024;

Balau::BStream::BStream(IO<Handle> h) : Filter(h), m_buffer((uint8_t *) malloc(s_blockSize)) {
    AAssert(h->canRead(), "You can't create a buffered stream with a Handle that can't read");
    m_name.set("Stream(%s)", h->getName());
    if ((h.isA<Buffer>()) || (h.isA<BStream>()))
        m_passThru = true;
}

void Balau::BStream::close() throw (Balau::GeneralException) {
    Filter::close();
    free(m_buffer);
    m_buffer = NULL;
    m_availBytes = 0;
    m_cursor = 0;
}

ssize_t Balau::BStream::read(void * _buf, size_t count) throw (Balau::GeneralException) {
    if (m_passThru)
        return getIO()->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 getIO()->read(buf, count) + copied;

    m_cursor = 0;
    IAssert(m_availBytes == 0, "At this point, our internal buffer should be empty, but it's not: %zu", m_availBytes);
    ssize_t r = getIO()->read(m_buffer, s_blockSize);
    EAssert(r >= 0, "BStream got an error while reading: %zi", r);
    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;
        EAssert(r == 1, "We asked for one byte, yet we got %zi", r);
        IAssert(m_cursor > 0, "m_cursor is %zi", m_cursor);
        IAssert(m_availBytes < s_blockSize, "m_availBytes = %zi; s_blockSize = %i", m_availBytes, s_blockSize);
        m_cursor--;
        m_availBytes++;
    }

    return m_buffer[m_cursor];
}

Balau::String Balau::BStream::readString(bool putNL) {
    if (getIO().isA<BStream>())
        return getIO().asA<BStream>()->readString(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();
        IAssert(m_cursor == 0, "m_cursor is %zi", m_cursor);
        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;
}