summaryrefslogtreecommitdiff
path: root/src/Buffer.cc
blob: 199b8e1b793cdbcef3b6eb54e8a2d87faae5e91a (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
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "Buffer.h"
#include "Task.h"
#include "Printer.h"

static const int s_blockSize = 16 * 1024;

Balau::Buffer::~Buffer() {
    if (!m_fromConst)
        free(m_buffer);
}

void Balau::Buffer::close() throw (GeneralException) {
    reset();
}

ssize_t Balau::Buffer::read(void * buf, size_t count) throw (GeneralException) {
    off64_t cursor = rtell();
    if (cursor >= m_bufSize)
        return 0;
    size_t avail = m_bufSize - cursor;

    if (count > avail)
        count = avail;

    memcpy(buf, m_buffer + cursor, count);

    rseek(cursor + count);

    return count;
}

ssize_t Balau::Buffer::write(const void * buf, size_t count) throw (GeneralException) {
    if (m_fromConst)
        throw GeneralException("Buffer is read only and can't be written to.");

    off64_t cursor = wtell();
    off64_t end = cursor + count;
    off64_t endBlock = (end / s_blockSize) + ((end % s_blockSize) ? 1 : 0);
    off64_t oldEndBlock = m_numBlocks;

    if (endBlock > oldEndBlock) {
        m_buffer = (uint8_t *) realloc(m_buffer, endBlock * s_blockSize);
        memset(m_buffer + oldEndBlock * s_blockSize, 0, (endBlock - oldEndBlock) * s_blockSize);
        m_numBlocks = endBlock;
    }

    memcpy(m_buffer + cursor, buf, count);

    wseek(cursor + count);

    if (m_bufSize < end)
        m_bufSize = end;

    return count;
}

void Balau::Buffer::reset() {
    if (!m_fromConst) {
        m_buffer = (uint8_t *)realloc(m_buffer, 0);
        m_bufSize = 0;
    }
    m_numBlocks = 0;
    wseek(0);
    rseek(0);
}

void Balau::Buffer::clear() {
    reset();
    m_fromConst = false;
    m_buffer = NULL;
    m_bufSize = 0;
}

void Balau::Buffer::borrow(const uint8_t * buffer, size_t s) {
    clear();
    m_fromConst = true;
    m_buffer = const_cast<uint8_t *>(buffer);
    m_bufSize = s;
}