Files

209 lines
5.4 KiB
C++
Raw Permalink Normal View History

2016-08-16 16:51:22 +02:00
/*
2017-11-09 19:34:51 +01:00
* This file is part of the Flowee project
* Copyright (C) 2016-2024 Tom Zander <tom@flowee.org>
2016-08-16 16:51:22 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ConstBuffer.h"
#include <cassert>
2019-08-26 20:48:12 +02:00
#include <cstdint>
2016-08-16 16:51:22 +02:00
2019-10-05 16:00:31 +02:00
Streaming::ConstBuffer Streaming::ConstBuffer::create(const char *start, size_t size)
{
auto buf = std::shared_ptr<char>(new char[size], std::default_delete<char[]>());
memcpy(buf.get(), start, size);
return Streaming::ConstBuffer(buf, buf.get(), buf.get() + size);
}
Streaming::ConstBuffer Streaming::ConstBuffer::create(const std::vector<unsigned char> &vector)
{
auto buf = std::shared_ptr<char>(new char[vector.size()], std::default_delete<char[]>());
memcpy(buf.get(), &vector[0], vector.size());
return Streaming::ConstBuffer(buf, buf.get(), buf.get() + vector.size());
}
2016-08-16 16:51:22 +02:00
Streaming::ConstBuffer::ConstBuffer()
: m_buffer(nullptr),
m_start(nullptr),
m_stop(nullptr)
{
}
2018-03-07 22:07:03 +01:00
Streaming::ConstBuffer::ConstBuffer(std::shared_ptr<char> buffer, char const *start, char const *stop)
2016-08-16 16:51:22 +02:00
: m_buffer(buffer),
m_start(start),
m_stop(stop)
{
assert(stop >= start);
assert(start >= buffer.get());
assert(stop >= buffer.get());
}
char const* Streaming::ConstBuffer::begin() const
{
return m_start;
}
char const* Streaming::ConstBuffer::constData() const
{
return m_start;
}
char const* Streaming::ConstBuffer::end() const
{
return m_stop;
}
int Streaming::ConstBuffer::size() const
{
return m_stop - m_start;
}
std::string Streaming::ConstBuffer::toHex(Direction_ dir) const
{
std::string buf(size() * 2 + 1, 0);
char *pbuf = &buf.front();
for (int i = 0; i < size(); ++i) {
const uint8_t k = m_start[dir == Forward ? i : (size() - i - 1)];
sprintf(pbuf, "%02x", k);
pbuf += 2;
}
buf.pop_back(); // remove the last zero
return buf;
}
2024-06-11 21:34:41 +02:00
int Streaming::ConstBuffer::indexOf(char k, int offset) const
2022-01-25 23:59:54 +01:00
{
const char *x = m_start + offset;
while (x < m_stop) {
if (*x == k) return x - m_start;
++x;
}
return -1;
}
2016-08-16 16:51:22 +02:00
Streaming::ConstBuffer::operator boost::asio::const_buffer() const
{
return boost::asio::const_buffer(constData(), size());
}
std::shared_ptr<char> Streaming::ConstBuffer::internal_buffer() const
{
return m_buffer;
}
2017-05-03 16:08:13 +02:00
Streaming::ConstBuffer Streaming::ConstBuffer::mid(int offset, int length) const
{
2019-08-23 22:34:33 +02:00
if (length > 0) {
assert(m_start + offset + length <= m_stop);
return ConstBuffer(m_buffer, m_start + offset, m_start + offset + length);
}
2022-02-10 17:58:38 +01:00
if (length == 0)
return ConstBuffer();
2019-08-23 22:34:33 +02:00
assert(m_start + offset <= m_stop);
return ConstBuffer(m_buffer, m_start + offset, m_stop);
2017-05-03 16:08:13 +02:00
}
2016-08-16 16:51:22 +02:00
char Streaming::ConstBuffer::operator[](size_t idx) const
{
assert(begin() + idx < end());
return *(begin() + idx);
}
2019-08-26 20:48:12 +02:00
bool Streaming::ConstBuffer::startsWith(const Streaming::ConstBuffer &other) const
{
if (!other.isValid()) return false;
2024-06-11 17:57:17 +02:00
const auto otherSize = other.size();
if (otherSize == 0) return true;
if (otherSize > size()) return false;
if (isEmpty()) return false;
2019-08-26 20:48:12 +02:00
const char *s1 = m_start;
const char *s2 = other.begin();
while (s2 != other.end()) {
const uint8_t a = static_cast<uint8_t>(*s1);
const uint8_t b = static_cast<uint8_t>(*s2);
if (a != b)
return false;
s1++; s2++;
}
return true;
}
2019-12-30 17:37:23 +01:00
2024-06-11 17:57:17 +02:00
bool Streaming::ConstBuffer::startsWith(const char byte) const
{
if (!isValid()) return false;
if (m_start == m_stop) return false;
return byte == *m_start;
}
bool Streaming::ConstBuffer::endsWith(const ConstBuffer &other) const
{
if (!other.isValid()) return false;
const auto otherSize = other.size();
if (otherSize == 0) return true;
if (otherSize > size()) return false;
if (isEmpty()) return false;
const char *s1 = m_stop - other.size();
const char *s2 = other.begin();
while (s2 != other.end()) {
const uint8_t a = static_cast<uint8_t>(*s1);
const uint8_t b = static_cast<uint8_t>(*s2);
if (a != b)
return false;
s1++; s2++;
}
return true;
}
bool Streaming::ConstBuffer::endsWith(const char byte) const
{
if (!isValid()) return false;
if (m_start == m_stop) return false;
return byte == *(m_stop - 1);
}
2019-12-30 17:37:23 +01:00
bool Streaming::ConstBuffer::operator==(const Streaming::ConstBuffer &other) const
{
if (!other.isValid())
return !isValid();
if (!isValid())
return false;
if (size() != other.size())
return false;
const char *me = begin();
const char *them = other.begin();
while (me != end()) {
if (*me != *them)
return false;
++them;
++me;
}
return true;
}
void Streaming::ConstBuffer::clear()
{
m_buffer = nullptr;
m_start = nullptr;
m_stop = nullptr;
}