/* * 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 . * */ #include #include #include 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(&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); }