SettingsInterface: Add Error to Save()

This commit is contained in:
Stenzek 2024-03-31 15:50:29 +10:00
parent eb504143c1
commit 693982d755
No known key found for this signature in database
7 changed files with 30 additions and 16 deletions

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "layered_settings_interface.h" #include "layered_settings_interface.h"
@ -9,7 +9,7 @@ LayeredSettingsInterface::LayeredSettingsInterface() = default;
LayeredSettingsInterface::~LayeredSettingsInterface() = default; LayeredSettingsInterface::~LayeredSettingsInterface() = default;
bool LayeredSettingsInterface::Save() bool LayeredSettingsInterface::Save(Error* error /* = nullptr */)
{ {
Panic("Attempting to save layered settings interface"); Panic("Attempting to save layered settings interface");
} }

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
@ -23,7 +23,7 @@ public:
SettingsInterface* GetLayer(Layer layer) const { return m_layers[layer]; } SettingsInterface* GetLayer(Layer layer) const { return m_layers[layer]; }
void SetLayer(Layer layer, SettingsInterface* sif) { m_layers[layer] = sif; } void SetLayer(Layer layer, SettingsInterface* sif) { m_layers[layer] = sif; }
bool Save() override; bool Save(Error* error = nullptr) override;
void Clear() override; void Clear() override;

View file

@ -1,16 +1,19 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "memory_settings_interface.h" #include "memory_settings_interface.h"
#include "common/assert.h" #include "common/assert.h"
#include "common/error.h"
#include "common/string_util.h" #include "common/string_util.h"
MemorySettingsInterface::MemorySettingsInterface() = default; MemorySettingsInterface::MemorySettingsInterface() = default;
MemorySettingsInterface::~MemorySettingsInterface() = default; MemorySettingsInterface::~MemorySettingsInterface() = default;
bool MemorySettingsInterface::Save() bool MemorySettingsInterface::Save(Error* error /* = nullptr */)
{ {
Error::SetStringView(error, "Memory settings cannot be saved.");
return false; return false;
} }

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
@ -12,7 +12,7 @@ public:
MemorySettingsInterface(); MemorySettingsInterface();
~MemorySettingsInterface(); ~MemorySettingsInterface();
bool Save() override; bool Save(Error* error = nullptr) override;
void Clear() override; void Clear() override;

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
@ -8,12 +8,14 @@
#include <string> #include <string>
#include <vector> #include <vector>
class Error;
class SettingsInterface class SettingsInterface
{ {
public: public:
virtual ~SettingsInterface() = default; virtual ~SettingsInterface() = default;
virtual bool Save() = 0; virtual bool Save(Error* error = nullptr) = 0;
virtual void Clear() = 0; virtual void Clear() = 0;
virtual bool GetIntValue(const char* section, const char* key, s32* value) const = 0; virtual bool GetIntValue(const char* section, const char* key, s32* value) const = 0;

View file

@ -1,14 +1,18 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#include "ini_settings_interface.h" #include "ini_settings_interface.h"
#include "common/file_system.h" #include "common/file_system.h"
#include "common/log.h" #include "common/log.h"
#include "common/path.h" #include "common/path.h"
#include "common/string_util.h" #include "common/string_util.h"
#include "common/error.h"
#include <algorithm> #include <algorithm>
#include <iterator> #include <iterator>
#include <mutex> #include <mutex>
Log_SetChannel(INISettingsInterface); Log_SetChannel(INISettingsInterface);
#ifdef _WIN32 #ifdef _WIN32
@ -64,15 +68,18 @@ bool INISettingsInterface::Load()
return (err == SI_OK); return (err == SI_OK);
} }
bool INISettingsInterface::Save() bool INISettingsInterface::Save(Error* error /* = nullptr */)
{ {
if (m_filename.empty()) if (m_filename.empty())
{
Error::SetStringView(error, "Filename is not set.");
return false; return false;
}
std::unique_lock lock(s_ini_load_save_mutex); std::unique_lock lock(s_ini_load_save_mutex);
std::string temp_filename(GetTemporaryFileName(m_filename)); std::string temp_filename(GetTemporaryFileName(m_filename));
SI_Error err = SI_FAIL; SI_Error err = SI_FAIL;
std::FILE* fp = FileSystem::OpenCFile(temp_filename.c_str(), "wb"); std::FILE* fp = FileSystem::OpenCFile(temp_filename.c_str(), "wb", error);
if (fp) if (fp)
{ {
err = m_ini.SaveFile(fp, false); err = m_ini.SaveFile(fp, false);
@ -80,10 +87,12 @@ bool INISettingsInterface::Save()
if (err != SI_OK) if (err != SI_OK)
{ {
Error::SetStringFmt(error, "INI SaveFile() failed: {}", static_cast<int>(err));
// remove temporary file // remove temporary file
FileSystem::DeleteFile(temp_filename.c_str()); FileSystem::DeleteFile(temp_filename.c_str());
} }
else if (!FileSystem::RenamePath(temp_filename.c_str(), m_filename.c_str())) else if (!FileSystem::RenamePath(temp_filename.c_str(), m_filename.c_str(), error))
{ {
Log_ErrorPrintf("Failed to rename '%s' to '%s'", temp_filename.c_str(), m_filename.c_str()); Log_ErrorPrintf("Failed to rename '%s' to '%s'", temp_filename.c_str(), m_filename.c_str());
FileSystem::DeleteFile(temp_filename.c_str()); FileSystem::DeleteFile(temp_filename.c_str());

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com> // SPDX-FileCopyrightText: 2019-2024 Connor McLaughlin <stenzek@gmail.com>
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0) // SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
#pragma once #pragma once
@ -19,7 +19,7 @@ public:
const std::string& GetFileName() const { return m_filename; } const std::string& GetFileName() const { return m_filename; }
bool Load(); bool Load();
bool Save() override; bool Save(Error* error = nullptr) override;
void Clear() override; void Clear() override;