restructured project to match a more standard cmake setup

This commit is contained in:
Noel Berry
2020-12-31 13:43:23 -08:00
parent c841bd82a1
commit 241d863ac4
97 changed files with 233 additions and 264 deletions

View File

@ -0,0 +1,107 @@
#include <blah/streams/bufferstream.h>
#include <string.h>
using namespace Blah;
BufferStream::BufferStream()
: m_buffer(nullptr), m_capacity(0), m_length(0), m_position(0) {}
BufferStream::BufferStream(int capacity)
: m_buffer(nullptr), m_capacity(0), m_length(0), m_position(0)
{
if (capacity > 0)
{
m_buffer = new char[capacity];
m_capacity = capacity;
}
}
BufferStream::BufferStream(BufferStream&& src) noexcept
{
m_buffer = src.m_buffer;
m_length = src.m_length;
m_capacity = src.m_capacity;
m_position = src.m_position;
src.m_buffer = nullptr;
src.m_position = src.m_length = src.m_capacity = 0;
}
BufferStream& BufferStream::operator=(BufferStream&& src) noexcept
{
m_buffer = src.m_buffer;
m_length = src.m_length;
m_capacity = src.m_capacity;
m_position = src.m_position;
src.m_buffer = nullptr;
src.m_position = src.m_length = src.m_capacity = 0;
return *this;
}
BufferStream::~BufferStream()
{
delete[] m_buffer;
}
int64_t BufferStream::read_into(void* ptr, int64_t len)
{
if (m_buffer == nullptr || ptr == nullptr)
return 0;
if (len < 0)
return 0;
if (len > m_length - m_position)
len = m_length - m_position;
memcpy(ptr, m_buffer + m_position, (size_t)len);
m_position += len;
return len;
}
int64_t BufferStream::write_from(const void* ptr, int64_t len)
{
if (len < 0)
return 0;
// resize
if (m_position + len >= m_capacity)
{
auto last_capacity = m_capacity;
if (m_capacity <= 0)
m_capacity = 16;
while (m_position + len >= m_capacity)
m_capacity *= 2;
char* new_buffer = new char[m_capacity];
if (m_buffer != nullptr)
{
memcpy(new_buffer, m_buffer, last_capacity);
delete[] m_buffer;
}
m_buffer = new_buffer;
}
// copy data
if (ptr != nullptr)
memcpy(m_buffer + m_position, ptr, (size_t)len);
// increment position
m_position += len;
if (m_position > m_length)
m_length = m_position;
// return the amount we wrote
return len;
}
void BufferStream::close()
{
delete[] m_buffer;
m_buffer = nullptr;
m_position = 0;
m_length = 0;
m_capacity = 0;
}

View File

@ -0,0 +1,97 @@
#include <blah/streams/filestream.h>
#include <blah/core/log.h>
#include "../internal/platform_backend.h"
#include <string.h>
using namespace Blah;
FileStream::FileStream()
{
m_handle = nullptr;
m_mode = FileMode::None;
}
FileStream::FileStream(const char* path, FileMode mode)
: m_mode(mode)
{
if (!PlatformBackend::file_open(path, &m_handle, mode))
m_handle = nullptr;
}
FileStream::FileStream(FileStream&& src) noexcept
{
m_handle = src.m_handle;
m_mode = src.m_mode;
src.m_handle = nullptr;
}
FileStream& FileStream::operator=(FileStream&& src) noexcept
{
m_handle = src.m_handle;
m_mode = src.m_mode;
src.m_handle = nullptr;
return *this;
}
FileStream::~FileStream()
{
if (m_handle != nullptr)
PlatformBackend::file_close(m_handle);
}
int64_t FileStream::length() const
{
if (m_handle == nullptr)
return 0;
return PlatformBackend::file_length(m_handle);
}
int64_t FileStream::position() const
{
if (m_handle == nullptr)
return 0;
return PlatformBackend::file_position(m_handle);
}
int64_t FileStream::seek(int64_t seek_to)
{
if (m_handle == nullptr)
return 0;
return PlatformBackend::file_seek(m_handle, seek_to);
}
int64_t FileStream::read_into(void* ptr, int64_t length)
{
if (m_handle == nullptr)
{
BLAH_ERROR("Unable to read from Stream");
return 0;
}
return PlatformBackend::file_read(m_handle, ptr, length);
}
int64_t FileStream::write_from(const void* ptr, int64_t length)
{
if (length <= 0)
return 0;
if (m_handle == nullptr)
{
BLAH_ERROR("Unable to write to Stream");
return 0;
}
return PlatformBackend::file_write(m_handle, ptr, length);
}
void FileStream::close()
{
if (m_handle != nullptr)
PlatformBackend::file_close(m_handle);
m_handle = nullptr;
m_mode = FileMode::None;
}

View File

@ -0,0 +1,55 @@
#include <blah/streams/memorystream.h>
#include <string.h>
using namespace Blah;
MemoryStream::MemoryStream()
: m_data(nullptr), m_length(0), m_position(0) {}
MemoryStream::MemoryStream(char* data, int64_t length)
: 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;
}
int64_t MemoryStream::read_into(void* ptr, int64_t len)
{
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;
}
int64_t MemoryStream::write_from(const void* ptr, int64_t len)
{
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;
}

29
src/streams/stream.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <blah/streams/stream.h>
#include <blah/containers/str.h>
#include <string.h>
using namespace Blah;
int64_t Stream::pipe(Stream& stream, int64_t length)
{
const int BUFFER_LENGTH = 4096;
int64_t result = 0;
char buffer[BUFFER_LENGTH];
while (length > 0)
{
auto step = length;
if (step > BUFFER_LENGTH)
step = BUFFER_LENGTH;
auto count = read(buffer, step);
auto wrote = stream.write(buffer, count);
result += wrote;
length -= step;
if (count < step || wrote < count)
break;
}
return result;
}