blah/src/streams/memorystream.cpp

56 lines
1.2 KiB
C++
Raw Normal View History

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) {}
MemoryStream::MemoryStream(char* data, i64 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;
}
i64 MemoryStream::read_into(void* ptr, i64 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;
}
i64 MemoryStream::write_from(const void* ptr, i64 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;
}