mirror of https://github.com/OpenRCT2/OpenRCT2.git
Use IStream for IniReader and IniWriter
This commit is contained in:
parent
6e9d22e99d
commit
4dc792bc1d
|
@ -17,6 +17,7 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include "../core/Console.hpp"
|
#include "../core/Console.hpp"
|
||||||
#include "../core/Exception.hpp"
|
#include "../core/Exception.hpp"
|
||||||
|
#include "../core/FileStream.hpp"
|
||||||
#include "../core/Memory.hpp"
|
#include "../core/Memory.hpp"
|
||||||
#include "../core/Path.hpp"
|
#include "../core/Path.hpp"
|
||||||
#include "../core/String.hpp"
|
#include "../core/String.hpp"
|
||||||
|
@ -505,7 +506,8 @@ namespace Config
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto reader = std::unique_ptr<IIniReader>(CreateIniReader(path));
|
auto fs = FileStream(path, FILE_MODE_OPEN);
|
||||||
|
auto reader = std::unique_ptr<IIniReader>(CreateIniReader(&fs));
|
||||||
ReadGeneral(reader.get());
|
ReadGeneral(reader.get());
|
||||||
ReadInterface(reader.get());
|
ReadInterface(reader.get());
|
||||||
ReadSound(reader.get());
|
ReadSound(reader.get());
|
||||||
|
@ -525,7 +527,8 @@ namespace Config
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto writer = std::unique_ptr<IIniWriter>(CreateIniWriter(path));
|
auto fs = FileStream(path, FILE_MODE_WRITE);
|
||||||
|
auto writer = std::unique_ptr<IIniWriter>(CreateIniWriter(&fs));
|
||||||
WriteGeneral(writer.get());
|
WriteGeneral(writer.get());
|
||||||
WriteInterface(writer.get());
|
WriteInterface(writer.get());
|
||||||
WriteSound(writer.get());
|
WriteSound(writer.get());
|
||||||
|
|
|
@ -60,12 +60,11 @@ private:
|
||||||
std::unordered_map<std::string, std::string> _values;
|
std::unordered_map<std::string, std::string> _values;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IniReader(const std::string &path)
|
IniReader(IStream * stream)
|
||||||
{
|
{
|
||||||
auto fs = FileStream(path, FILE_MODE_OPEN);
|
uint64 length = stream->GetLength() - stream->GetPosition();
|
||||||
uint64 length = fs.GetLength();
|
|
||||||
_buffer.resize(length);
|
_buffer.resize(length);
|
||||||
fs.Read(_buffer.data(), length);
|
stream->Read(_buffer.data(), length);
|
||||||
|
|
||||||
RemoveBOM();
|
RemoveBOM();
|
||||||
|
|
||||||
|
@ -368,9 +367,9 @@ utf8 * IIniReader::GetCString(const std::string &name, const utf8 * defaultValue
|
||||||
return String::Duplicate(szValue.c_str());
|
return String::Duplicate(szValue.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
IIniReader * CreateIniReader(const std::string &path)
|
IIniReader * CreateIniReader(IStream * stream)
|
||||||
{
|
{
|
||||||
return new IniReader(path);
|
return new IniReader(stream);
|
||||||
}
|
}
|
||||||
|
|
||||||
IIniReader * CreateDefaultIniReader()
|
IIniReader * CreateDefaultIniReader()
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
#include "ConfigEnum.hpp"
|
#include "ConfigEnum.hpp"
|
||||||
|
|
||||||
|
interface IStream;
|
||||||
|
|
||||||
interface IIniReader
|
interface IIniReader
|
||||||
{
|
{
|
||||||
virtual ~IIniReader() = default;
|
virtual ~IIniReader() = default;
|
||||||
|
@ -45,5 +47,5 @@ interface IIniReader
|
||||||
utf8 * GetCString(const std::string &name, const utf8 * defaultValue) const;
|
utf8 * GetCString(const std::string &name, const utf8 * defaultValue) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
IIniReader * CreateIniReader(const std::string &path);
|
IIniReader * CreateIniReader(IStream * stream);
|
||||||
IIniReader * CreateDefaultIniReader();
|
IIniReader * CreateDefaultIniReader();
|
||||||
|
|
|
@ -22,12 +22,12 @@
|
||||||
class IniWriter final : public IIniWriter
|
class IniWriter final : public IIniWriter
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
FileStream _fs;
|
IStream * _stream;
|
||||||
bool _firstSection = false;
|
bool _firstSection = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
IniWriter(const std::string &path)
|
IniWriter(IStream * stream)
|
||||||
: _fs(path, FILE_MODE_WRITE)
|
: _stream(stream)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,12 +87,12 @@ private:
|
||||||
|
|
||||||
void WriteLine()
|
void WriteLine()
|
||||||
{
|
{
|
||||||
_fs.Write(PLATFORM_NEWLINE, String::SizeOf(PLATFORM_NEWLINE));
|
_stream->Write(PLATFORM_NEWLINE, String::SizeOf(PLATFORM_NEWLINE));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteLine(const std::string &line)
|
void WriteLine(const std::string &line)
|
||||||
{
|
{
|
||||||
_fs.Write(line.c_str(), line.size());
|
_stream->Write(line.c_str(), line.size());
|
||||||
WriteLine();
|
WriteLine();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -102,7 +102,7 @@ void IIniWriter::WriteString(const std::string &name, const utf8 * value)
|
||||||
WriteString(name, String::ToStd(value));
|
WriteString(name, String::ToStd(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
IIniWriter * CreateIniWriter(const std::string &path)
|
IIniWriter * CreateIniWriter(IStream * stream)
|
||||||
{
|
{
|
||||||
return new IniWriter(path);
|
return new IniWriter(stream);
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@
|
||||||
#include "../common.h"
|
#include "../common.h"
|
||||||
#include "ConfigEnum.hpp"
|
#include "ConfigEnum.hpp"
|
||||||
|
|
||||||
|
interface IStream;
|
||||||
|
|
||||||
interface IIniWriter
|
interface IIniWriter
|
||||||
{
|
{
|
||||||
virtual ~IIniWriter() = default;
|
virtual ~IIniWriter() = default;
|
||||||
|
@ -47,4 +49,4 @@ interface IIniWriter
|
||||||
void WriteString(const std::string &name, const utf8 * value);
|
void WriteString(const std::string &name, const utf8 * value);
|
||||||
};
|
};
|
||||||
|
|
||||||
IIniWriter * CreateIniWriter(const std::string &path);
|
IIniWriter * CreateIniWriter(IStream * stream);
|
||||||
|
|
Loading…
Reference in New Issue