2017-02-16 20:12:30 +01:00
|
|
|
/*****************************************************************************
|
2020-07-21 15:04:34 +02:00
|
|
|
* Copyright (c) 2014-2020 OpenRCT2 developers
|
2017-02-16 20:12:30 +01: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
|
2017-02-16 20:12:30 +01:00
|
|
|
*
|
2018-06-15 14:07:34 +02:00
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
2017-02-16 20:12:30 +01:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2017-02-22 18:46:10 +01:00
|
|
|
#include "IniWriter.hpp"
|
2017-02-16 20:12:30 +01:00
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
#include "../core/IStream.hpp"
|
|
|
|
#include "../core/String.hpp"
|
2022-02-18 21:57:00 +01:00
|
|
|
#include "../platform/Platform.h"
|
2017-06-25 19:49:04 +02:00
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2017-02-16 20:12:30 +01:00
|
|
|
class IniWriter final : public IIniWriter
|
|
|
|
{
|
|
|
|
private:
|
2020-07-30 23:01:22 +02:00
|
|
|
OpenRCT2::IStream* _stream;
|
2018-06-22 22:58:08 +02:00
|
|
|
bool _firstSection = true;
|
2017-02-16 20:12:30 +01:00
|
|
|
|
|
|
|
public:
|
2020-07-30 23:01:22 +02:00
|
|
|
explicit IniWriter(OpenRCT2::IStream* stream)
|
2017-02-22 18:52:33 +01:00
|
|
|
: _stream(stream)
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteSection(const std::string& name) override
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
2017-02-19 22:37:06 +01:00
|
|
|
if (!_firstSection)
|
|
|
|
{
|
|
|
|
WriteLine();
|
|
|
|
}
|
|
|
|
_firstSection = false;
|
|
|
|
|
2017-02-16 20:12:30 +01:00
|
|
|
WriteLine("[" + name + "]");
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteBoolean(const std::string& name, bool value) override
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
WriteProperty(name, value ? "true" : "false");
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteInt32(const std::string& name, int32_t value) override
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
WriteProperty(name, std::to_string(value));
|
|
|
|
}
|
|
|
|
|
2020-07-25 09:37:35 +02:00
|
|
|
void WriteInt64(const std::string& name, int64_t value) override
|
|
|
|
{
|
|
|
|
WriteProperty(name, std::to_string(value));
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteFloat(const std::string& name, float value) override
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
WriteProperty(name, std::to_string(value));
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteString(const std::string& name, const std::string& value) override
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
std::ostringstream buffer;
|
|
|
|
buffer << '"';
|
|
|
|
for (char c : value)
|
|
|
|
{
|
|
|
|
if (c == '\\' || c == '"')
|
|
|
|
{
|
|
|
|
buffer << '\\';
|
|
|
|
}
|
|
|
|
buffer << c;
|
|
|
|
}
|
|
|
|
buffer << '"';
|
|
|
|
|
|
|
|
WriteProperty(name, buffer.str());
|
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteEnum(const std::string& name, const std::string& key) override
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
WriteProperty(name, key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteProperty(const std::string& name, const std::string& value)
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
WriteLine(name + " = " + value);
|
|
|
|
}
|
|
|
|
|
2017-02-19 22:37:06 +01:00
|
|
|
void WriteLine()
|
|
|
|
{
|
2017-02-22 18:52:33 +01:00
|
|
|
_stream->Write(PLATFORM_NEWLINE, String::SizeOf(PLATFORM_NEWLINE));
|
2017-02-19 22:37:06 +01:00
|
|
|
}
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void WriteLine(const std::string& line)
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
2017-02-22 18:52:33 +01:00
|
|
|
_stream->Write(line.c_str(), line.size());
|
2017-02-19 22:37:06 +01:00
|
|
|
WriteLine();
|
2017-02-16 20:12:30 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-22 22:58:08 +02:00
|
|
|
void IIniWriter::WriteString(const std::string& name, const utf8* value)
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
|
|
|
WriteString(name, String::ToStd(value));
|
|
|
|
}
|
|
|
|
|
2020-09-16 18:32:55 +02:00
|
|
|
std::unique_ptr<IIniWriter> CreateIniWriter(OpenRCT2::IStream* stream)
|
2017-02-16 20:12:30 +01:00
|
|
|
{
|
2020-09-16 18:32:55 +02:00
|
|
|
return std::make_unique<IniWriter>(stream);
|
2017-02-16 20:12:30 +01:00
|
|
|
}
|