From dce3c57e6a8bc581f79e430351b93ace7e24ca48 Mon Sep 17 00:00:00 2001 From: Jonathan Li Date: Mon, 24 Oct 2016 20:16:00 +0100 Subject: [PATCH] cdvdgigaherz: Add portable setting class Can load/save inis (though functionality is very basic). --- plugins/cdvdGigaherz/src/Settings.cpp | 113 ++++++++++++++++++ plugins/cdvdGigaherz/src/Settings.h | 41 +++++++ .../src/Windows/cdvdGigaherz.vcxproj | 2 + .../src/Windows/cdvdGigaherz.vcxproj.filters | 6 + 4 files changed, 162 insertions(+) create mode 100644 plugins/cdvdGigaherz/src/Settings.cpp create mode 100644 plugins/cdvdGigaherz/src/Settings.h diff --git a/plugins/cdvdGigaherz/src/Settings.cpp b/plugins/cdvdGigaherz/src/Settings.cpp new file mode 100644 index 0000000000..685d4b2500 --- /dev/null +++ b/plugins/cdvdGigaherz/src/Settings.cpp @@ -0,0 +1,113 @@ +/* PCSX2 - PS2 Emulator for PCs + * Copyright (C) 2016 PCSX2 Dev Team + * + * PCSX2 is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with PCSX2. + * If not, see . + */ + +#include "Settings.h" + +#if defined(_WIN32) +#include +#endif +#include +#include +#include + +Settings::Settings() +{ +} + +Settings::Settings(std::map data) + : m_data(data) +{ +} + +void Settings::TrimWhitespace(std::string &str) const +{ + // Leading whitespace + str.erase(0, str.find_first_not_of(" \r\t")); + // Trailing whitespace + auto pos = str.find_last_not_of(" \r\t"); + if (pos != std::string::npos && pos != str.size() - 1) + str.erase(pos + 1); +} + +void Settings::Load(const std::string &filename) +{ + std::ifstream file(filename); + if (!file.is_open()) + return; + + while (!file.eof()) { + std::string line; + std::getline(file, line); + + auto separator = line.find('='); + if (separator == std::string::npos) + continue; + + std::string key = line.substr(0, separator); + // Trim leading and trailing whitespace + TrimWhitespace(key); + if (key.empty()) + continue; + + std::string value = line.substr(separator + 1); + TrimWhitespace(value); + + Set(key, value); + } +} + +void Settings::Save(const std::string &filename) const +{ + std::ofstream file(filename, std::ios::trunc); + if (!file.is_open()) + return; + + for (const auto &pair : m_data) + file << pair.first << '=' << pair.second << '\n'; +} + +void Settings::Set(std::string key, std::string value) +{ + m_data[key] = value; +} + +bool Settings::Get(const std::string &key, std::string &data) const +{ + auto it = m_data.find(key); + if (it == m_data.end()) + return false; + + data = it->second; + return true; +} + +#if defined(_WIN32) +void Settings::Set(std::string key, std::wstring value) +{ + std::wstring_convert> converter; + m_data[key] = converter.to_bytes(value); +} + +bool Settings::Get(const std::string &key, std::wstring &data) const +{ + auto it = m_data.find(key); + if (it == m_data.end()) + return false; + + std::wstring_convert> converter; + data = converter.from_bytes(it->second); + return true; +} +#endif diff --git a/plugins/cdvdGigaherz/src/Settings.h b/plugins/cdvdGigaherz/src/Settings.h new file mode 100644 index 0000000000..520ad8431e --- /dev/null +++ b/plugins/cdvdGigaherz/src/Settings.h @@ -0,0 +1,41 @@ +/* PCSX2 - PS2 Emulator for PCs + * Copyright (C) 2016 PCSX2 Dev Team + * + * PCSX2 is free software: you can redistribute it and/or modify it under the terms + * of the GNU Lesser General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with PCSX2. + * If not, see . + */ + +#pragma once + +#include +#include + +class Settings +{ +private: + std::map m_data; + + void TrimWhitespace(std::string &str) const; + +public: + Settings(); + Settings(std::map data); + + void Load(const std::string &filename); + void Save(const std::string &filename) const; + + void Set(std::string key, std::string value); + bool Get(const std::string &key, std::string &data) const; +#if defined(_WIN32) + void Set(std::string key, std::wstring value); + bool Get(const std::string &key, std::wstring &data) const; +#endif +}; diff --git a/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj b/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj index de5d2f9fb0..e84528ad89 100644 --- a/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj +++ b/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj @@ -53,6 +53,7 @@ + @@ -60,6 +61,7 @@ + diff --git a/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj.filters b/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj.filters index cc9463670e..55d56aa662 100644 --- a/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj.filters +++ b/plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj.filters @@ -30,6 +30,9 @@ Source Files + + Source Files + @@ -38,6 +41,9 @@ Header Files + + Header Files +