mirror of
https://github.com/NoelFB/blah.git
synced 2025-06-29 19:25:26 +08:00
restructured project to match a more standard cmake setup
This commit is contained in:
37
include/blah/streams/bufferstream.h
Normal file
37
include/blah/streams/bufferstream.h
Normal file
@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
#include <blah/streams/stream.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
class BufferStream : public Stream
|
||||
{
|
||||
public:
|
||||
BufferStream();
|
||||
BufferStream(int capacity);
|
||||
BufferStream(BufferStream&& bs) noexcept;
|
||||
BufferStream& operator=(BufferStream&& bs) noexcept;
|
||||
~BufferStream();
|
||||
|
||||
virtual int64_t length() const override { return m_length; }
|
||||
virtual int64_t position() const override { return m_position; }
|
||||
virtual int64_t seek(int64_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
|
||||
virtual bool is_open() const override { return m_buffer != nullptr; }
|
||||
virtual bool is_readable() const override { return true; }
|
||||
virtual bool is_writable() const override { return true; }
|
||||
virtual void close() override;
|
||||
void clear() { m_length = m_position = 0; }
|
||||
|
||||
char* data() { return m_buffer; }
|
||||
const char* data() const { return m_buffer; }
|
||||
|
||||
protected:
|
||||
virtual int64_t read_into(void* ptr, int64_t length) override;
|
||||
virtual int64_t write_from(const void* ptr, int64_t length) override;
|
||||
|
||||
private:
|
||||
char* m_buffer;
|
||||
int64_t m_capacity;
|
||||
int64_t m_length;
|
||||
int64_t m_position;
|
||||
};
|
||||
}
|
39
include/blah/streams/endian.h
Normal file
39
include/blah/streams/endian.h
Normal file
@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
enum class Endian
|
||||
{
|
||||
Little,
|
||||
Big
|
||||
};
|
||||
|
||||
template<class T>
|
||||
inline void swap_endian(T* value)
|
||||
{
|
||||
for (int i = 0; i < sizeof(T) / 2; i++)
|
||||
{
|
||||
char* _ptr = (char*)&value;
|
||||
char _temp = *(_ptr + i);
|
||||
*(_ptr + i) = *(_ptr + sizeof(T) - i - 1);
|
||||
*(_ptr + sizeof(T) - i - 1) = _temp;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool is_big_endian()
|
||||
{
|
||||
return (*((short*)"AB") == 0x4243);
|
||||
}
|
||||
|
||||
inline bool is_little_endian()
|
||||
{
|
||||
return (*((short*)"AB") != 0x4243);
|
||||
}
|
||||
|
||||
inline bool is_endian(const Endian& endian)
|
||||
{
|
||||
return
|
||||
(endian == Endian::Little && is_little_endian()) ||
|
||||
(endian == Endian::Big && is_big_endian());
|
||||
}
|
||||
}
|
32
include/blah/streams/filestream.h
Normal file
32
include/blah/streams/filestream.h
Normal file
@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include <blah/streams/stream.h>
|
||||
#include <blah/core/filesystem.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
class FileStream : public Stream
|
||||
{
|
||||
public:
|
||||
FileStream();
|
||||
FileStream(const char* path, FileMode mode = FileMode::ReadWrite);
|
||||
FileStream(FileStream&& fs) noexcept;
|
||||
FileStream& operator=(FileStream&& fs) noexcept;
|
||||
~FileStream();
|
||||
|
||||
virtual int64_t length() const override;
|
||||
virtual int64_t position() const override;
|
||||
virtual int64_t seek(int64_t seekTo) override;
|
||||
virtual bool is_open() const override { return m_handle != nullptr; }
|
||||
virtual bool is_readable() const override { return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Read); }
|
||||
virtual bool is_writable() const override { return m_handle != nullptr && (m_mode == FileMode::ReadWrite || m_mode == FileMode::Write); }
|
||||
virtual void close() override;
|
||||
|
||||
protected:
|
||||
virtual int64_t read_into(void* ptr, int64_t length) override;
|
||||
virtual int64_t write_from(const void* ptr, int64_t length) override;
|
||||
|
||||
private:
|
||||
FileMode m_mode;
|
||||
void* m_handle;
|
||||
};
|
||||
}
|
35
include/blah/streams/memorystream.h
Normal file
35
include/blah/streams/memorystream.h
Normal file
@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
#include <blah/streams/stream.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
class MemoryStream : public Stream
|
||||
{
|
||||
public:
|
||||
MemoryStream();
|
||||
MemoryStream(char* data, int64_t length);
|
||||
MemoryStream(MemoryStream&& ms) noexcept;
|
||||
MemoryStream& operator=(MemoryStream&& ms) noexcept;
|
||||
~MemoryStream() { m_data = nullptr; m_length = m_position = 0; }
|
||||
|
||||
virtual int64_t length() const override { return m_length; }
|
||||
virtual int64_t position() const override { return m_position; }
|
||||
virtual int64_t seek(int64_t seekTo) override { return m_position = (seekTo < 0 ? 0 : (seekTo > m_length ? m_length : seekTo)); }
|
||||
virtual bool is_open() const override { return m_data != nullptr; }
|
||||
virtual bool is_readable() const override { return true; }
|
||||
virtual bool is_writable() const override { return true; }
|
||||
virtual void close() override { m_data = nullptr; m_length = m_position = 0; }
|
||||
|
||||
char* data() { return m_data; }
|
||||
const char* data() const { return m_data; }
|
||||
|
||||
protected:
|
||||
virtual int64_t read_into(void* ptr, int64_t length) override;
|
||||
virtual int64_t write_from(const void* ptr, int64_t length) override;
|
||||
|
||||
private:
|
||||
char* m_data;
|
||||
int64_t m_length;
|
||||
int64_t m_position;
|
||||
};
|
||||
}
|
141
include/blah/streams/stream.h
Normal file
141
include/blah/streams/stream.h
Normal file
@ -0,0 +1,141 @@
|
||||
#pragma once
|
||||
#include <inttypes.h>
|
||||
#include <blah/containers/str.h>
|
||||
#include <blah/streams/endian.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace Blah
|
||||
{
|
||||
class Stream
|
||||
{
|
||||
public:
|
||||
Stream() = default;
|
||||
Stream(const Stream&) = delete;
|
||||
Stream& operator=(const Stream&) = delete;
|
||||
|
||||
virtual ~Stream() = default;
|
||||
|
||||
// returns the length of the stream
|
||||
virtual int64_t length() const = 0;
|
||||
|
||||
// returns the position of the stream
|
||||
virtual int64_t position() const = 0;
|
||||
|
||||
// seeks the position of the stream
|
||||
virtual int64_t seek(int64_t seek_to) = 0;
|
||||
|
||||
// returns true of the stream is open
|
||||
virtual bool is_open() const = 0;
|
||||
|
||||
// returns true of the stream is readable
|
||||
virtual bool is_readable() const = 0;
|
||||
|
||||
// returns true of the stream is writable
|
||||
virtual bool is_writable() const = 0;
|
||||
|
||||
// closes the stream
|
||||
virtual void close() = 0;
|
||||
|
||||
// pipes the contents of this tream to another stream
|
||||
int64_t pipe(Stream& to, int64_t length);
|
||||
|
||||
// reads the amount of bytes into the given buffer, and returns the amount read
|
||||
int64_t read(void* buffer, int64_t length) { return read_into(buffer, length); }
|
||||
|
||||
// reads a string. if length < 0, assumes null-terminated
|
||||
String read_string(int length = -1)
|
||||
{
|
||||
String result;
|
||||
|
||||
if (length < 0)
|
||||
{
|
||||
char next;
|
||||
while (read(&next, 1) && next != '\0')
|
||||
result.append(next);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.set_length(length);
|
||||
read_into(result.cstr(), length);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
String read_line()
|
||||
{
|
||||
String result;
|
||||
|
||||
char next;
|
||||
while (read(&next, 1) && next != '\n' && next != '\0')
|
||||
result.append(next);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// reads a number
|
||||
template<class T>
|
||||
T read()
|
||||
{
|
||||
return read<T>(Endian::Little);
|
||||
}
|
||||
|
||||
// reads a number
|
||||
template<class T>
|
||||
T read(Endian endian)
|
||||
{
|
||||
T result;
|
||||
read(&result, sizeof(T));
|
||||
if (!Blah::is_endian(endian))
|
||||
Blah::swap_endian(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// writes the amount of bytes to the stream from the given buffer, and returns the amount written
|
||||
int64_t write(const void* buffer, int64_t length)
|
||||
{
|
||||
return write_from(buffer, length);
|
||||
}
|
||||
|
||||
// writes a null-terminated string, and returns the amount written
|
||||
int64_t write_cstr(const Str& string)
|
||||
{
|
||||
return write(string.cstr(), string.length() + 1);
|
||||
}
|
||||
|
||||
// writes a null-terminated string, and returns the amount written
|
||||
int64_t write_cstr(const char* cstr)
|
||||
{
|
||||
return write(cstr, strlen(cstr) + 1);
|
||||
}
|
||||
|
||||
// writes a number
|
||||
template<class T>
|
||||
int64_t write(const T& value)
|
||||
{
|
||||
return write<T>(value, Endian::Little);
|
||||
}
|
||||
|
||||
// writes a number
|
||||
template<class T>
|
||||
int64_t write(const T& value, Endian endian)
|
||||
{
|
||||
T writing = value;
|
||||
|
||||
if (!Blah::is_endian(endian))
|
||||
Blah::swap_endian(&writing);
|
||||
|
||||
return write(&writing, sizeof(T));
|
||||
}
|
||||
|
||||
protected:
|
||||
// reads from the stream into the given buffer, and returns the number of bytes read
|
||||
virtual int64_t read_into(void* buffer, int64_t length) = 0;
|
||||
|
||||
// writes from the stream from the given buffer, and returns the number of bytes written
|
||||
virtual int64_t write_from(const void* buffer, int64_t length) = 0;
|
||||
};
|
||||
}
|
||||
|
||||
#undef BLAH_SWAP_ENDIAN
|
||||
#undef BLAH_BIG_ENDIAN
|
Reference in New Issue
Block a user