mirror of
https://github.com/RPCS3/rpcs3.git
synced 2025-07-06 06:51:26 +12:00
use non-wx header only Ini library in preparation of the core/gui devide
This commit is contained in:
parent
c00f4b6022
commit
2d77415cc3
8 changed files with 4434 additions and 170 deletions
190
rpcs3/Ini.cpp
190
rpcs3/Ini.cpp
|
@ -1,10 +1,30 @@
|
|||
#include "stdafx.h"
|
||||
#include "Ini.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <wx/msw/iniconf.h>
|
||||
#endif
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
|
||||
#define DEF_CONFIG_NAME "./rpcs3.ini"
|
||||
|
||||
CSimpleIniCaseA *getIniFile()
|
||||
{
|
||||
static bool inited = false;
|
||||
static CSimpleIniCaseA ini;
|
||||
if (inited == false)
|
||||
{
|
||||
ini.SetUnicode(true);
|
||||
ini.LoadFile(DEF_CONFIG_NAME);
|
||||
inited = true;
|
||||
}
|
||||
return &ini;
|
||||
}
|
||||
|
||||
void saveIniFile()
|
||||
{
|
||||
getIniFile()->SaveFile(DEF_CONFIG_NAME);
|
||||
}
|
||||
|
||||
std::pair<int, int> rDefaultSize = { -1, -1 };
|
||||
Inis Ini;
|
||||
|
||||
static bool StringToBool(const wxString& str)
|
||||
|
@ -29,42 +49,49 @@ static wxString BoolToString(const bool b)
|
|||
return "false";
|
||||
}
|
||||
|
||||
static wxSize StringToSize(const wxString& str)
|
||||
//takes a string of format "[number]x[number]" and returns a pair of ints
|
||||
//example input would be "123x456" and the returned value would be {123,456}
|
||||
static std::pair<int, int> StringToSize(const std::string& str)
|
||||
{
|
||||
wxSize ret;
|
||||
std::pair<int, int> ret;
|
||||
|
||||
wxString s[2] = {wxEmptyString, wxEmptyString};
|
||||
std::string s[2] = { "", "" };
|
||||
|
||||
for(uint i=0, a=0; i<str.Length(); ++i)
|
||||
for (uint i = 0, a = 0; i<str.size(); ++i)
|
||||
{
|
||||
if(!str(i, 1).CmpNoCase("x"))
|
||||
if (!fmt::CmpNoCase(str.substr(i, 1), "x"))
|
||||
{
|
||||
if(++a >= 2) return wxDefaultSize;
|
||||
if (++a >= 2) return rDefaultSize;
|
||||
continue;
|
||||
}
|
||||
|
||||
s[a] += str(i, 1);
|
||||
}
|
||||
|
||||
if(s[0].IsEmpty() || s[1].IsEmpty())
|
||||
{
|
||||
return wxDefaultSize;
|
||||
s[a] += str.substr(i, 1);
|
||||
}
|
||||
|
||||
s[0].ToLong((long*)&ret.x);
|
||||
s[1].ToLong((long*)&ret.y);
|
||||
|
||||
if(ret.x <= 0 || ret.y <= 0)
|
||||
if (s[0].empty() || s[1].empty())
|
||||
{
|
||||
return wxDefaultSize;
|
||||
return rDefaultSize;
|
||||
}
|
||||
|
||||
try{
|
||||
ret.first = std::stoi(s[0]);
|
||||
ret.first = std::stoi(s[1]);
|
||||
}
|
||||
catch (const std::invalid_argument &e)
|
||||
{
|
||||
return rDefaultSize;
|
||||
}
|
||||
if (ret.first < 0 || ret.second < 0)
|
||||
{
|
||||
return rDefaultSize;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static wxString SizeToString(const wxSize& size)
|
||||
static std::string SizeToString(const std::pair<int, int>& size)
|
||||
{
|
||||
return wxString::Format("%dx%d", size.x, size.y);
|
||||
return fmt::Format("%dx%d", size.first, size.second);
|
||||
}
|
||||
|
||||
static wxPoint StringToPosition(const wxString& str)
|
||||
|
@ -100,39 +127,39 @@ static wxPoint StringToPosition(const wxString& str)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static wxString PositionToString(const wxPoint& position)
|
||||
static WindowInfo StringToWindowInfo(const std::string& str)
|
||||
{
|
||||
return wxString::Format("%dx%d", position.x, position.y);
|
||||
}
|
||||
WindowInfo ret = WindowInfo(rDefaultSize, rDefaultSize);
|
||||
|
||||
static WindowInfo StringToWindowInfo(const wxString& str)
|
||||
{
|
||||
WindowInfo ret = WindowInfo(wxDefaultSize, wxDefaultPosition);
|
||||
std::string s[4] = { "", "", "", "" };
|
||||
|
||||
wxString s[4] = {wxEmptyString, wxEmptyString, wxEmptyString, wxEmptyString};
|
||||
|
||||
for(uint i=0, a=0; i<str.Length(); ++i)
|
||||
for (uint i = 0, a = 0; i<str.size(); ++i)
|
||||
{
|
||||
if(!str(i, 1).CmpNoCase("x") || !str(i, 1).CmpNoCase(":"))
|
||||
if (!fmt::CmpNoCase(str.substr(i, 1), "x") || !fmt::CmpNoCase(str.substr(i, 1), ":"))
|
||||
{
|
||||
if(++a >= 4) return WindowInfo::GetDefault();
|
||||
if (++a >= 4) return WindowInfo::GetDefault();
|
||||
continue;
|
||||
}
|
||||
|
||||
s[a] += str(i, 1);
|
||||
s[a] += str.substr(i, 1);
|
||||
}
|
||||
|
||||
if(s[0].IsEmpty() || s[1].IsEmpty() || s[2].IsEmpty() || s[3].IsEmpty())
|
||||
|
||||
if (s[0].empty() || s[1].empty() || s[2].empty() || s[3].empty())
|
||||
{
|
||||
return WindowInfo::GetDefault();
|
||||
}
|
||||
|
||||
s[0].ToLong((long*)&ret.size.x);
|
||||
s[1].ToLong((long*)&ret.size.y);
|
||||
s[2].ToLong((long*)&ret.position.x);
|
||||
s[3].ToLong((long*)&ret.position.y);
|
||||
|
||||
if(ret.size.x <= 0 || ret.size.y <= 0)
|
||||
try{
|
||||
ret.size.first = std::stoi(s[0]);
|
||||
ret.size.second = std::stoi(s[1]);
|
||||
ret.position.first = std::stoi(s[2]);
|
||||
ret.position.second = std::stoi(s[3]);
|
||||
}
|
||||
catch (const std::invalid_argument &e)
|
||||
{
|
||||
return WindowInfo::GetDefault();
|
||||
}
|
||||
if (ret.size.first <= 0 || ret.size.second <= 0)
|
||||
{
|
||||
return WindowInfo::GetDefault();
|
||||
}
|
||||
|
@ -140,86 +167,81 @@ static WindowInfo StringToWindowInfo(const wxString& str)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static wxString WindowInfoToString(const WindowInfo& wind)
|
||||
static std::string WindowInfoToString(const WindowInfo& wind)
|
||||
{
|
||||
const int px = wind.position.x < -wind.size.x ? -1 : wind.position.x;
|
||||
const int py = wind.position.y < -wind.size.y ? -1 : wind.position.y;
|
||||
return wxString::Format("%dx%d:%dx%d", wind.size.x, wind.size.y, px, py);
|
||||
const int px = wind.position.first < -wind.size.first ? -1 : wind.position.first;
|
||||
const int py = wind.position.second < -wind.size.second ? -1 : wind.position.second;
|
||||
return fmt::Format("%dx%d:%dx%d", wind.size.first, wind.size.second, px, py);
|
||||
}
|
||||
|
||||
//Ini
|
||||
Ini::Ini()
|
||||
{
|
||||
#ifdef _WIN32
|
||||
m_Config = new wxIniConfig( wxEmptyString, wxEmptyString,
|
||||
wxGetCwd() + "/rpcs3.ini",
|
||||
wxEmptyString, wxCONFIG_USE_LOCAL_FILE );
|
||||
#else
|
||||
m_Config = new wxConfig("rpcs3");
|
||||
#endif
|
||||
m_Config = getIniFile();
|
||||
}
|
||||
|
||||
Ini::~Ini()
|
||||
{
|
||||
safe_delete(m_Config);
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
void Ini::Save(const wxString& key, int value)
|
||||
//TODO: saving the file after each change seems like overkill but that's how wx did it
|
||||
void Ini::Save(const std::string& section, const std::string& key, int value)
|
||||
{
|
||||
m_Config->Write(key, value);
|
||||
m_Config->SetLongValue(section.c_str(), key.c_str(), value);
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
void Ini::Save(const wxString& key, bool value)
|
||||
void Ini::Save(const std::string& section, const std::string& key, bool value)
|
||||
{
|
||||
m_Config->Write(key, BoolToString(value));
|
||||
m_Config->SetBoolValue(section.c_str(), key.c_str(), value);
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
void Ini::Save(const wxString& key, wxSize value)
|
||||
void Ini::Save(const std::string& section, const std::string& key, std::pair<int, int> value)
|
||||
{
|
||||
m_Config->Write(key, SizeToString(value));
|
||||
m_Config->SetValue(section.c_str(), key.c_str(), SizeToString(value).c_str());
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
void Ini::Save(const wxString& key, wxPoint value)
|
||||
void Ini::Save(const std::string& section, const std::string& key, const std::string& value)
|
||||
{
|
||||
m_Config->Write(key, PositionToString(value));
|
||||
m_Config->SetValue(section.c_str(), key.c_str(), value.c_str());
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
void Ini::Save(const wxString& key, const std::string& value)
|
||||
void Ini::Save(const std::string& section, const std::string& key, WindowInfo value)
|
||||
{
|
||||
m_Config->Write(key, fmt::FromUTF8(value));
|
||||
m_Config->SetValue(section.c_str(), key.c_str(), WindowInfoToString(value).c_str());
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
void Ini::Save(const wxString& key, WindowInfo value)
|
||||
int Ini::Load(const std::string& section, const std::string& key, const int def_value)
|
||||
{
|
||||
m_Config->Write(key, WindowInfoToString(value));
|
||||
return m_Config->GetLongValue(section.c_str(), key.c_str(), def_value);
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
int Ini::Load(const wxString& key, const int def_value)
|
||||
bool Ini::Load(const std::string& section, const std::string& key, const bool def_value)
|
||||
{
|
||||
return m_Config->Read(key, def_value);
|
||||
return StringToBool(m_Config->GetValue(section.c_str(), key.c_str(), BoolToString(def_value).c_str()));
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
bool Ini::Load(const wxString& key, const bool def_value)
|
||||
std::pair<int, int> Ini::Load(const std::string& section, const std::string& key, const std::pair<int, int> def_value)
|
||||
{
|
||||
return StringToBool(m_Config->Read(key, BoolToString(def_value)));
|
||||
return StringToSize(m_Config->GetValue(section.c_str(), key.c_str(), SizeToString(def_value).c_str()));
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
wxSize Ini::Load(const wxString& key, const wxSize def_value)
|
||||
std::string Ini::Load(const std::string& section, const std::string& key, const std::string& def_value)
|
||||
{
|
||||
return StringToSize(m_Config->Read(key, SizeToString(def_value)));
|
||||
return std::string(m_Config->GetValue(section.c_str(), key.c_str(), def_value.c_str()));
|
||||
saveIniFile();
|
||||
}
|
||||
|
||||
wxPoint Ini::Load(const wxString& key, const wxPoint def_value)
|
||||
WindowInfo Ini::Load(const std::string& section, const std::string& key, const WindowInfo& def_value)
|
||||
{
|
||||
return StringToPosition(m_Config->Read(key, PositionToString(def_value)));
|
||||
}
|
||||
|
||||
std::string Ini::Load(const wxString& key, const std::string& def_value)
|
||||
{
|
||||
return fmt::ToUTF8(m_Config->Read(key, def_value));
|
||||
}
|
||||
|
||||
WindowInfo Ini::Load(const wxString& key, const WindowInfo& def_value)
|
||||
{
|
||||
return StringToWindowInfo(m_Config->Read(key, WindowInfoToString(def_value)));
|
||||
return StringToWindowInfo(m_Config->GetValue(section.c_str(), key.c_str(), WindowInfoToString(def_value).c_str()));
|
||||
saveIniFile();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue