1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-15 19:13:07 +01:00

Implement IniWriter

This commit is contained in:
Ted John
2017-02-16 19:12:30 +00:00
parent 2fc5012670
commit 23fef54b14
6 changed files with 205 additions and 2 deletions

View File

@@ -339,7 +339,7 @@ notification_configuration gConfigNotifications;
font_configuration gConfigFonts;
bool config_open(const utf8string path);
static bool config_save(const utf8string path);
bool config_save(const utf8string path);
static void config_read_properties(config_section_definition **currentSection, const_utf8string line);
static void config_save_property_value(SDL_RWops *file, uint8 type, value_union *value);
static bool config_read_enum(void *dest, sint32 destSize, const utf8 *key, sint32 keySize, config_enum_definition *enumDefinitions);
@@ -557,7 +557,7 @@ static bool config_open_2(const utf8string path)
return true;
}
bool config_save(const utf8string path)
static bool config_save_2(const utf8string path)
{
SDL_RWops *file;
sint32 i, j;

View File

@@ -20,6 +20,7 @@
#include "../network/network.h"
#include "Config.h"
#include "IniReader.h"
#include "IniWriter.h"
extern "C"
{
@@ -190,6 +191,10 @@ namespace Config
}
}
static void WriteGeneral(IIniWriter * writer)
{
}
static void ReadInterface(IIniReader * reader)
{
if (reader->ReadSection("interface"))
@@ -207,6 +212,10 @@ namespace Config
}
}
static void WriteInterface(IIniWriter * writer)
{
}
static void ReadSound(IIniReader * reader)
{
if (reader->ReadSection("sound"))
@@ -223,6 +232,10 @@ namespace Config
}
}
static void WriteSound(IIniWriter * writer)
{
}
static void ReadNetwork(IIniReader * reader)
{
if (reader->ReadSection("network"))
@@ -246,6 +259,10 @@ namespace Config
}
}
static void WriteNetwork(IIniWriter * writer)
{
}
static void ReadNotifications(IIniReader * reader)
{
if (reader->ReadSection("notifications"))
@@ -271,6 +288,10 @@ namespace Config
}
}
static void WriteNotifications(IIniWriter * writer)
{
}
static void ReadTwitch(IIniReader * reader)
{
if (reader->ReadSection("sound"))
@@ -285,6 +306,10 @@ namespace Config
}
}
static void WriteTwitch(IIniWriter * writer)
{
}
static void ReadFont(IIniReader * reader)
{
if (reader->ReadSection("font"))
@@ -305,6 +330,10 @@ namespace Config
}
}
static void WriteFont(IIniWriter * writer)
{
}
static bool ReadFile(const std::string &path)
{
try
@@ -324,6 +353,26 @@ namespace Config
return false;
}
}
static bool WriteFile(const std::string &path)
{
try
{
auto writer = std::unique_ptr<IIniWriter>(CreateIniWriter(path));
WriteGeneral(writer.get());
WriteInterface(writer.get());
WriteSound(writer.get());
WriteNetwork(writer.get());
WriteNotifications(writer.get());
WriteTwitch(writer.get());
WriteFont(writer.get());
return true;
}
catch (const Exception &)
{
return false;
}
}
}
extern "C"
@@ -332,4 +381,9 @@ extern "C"
{
return Config::ReadFile(path);
}
bool config_save(const utf8 * path)
{
return Config::WriteFile(path);
}
}

View File

@@ -21,4 +21,5 @@
extern "C"
{
bool config_open(const utf8 * path);
bool config_save(const utf8 * path);
}

View File

@@ -0,0 +1,96 @@
#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);
}

View File

@@ -0,0 +1,50 @@
#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 <string>
#include "../common.h"
#include "ConfigEnum.h"
interface IIniWriter
{
virtual ~IIniWriter() = default;
virtual void WriteSection(const std::string &name) abstract;
virtual void WriteBoolean(const std::string &name, bool value) abstract;
virtual void WriteSint32(const std::string &name, sint32 value) abstract;
virtual void WriteFloat(const std::string &name, float value) abstract;
virtual void WriteString(const std::string &name, const std::string &value) abstract;
virtual void WriteEnum(const std::string &name, const std::string &key) abstract;
template<typename T>
void WriteEnum(const std::string &name, T value, const IConfigEnum<T> &configEnum)
{
std::string key = configEnum.GetName(value);
if (key.empty)
{
WriteSint32(value);
}
else
{
WriteEnum(name, key);
}
}
void WriteString(const std::string &name, const utf8 * value);
};
IIniWriter * CreateIniWriter(const std::string &path);

View File

@@ -74,6 +74,7 @@
<ClCompile Include="audio\NullAudioSource.cpp" />
<ClCompile Include="config\Config.cpp" />
<ClCompile Include="config\IniReader.cpp" />
<ClCompile Include="config\IniWriter.cpp" />
<ClCompile Include="FileClassifier.cpp" />
<ClCompile Include="network\ServerList.cpp" />
<ClCompile Include="ParkImporter.cpp" />
@@ -424,6 +425,7 @@
<ClInclude Include="config\Config.h" />
<ClInclude Include="config\ConfigEnum.h" />
<ClInclude Include="config\IniReader.h" />
<ClInclude Include="config\IniWriter.h" />
<ClInclude Include="FileClassifier.h" />
<ClInclude Include="network\ServerList.h" />
<ClInclude Include="rct12.h" />