Skip to content

Commit

Permalink
cdvdgigaherz: Add portable setting class
Browse files Browse the repository at this point in the history
Can load/save inis (though functionality is very basic).
  • Loading branch information
turtleli committed Nov 7, 2016
1 parent 9c440ab commit dce3c57
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
113 changes: 113 additions & 0 deletions plugins/cdvdGigaherz/src/Settings.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#include "Settings.h"

#if defined(_WIN32)
#include <codecvt>
#endif
#include <fstream>
#include <locale>
#include <string>

Settings::Settings()
{
}

Settings::Settings(std::map<std::string, std::string> 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<std::codecvt_utf8_utf16<wchar_t>> 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<std::codecvt_utf8_utf16<wchar_t>> converter;
data = converter.from_bytes(it->second);
return true;
}
#endif
41 changes: 41 additions & 0 deletions plugins/cdvdGigaherz/src/Settings.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <map>
#include <string>

class Settings
{
private:
std::map<std::string, std::string> m_data;

void TrimWhitespace(std::string &str) const;

public:
Settings();
Settings(std::map<std::string, std::string> 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
};
2 changes: 2 additions & 0 deletions plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\CDVD.cpp" />
<ClCompile Include="..\Settings.cpp" />
<ClCompile Include="config.cpp" />
<ClCompile Include="IOCtlSrc.cpp" />
<ClCompile Include="..\ReadThread.cpp" />
<ClCompile Include="..\TocStuff.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\CDVD.h" />
<ClInclude Include="..\Settings.h" />
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>
Expand Down
6 changes: 6 additions & 0 deletions plugins/cdvdGigaherz/src/Windows/cdvdGigaherz.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
<ClCompile Include="..\TocStuff.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Settings.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\CDVD.h">
Expand All @@ -38,6 +41,9 @@
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\Settings.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="cdvdGigaherz.rc">
Expand Down

0 comments on commit dce3c57

Please sign in to comment.