2020-08-26 15:38:01 +08:00
|
|
|
#include <blah/streams/memorystream.h>
|
2021-05-07 12:48:06 +08:00
|
|
|
#include <cstring>
|
2020-08-26 15:38:01 +08:00
|
|
|
|
|
|
|
using namespace Blah;
|
|
|
|
|
|
|
|
MemoryStream::MemoryStream()
|
|
|
|
: m_data(nullptr), m_length(0), m_position(0) {}
|
|
|
|
|
2021-05-10 08:23:02 +08:00
|
|
|
MemoryStream::MemoryStream(char* data, size_t length)
|
2020-08-26 15:38:01 +08:00
|
|
|
: m_data(data), m_length(length), m_position(0) {}
|
|
|
|
|
|
|
|
MemoryStream::MemoryStream(MemoryStream&& src) noexcept
|
|
|
|
{
|
|
|
|
m_data = src.m_data;
|
|
|
|
m_position = src.m_position;
|
|
|
|
m_length = src.m_length;
|
|
|
|
src.m_data = nullptr;
|
|
|
|
src.m_length = src.m_position = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemoryStream& MemoryStream::operator=(MemoryStream&& src) noexcept
|
|
|
|
{
|
|
|
|
m_data = src.m_data;
|
|
|
|
m_position = src.m_position;
|
|
|
|
m_length = src.m_length;
|
|
|
|
src.m_data = nullptr;
|
|
|
|
src.m_length = src.m_position = 0;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-05-10 08:23:02 +08:00
|
|
|
size_t MemoryStream::read_into(void* ptr, size_t len)
|
2020-08-26 15:38:01 +08:00
|
|
|
{
|
|
|
|
if (len < 0 || ptr == nullptr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (len > m_length - m_position)
|
|
|
|
len = m_length - m_position;
|
|
|
|
|
|
|
|
memcpy(ptr, m_data + m_position, (size_t)len);
|
|
|
|
m_position += len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2021-05-10 08:23:02 +08:00
|
|
|
size_t MemoryStream::write_from(const void* ptr, size_t len)
|
2020-08-26 15:38:01 +08:00
|
|
|
{
|
|
|
|
if (len < 0 || ptr == nullptr)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (len > m_length - m_position)
|
|
|
|
len = m_length - m_position;
|
|
|
|
|
|
|
|
memcpy(m_data + m_position, ptr, (size_t)len);
|
|
|
|
m_position += len;
|
|
|
|
return len;
|
|
|
|
}
|