Fix some ini file misbehaviors

This commit is contained in:
Henrik Rydgård committed 2025-08-31 14:13:12 +02:00
1 parent 3056e977cc
commit 6e5c567809
5 files changed
+51 -40

No files matched your search

+13 -35
View File
@@ -300,23 +300,19 @@ void Section::Set(std::string_view key, bool newValue, bool defaultValue)
Delete(key);
}
void Section::Set(std::string_view key, const std::vector<std::string>& newValues)
{
void Section::Set(std::string_view key, const std::vector<std::string> &newValues) {
std::string temp;
// Join the strings with ,
for (const auto &value : newValues) {
temp += value + ",";
temp += value;
temp.push_back(',');
}
// remove last ,
if (!temp.empty())
temp.resize(temp.length() - 1);
temp.pop_back();
Set(key, temp.c_str());
}
void Section::AddComment(const std::string &comment) {
lines_.emplace_back(ParsedIniLine::CommentOnly("# " + comment));
}
bool Section::Get(std::string_view key, std::vector<std::string> *values, const std::vector<std::string> *defaultValues) const {
std::string temp;
bool retval = Get(key, &temp, 0);
@@ -326,23 +322,8 @@ bool Section::Get(std::string_view key, std::vector<std::string> *values, const
}
return false;
}
// ignore starting , if any
size_t subStart = temp.find_first_not_of(',');
size_t subEnd;
// split by ,
while (subStart != std::string::npos) {
// Find next ,
subEnd = temp.find_first_of(',', subStart);
if (subStart != subEnd) {
// take from first char until next ,
values->push_back(StripSpaces(temp.substr(subStart, subEnd - subStart)));
}
// Find the next non , char
subStart = temp.find_first_not_of(',', subEnd);
}
SplitString(temp, ',', *values, true);
return true;
}
@@ -408,6 +389,10 @@ bool Section::Exists(std::string_view key) const {
return false;
}
void Section::AddComment(const std::string &comment) {
lines_.emplace_back(ParsedIniLine::CommentOnly("# " + comment));
}
std::map<std::string, std::string> Section::ToMap() const {
std::map<std::string, std::string> outMap;
for (auto &line : lines_) {
@@ -502,8 +487,7 @@ void IniFile::SortSections()
std::sort(sections.begin(), sections.end());
}
bool IniFile::Load(const Path &path)
{
bool IniFile::Load(const Path &path) {
sections.clear();
sections.push_back(std::make_unique<Section>(""));
// first section consists of the comments before the first real section
@@ -531,15 +515,10 @@ bool IniFile::LoadFromVFS(VFSInterface &vfs, const std::string &filename) {
}
bool IniFile::Load(std::istream &in) {
// Maximum number of letters in a line
static const int MAX_BYTES = 1024*32;
char *templine = new char[MAX_BYTES]; // avoid using up massive stack space
while (!(in.eof() || in.fail()))
{
in.getline(templine, MAX_BYTES);
std::string_view line = templine;
std::string linebuf;
while (std::getline(in, linebuf)) {
std::string_view line = StripSpaces(std::string_view(linebuf));
// Remove UTF-8 byte order marks.
if (line.substr(0, 3) == "\xEF\xBB\xBF") {
line = line.substr(3);
@@ -575,7 +554,6 @@ bool IniFile::Load(std::istream &in) {
}
}
delete[] templine;
return true;
}
+24 -2
View File
@@ -16,7 +16,7 @@
// http://code.google.com/p/dolphin-emu/
#include <cstring>
#include <unordered_set>
#include "ppsspp_config.h"
#ifdef _WIN32
@@ -418,7 +418,7 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
}
}
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output) {
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output, bool trimOutput) {
size_t next = 0;
size_t pos = 0;
while (pos < str.length()) {
@@ -427,6 +427,9 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
break;
}
output.emplace_back(str.substr(next, delimPos - next));
if (trimOutput) {
output.back() = StripSpaces(output.back());
}
next = delimPos + 1;
pos = delimPos + 1;
}
@@ -435,6 +438,11 @@ void SplitString(std::string_view str, const char delim, std::vector<std::string
output.emplace_back(str);
} else if (next < str.length()) {
output.emplace_back(str.substr(next));
} else {
return;
}
if (trimOutput) {
output.back() = StripSpaces(output.back());
}
}
@@ -582,3 +590,17 @@ std::string ApplySafeSubstitutions(std::string_view format, int i1, int i2, int
}
return output;
}
void MakeUnique(std::vector<std::string> &v) {
std::unordered_set<std::string> seen;
std::vector<std::string> result;
result.reserve(v.size()); // minimize reallocations
for (const auto &s : v) {
if (seen.insert(s).second) {
// insert returns {iterator, bool}
// bool == true if it was newly inserted (didn't already exist)
result.push_back(s);
}
}
v.swap(result);
}
+3 -1
View File
@@ -111,7 +111,7 @@ int CountChar(std::string_view haystack, char needle);
// NOTE: str must live at least as long as all uses of output.
void SplitString(std::string_view str, const char delim, std::vector<std::string_view> &output);
// Try to avoid this when possible, in favor of the string_view version.
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output);
void SplitString(std::string_view str, const char delim, std::vector<std::string> &output, bool trimOutput = false);
void GetQuotedStrings(std::string_view str, std::vector<std::string> &output);
@@ -153,6 +153,8 @@ inline void CharArrayFromFormat(char (& out)[Count], const char* format, ...)
// "C:/Windows/winhelp.exe" to "C:/Windows/", "winhelp", ".exe"
bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _pFilename, std::string* _pExtension);
void MakeUnique(std::vector<std::string> &vec);
// Replaces %1, %2, %3 in format with arg1, arg2, arg3.
// Much safer than snprintf and friends.
// For mixes of strings and ints, manually convert the ints to strings.
+3 -1
View File
@@ -548,7 +548,9 @@ int Config::NextValidBackend() {
#endif
// They've all failed. Let them try the default - or on Android, OpenGL.
sFailedGPUBackends += ",ALL";
if (sFailedGPUBackends.find(",ALL") == std::string::npos) {
sFailedGPUBackends += ",ALL";
}
ERROR_LOG(Log::Loader, "All graphics backends failed");
#if PPSSPP_PLATFORM(ANDROID)
return (int)GPUBackend::OPENGL;
+8 -1
View File
@@ -1,6 +1,7 @@
#include "Common/Data/Format/IniFile.h"
#include "Common/Net/URL.h"
#include "Common/Log.h"
#include "Common/StringUtils.h"
#include "Core/ConfigSettings.h"
#include "Core/ConfigValues.h"
@@ -38,8 +39,14 @@ bool ConfigSetting::Get(const Section *section) const {
case TYPE_STRING:
return section->Get(iniKey_, ptr_.s, cb_.s ? cb_.s().c_str() : default_.s);
case TYPE_STRING_VECTOR:
{
// No support for callbacks for these yet. that's not an issue.
return section->Get(iniKey_, ptr_.v, default_.v);
bool success = section->Get(iniKey_, ptr_.v, default_.v);
if (success) {
MakeUnique(*ptr_.v);
}
return success;
}
case TYPE_TOUCH_POS:
{
ConfigTouchPos defaultTouchPos = cb_.touchPos ? cb_.touchPos() : default_.touchPos;