summaryrefslogtreecommitdiff
path: root/src/MTS_Buffer.cpp
blob: 91ece42fd07840cd34c75a1a16a4740b3d72d152 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * Copyright (C) 2015 by Multi-Tech Systems
 *
 * This file is part of libmts.
 *
 * libmts is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * libmts is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libmts.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <mts/MTS_Buffer.h>
#include <stdexcept>
#include <string>

using namespace MTS;

const uint32_t Buffer::DEFAULT_CAPACITY = 80;

Buffer::Buffer(uint32_t capacity) {
    setCapacity(capacity);
}

Buffer::Buffer(const uint8_t* bytes, uint32_t count)
: m_vBuffer(bytes, bytes + count) {

}

Buffer::Buffer(const Buffer& other)
: m_vBuffer(other.m_vBuffer.begin(), other.m_vBuffer.end()) {

}

Buffer::~Buffer() {

}

Buffer&
Buffer::operator=(const Buffer& other) {
    if (&other == this) {
        return *this;
    }
    m_vBuffer = other.m_vBuffer;
    return *this;
}

const uint8_t*
Buffer::getBuffer() const {
    return &m_vBuffer.at(0);
}

std::string Buffer::str() const {
    return std::string(reinterpret_cast<const char*>(&m_vBuffer.at(0)), m_vBuffer.size());
}

uint32_t Buffer::getSize() const {
    return m_vBuffer.size();
}

void Buffer::setSize(uint32_t newSize) {
    m_vBuffer.resize(newSize);
}

uint32_t Buffer::getCapacity() const {
    return m_vBuffer.capacity();
}

void Buffer::setCapacity(uint32_t newCapacity) {
    m_vBuffer.reserve(newCapacity);
}

void Buffer::clear() {
    m_vBuffer.clear();
}

void Buffer::compact() {
    m_vBuffer.resize(m_vBuffer.size());
}

uint8_t Buffer::operator[](uint32_t index) const {
    return m_vBuffer[index];
}

uint8_t Buffer::operator[](uint32_t index) {
    return m_vBuffer[index];
}

const uint8_t&
Buffer::at(uint32_t index) const {
    return m_vBuffer.at(index);
}

uint8_t&
Buffer::at(uint32_t index) {
    return m_vBuffer.at(index);
}

Buffer&
Buffer::append(uint8_t byte) {
    m_vBuffer.push_back(byte);
    return *this;
}

Buffer& Buffer::append(const uint8_t* bytes, uint32_t count) {
    m_vBuffer.resize(m_vBuffer.size() + count);
    std::copy(bytes, bytes + count, std::back_inserter(m_vBuffer));
    return *this;
}

Buffer&
Buffer::insert(uint32_t index, uint8_t byte) {
    m_vBuffer.insert(m_vBuffer.begin() + index, byte);
    return *this;
}

Buffer&
Buffer::insert(uint32_t index, const uint8_t* bytes, uint32_t count) {
    m_vBuffer.insert(m_vBuffer.begin() + index, bytes, bytes + count);
    return *this;
}

Buffer&
Buffer::remove(uint32_t index) {
    m_vBuffer.erase(m_vBuffer.begin() + index);
    return *this;
}

Buffer&
Buffer::remove(uint32_t start, uint32_t end) {
    m_vBuffer.erase(m_vBuffer.begin() + start, m_vBuffer.begin() + end);
    return *this;
}

Buffer&
Buffer::replace(uint32_t start, uint32_t end, const uint8_t* bytes,
uint32_t count) {
    if (end - start == count) {
        if (start > end || end > m_vBuffer.size()) {
            throw std::out_of_range("Buffer| index out of bounds");
        }
        std::copy(bytes, bytes + count, m_vBuffer.begin());
        return *this;
    }
    return remove(start, end).insert(start, bytes, count);
}

Buffer* Buffer::clone() const {
    return new Buffer(*this);
}