blah/include/blah/streams/stream.h

101 lines
2.7 KiB
C
Raw Normal View History

2020-08-26 15:38:01 +08:00
#pragma once
#include <blah/common.h>
2020-08-26 15:38:01 +08:00
#include <blah/containers/str.h>
2020-10-25 08:34:20 +08:00
#include <blah/streams/endian.h>
2020-10-25 15:54:19 +08:00
#include <string.h>
2020-08-26 15:38:01 +08:00
namespace Blah
{
class Stream
{
public:
2020-10-25 08:34:20 +08:00
Stream() = default;
2020-08-26 15:38:01 +08:00
Stream(const Stream&) = delete;
Stream& operator=(const Stream&) = delete;
virtual ~Stream() = default;
// returns the length of the stream
virtual size_t length() const = 0;
2020-08-26 15:38:01 +08:00
// returns the position of the stream
virtual size_t position() const = 0;
2020-08-26 15:38:01 +08:00
// seeks the position of the stream
virtual size_t seek(size_t position) = 0;
2020-08-26 15:38:01 +08:00
// 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;
2021-03-21 17:08:28 +08:00
// pipes the contents of this stream to another stream
size_t pipe(Stream& to, size_t length);
2020-08-26 15:38:01 +08:00
2020-10-25 08:34:20 +08:00
// reads the amount of bytes into the given buffer, and returns the amount read
2021-05-10 10:30:01 +08:00
size_t read(void* buffer, size_t length);
2020-10-25 08:34:20 +08:00
// reads a string. if length < 0, assumes null-terminated
2021-01-15 04:21:59 +08:00
String read_string(int length = -1);
2020-08-26 15:38:01 +08:00
2021-01-15 04:21:59 +08:00
// reads a string until a newline '\n' or null-terminator '\0' is found
String read_line();
2020-08-26 15:38:01 +08:00
2020-10-25 08:34:20 +08:00
// reads a number
2021-01-15 04:21:59 +08:00
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
2020-10-25 08:34:20 +08:00
T read()
{
return read<T>(Endian::Little);
}
2020-08-26 15:38:01 +08:00
2020-10-25 08:34:20 +08:00
// reads a number
2021-01-15 04:21:59 +08:00
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
2020-08-26 15:38:01 +08:00
T read(Endian endian)
{
2020-10-25 08:34:20 +08:00
T result;
read(&result, sizeof(T));
if (!Blah::is_endian(endian))
Blah::swap_endian(&result);
return result;
}
2020-08-26 15:38:01 +08:00
2020-10-25 08:34:20 +08:00
// writes the amount of bytes to the stream from the given buffer, and returns the amount written
size_t write(const void* buffer, size_t length);
2020-08-26 15:38:01 +08:00
// writes the contents of a string to the stream
size_t write(const String& string);
2020-10-25 08:34:20 +08:00
// writes a number
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
size_t write(const T& value)
2020-10-25 08:34:20 +08:00
{
return write<T>(value, Endian::Little);
}
2020-08-26 15:38:01 +08:00
2020-10-25 08:34:20 +08:00
// writes a number
template<typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
size_t write(const T& value, Endian endian)
2020-08-26 15:38:01 +08:00
{
T writing = value;
2020-10-25 08:34:20 +08:00
if (!Blah::is_endian(endian))
Blah::swap_endian(&writing);
2020-08-26 15:38:01 +08:00
return write(&writing, sizeof(T));
}
protected:
2021-05-10 10:30:01 +08:00
// reads the amount of bytes into the given buffer, and returns the amount read
virtual size_t read_data(void* buffer, size_t length) = 0;
2020-08-26 15:38:01 +08:00
2021-05-10 10:30:01 +08:00
// writes the amount of bytes to the stream from the given buffer, and returns the amount written
virtual size_t write_data(const void* buffer, size_t length) = 0;
2020-08-26 15:38:01 +08:00
};
2021-01-15 04:21:59 +08:00
}