2016-05-04 19:24:41 +02:00
|
|
|
/*****************************************************************************
|
2018-06-15 14:07:34 +02:00
|
|
|
* Copyright (c) 2014-2018 OpenRCT2 developers
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
2016-05-04 19:24:41 +02:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2016-05-04 19:24:41 +02:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2015-08-29 03:05:21 +02:00
|
|
|
#pragma once
|
|
|
|
|
2018-06-01 22:08:40 +02:00
|
|
|
#include <istream>
|
2018-01-02 20:23:22 +01:00
|
|
|
#include <stdexcept>
|
2016-10-22 19:06:52 +02:00
|
|
|
#include <string>
|
2018-05-26 16:29:18 +02:00
|
|
|
#include <vector>
|
2015-08-29 03:05:21 +02:00
|
|
|
#include "../common.h"
|
2016-06-25 23:07:01 +02:00
|
|
|
#include "Memory.hpp"
|
2015-08-29 03:05:21 +02:00
|
|
|
|
|
|
|
enum {
|
2016-01-23 19:58:31 +01:00
|
|
|
STREAM_SEEK_BEGIN,
|
|
|
|
STREAM_SEEK_CURRENT,
|
|
|
|
STREAM_SEEK_END
|
2015-08-29 03:05:21 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a stream that can be read or written to. Implemented by types such as FileStream, NetworkStream or MemoryStream.
|
|
|
|
*/
|
2016-06-25 13:04:12 +02:00
|
|
|
interface IStream
|
2016-01-23 19:58:31 +01:00
|
|
|
{
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Interface methods
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2016-06-25 13:04:12 +02:00
|
|
|
virtual ~IStream() { }
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
virtual bool CanRead() const abstract;
|
|
|
|
virtual bool CanWrite() const abstract;
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
virtual uint64 GetLength() const abstract;
|
|
|
|
virtual uint64 GetPosition() const abstract;
|
|
|
|
virtual void SetPosition(uint64 position) abstract;
|
2017-01-04 22:17:08 +01:00
|
|
|
virtual void Seek(sint64 offset, sint32 origin) abstract;
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
virtual void Read(void * buffer, uint64 length) abstract;
|
|
|
|
virtual void Write(const void * buffer, uint64 length) abstract;
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-06-28 22:16:09 +02:00
|
|
|
virtual uint64 TryRead(void * buffer, uint64 length) abstract;
|
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helper methods
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Reads the size of the given type from the stream directly into the given address.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void Read(T * value)
|
|
|
|
{
|
|
|
|
Read(value, sizeof(T));
|
|
|
|
}
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Writes the size of the given type to the stream directly from the given address.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void Write(const T * value)
|
|
|
|
{
|
|
|
|
Write(value, sizeof(T));
|
|
|
|
}
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Reads the given type from the stream. Use this only for small types (e.g. sint8, sint64, double)
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
T ReadValue()
|
|
|
|
{
|
|
|
|
T buffer;
|
|
|
|
Read(&buffer);
|
|
|
|
return buffer;
|
|
|
|
}
|
2015-08-29 03:05:21 +02:00
|
|
|
|
2016-01-23 19:58:31 +01:00
|
|
|
/**
|
|
|
|
* Writes the given type to the stream. Use this only for small types (e.g. sint8, sint64, double)
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
void WriteValue(const T value)
|
|
|
|
{
|
|
|
|
Write(&value);
|
|
|
|
}
|
2016-06-24 22:16:51 +02:00
|
|
|
|
2016-06-25 23:07:01 +02:00
|
|
|
template<typename T>
|
|
|
|
T * ReadArray(size_t count)
|
|
|
|
{
|
|
|
|
T * buffer = Memory::AllocateArray<T>(count);
|
|
|
|
Read(buffer, sizeof(T) * count);
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void WriteArray(T * buffer, size_t count)
|
|
|
|
{
|
|
|
|
Write(buffer, sizeof(T) * count);
|
|
|
|
}
|
|
|
|
|
2016-06-24 22:16:51 +02:00
|
|
|
utf8 * ReadString();
|
2016-10-23 22:27:44 +02:00
|
|
|
std::string ReadStdString();
|
2016-10-22 19:06:52 +02:00
|
|
|
void WriteString(const utf8 * str);
|
|
|
|
void WriteString(const std::string &string);
|
2015-08-29 03:05:21 +02:00
|
|
|
};
|
|
|
|
|
2018-01-02 20:23:22 +01:00
|
|
|
class IOException : public std::runtime_error
|
2016-01-23 19:58:31 +01:00
|
|
|
{
|
2015-08-29 03:05:21 +02:00
|
|
|
public:
|
2018-01-02 20:23:22 +01:00
|
|
|
explicit IOException(const std::string &message) : std::runtime_error(message) { }
|
2015-08-29 03:05:21 +02:00
|
|
|
};
|
2018-05-26 16:29:18 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class ivstream : public std::istream
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
class vector_streambuf : public std::basic_streambuf<char, std::char_traits<char>>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit vector_streambuf(const std::vector<T>& vec)
|
|
|
|
{
|
|
|
|
this->setg((char *)vec.data(), (char *)vec.data(), (char *)(vec.data() + vec.size()));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
vector_streambuf _streambuf;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ivstream(const std::vector<T>& vec)
|
|
|
|
: std::istream(&_streambuf),
|
|
|
|
_streambuf(vec)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|