mirror of https://github.com/OpenRCT2/OpenRCT2.git
97 lines
2.5 KiB
C++
97 lines
2.5 KiB
C++
![]() |
#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
|
||
|
/*****************************************************************************
|
||
|
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
|
||
|
*
|
||
|
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
|
||
|
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
|
||
|
*
|
||
|
* OpenRCT2 is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU General Public License as published by
|
||
|
* the Free Software Foundation, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* A full copy of the GNU General Public License can be found in licence.txt
|
||
|
*****************************************************************************/
|
||
|
#pragma endregion
|
||
|
|
||
|
#include <sstream>
|
||
|
#include "../core/FileStream.hpp"
|
||
|
#include "../platform/platform.h"
|
||
|
#include "IniWriter.h"
|
||
|
|
||
|
class IniWriter final : public IIniWriter
|
||
|
{
|
||
|
private:
|
||
|
FileStream _fs;
|
||
|
|
||
|
public:
|
||
|
IniWriter(const std::string &path)
|
||
|
: _fs(path, FILE_MODE_WRITE)
|
||
|
{
|
||
|
}
|
||
|
|
||
|
void WriteSection(const std::string &name) override
|
||
|
{
|
||
|
WriteLine("[" + name + "]");
|
||
|
}
|
||
|
|
||
|
void WriteBoolean(const std::string &name, bool value)
|
||
|
{
|
||
|
WriteProperty(name, value ? "true" : "false");
|
||
|
}
|
||
|
|
||
|
void WriteSint32(const std::string &name, sint32 value) override
|
||
|
{
|
||
|
WriteProperty(name, std::to_string(value));
|
||
|
}
|
||
|
|
||
|
void WriteFloat(const std::string &name, float value) override
|
||
|
{
|
||
|
WriteProperty(name, std::to_string(value));
|
||
|
}
|
||
|
|
||
|
void WriteString(const std::string &name, const std::string &value) override
|
||
|
{
|
||
|
std::ostringstream buffer;
|
||
|
buffer << '"';
|
||
|
for (char c : value)
|
||
|
{
|
||
|
if (c == '\\' || c == '"')
|
||
|
{
|
||
|
buffer << '\\';
|
||
|
}
|
||
|
buffer << c;
|
||
|
}
|
||
|
buffer << '"';
|
||
|
|
||
|
WriteProperty(name, buffer.str());
|
||
|
}
|
||
|
|
||
|
void WriteEnum(const std::string &name, const std::string &key) override
|
||
|
{
|
||
|
WriteProperty(name, key);
|
||
|
}
|
||
|
|
||
|
private:
|
||
|
void WriteProperty(const std::string &name, const std::string &value)
|
||
|
{
|
||
|
WriteLine(name + " = " + value);
|
||
|
}
|
||
|
|
||
|
void WriteLine(const std::string &line)
|
||
|
{
|
||
|
_fs.Write(line.c_str(), line.size());
|
||
|
_fs.Write(PLATFORM_NEWLINE, String::SizeOf(PLATFORM_NEWLINE));
|
||
|
}
|
||
|
};
|
||
|
|
||
|
void IIniWriter::WriteString(const std::string &name, const utf8 * value)
|
||
|
{
|
||
|
WriteString(name, String::ToStd(value));
|
||
|
}
|
||
|
|
||
|
IIniWriter * CreateIniWriter(const std::string &path)
|
||
|
{
|
||
|
return new IniWriter(path);
|
||
|
}
|