mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-01-10 08:09:26 +01:00
Merge pull request #7057 from leoetlino/string
Config/StringUtil/IniFile: Get rid of some duplicated code
This commit is contained in:
commit
8074192a85
@ -11,44 +11,6 @@
|
||||
|
||||
namespace Config
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
std::string ValueToString(u16 value)
|
||||
{
|
||||
return StringFromFormat("0x%04x", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(u32 value)
|
||||
{
|
||||
return StringFromFormat("0x%08x", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(float value)
|
||||
{
|
||||
return StringFromFormat("%#.9g", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(double value)
|
||||
{
|
||||
return StringFromFormat("%#.17g", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(int value)
|
||||
{
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
std::string ValueToString(bool value)
|
||||
{
|
||||
return StringFromBool(value);
|
||||
}
|
||||
|
||||
std::string ValueToString(const std::string& value)
|
||||
{
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
ConfigLayerLoader::ConfigLayerLoader(LayerType layer) : m_layer(layer)
|
||||
{
|
||||
}
|
||||
|
@ -19,19 +19,6 @@ namespace Config
|
||||
{
|
||||
namespace detail
|
||||
{
|
||||
std::string ValueToString(u16 value);
|
||||
std::string ValueToString(u32 value);
|
||||
std::string ValueToString(float value);
|
||||
std::string ValueToString(double value);
|
||||
std::string ValueToString(int value);
|
||||
std::string ValueToString(bool value);
|
||||
std::string ValueToString(const std::string& value);
|
||||
template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
|
||||
std::string ValueToString(T value)
|
||||
{
|
||||
return ValueToString(static_cast<std::underlying_type_t<T>>(value));
|
||||
}
|
||||
|
||||
template <typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
|
||||
std::optional<T> TryParse(const std::string& str_value)
|
||||
{
|
||||
@ -133,13 +120,17 @@ public:
|
||||
template <typename T>
|
||||
void Set(const ConfigInfo<T>& config_info, const std::common_type_t<T>& value)
|
||||
{
|
||||
Set<T>(config_info.location, value);
|
||||
Set(config_info.location, value);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Set(const ConfigLocation& location, const T& value)
|
||||
{
|
||||
const std::string new_value = detail::ValueToString(value);
|
||||
Set(location, ValueToString(value));
|
||||
}
|
||||
|
||||
void Set(const ConfigLocation& location, const std::string& new_value)
|
||||
{
|
||||
std::optional<std::string>& current_value = m_map[location];
|
||||
if (current_value == new_value)
|
||||
return;
|
||||
|
@ -57,55 +57,11 @@ void IniFile::Section::Set(const std::string& key, const std::string& newValue)
|
||||
}
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, const std::string& newValue,
|
||||
const std::string& defaultValue)
|
||||
{
|
||||
if (newValue != defaultValue)
|
||||
Set(key, newValue);
|
||||
else
|
||||
Delete(key);
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, const std::vector<std::string>& newValues)
|
||||
{
|
||||
Set(key, JoinStrings(newValues, ","));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, u32 newValue)
|
||||
{
|
||||
Set(key, StringFromFormat("0x%08x", newValue));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, u64 new_value)
|
||||
{
|
||||
Set(key, StringFromFormat("0x%016" PRIx64, new_value));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, float newValue)
|
||||
{
|
||||
Set(key, StringFromFormat("%#.9g", newValue));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, double newValue)
|
||||
{
|
||||
Set(key, StringFromFormat("%#.17g", newValue));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, int newValue)
|
||||
{
|
||||
Set(key, std::to_string(newValue));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, s64 newValue)
|
||||
{
|
||||
Set(key, StringFromFormat("%" PRId64, newValue));
|
||||
}
|
||||
|
||||
void IniFile::Section::Set(const std::string& key, bool newValue)
|
||||
{
|
||||
Set(key, StringFromBool(newValue));
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, std::string* value,
|
||||
const std::string& defaultValue) const
|
||||
{
|
||||
@ -154,90 +110,6 @@ bool IniFile::Section::Get(const std::string& key, std::vector<std::string>* out
|
||||
return true;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, int* value, int defaultValue) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, s64* value, s64 default_value) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = default_value;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, u32* value, u32 defaultValue) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, u64* value, u64 default_value) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = default_value;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, bool* value, bool defaultValue) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, float* value, float defaultValue) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Get(const std::string& key, double* value, double defaultValue) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
|
||||
*value = defaultValue;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool IniFile::Section::Exists(const std::string& key) const
|
||||
{
|
||||
return values.find(key) != values.end();
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
#include "Common/CommonFuncs.h"
|
||||
#include "Common/CommonTypes.h"
|
||||
#include "Common/StringUtil.h"
|
||||
|
||||
struct CaseInsensitiveStringCompare
|
||||
{
|
||||
@ -35,20 +36,17 @@ public:
|
||||
bool Delete(const std::string& key);
|
||||
|
||||
void Set(const std::string& key, const std::string& newValue);
|
||||
void Set(const std::string& key, const std::string& newValue, const std::string& defaultValue);
|
||||
void Set(const std::string& key, u32 newValue);
|
||||
void Set(const std::string& key, u64 new_value);
|
||||
void Set(const std::string& key, float newValue);
|
||||
void Set(const std::string& key, double newValue);
|
||||
void Set(const std::string& key, int newValue);
|
||||
void Set(const std::string& key, s64 new_value);
|
||||
void Set(const std::string& key, bool newValue);
|
||||
template <typename T>
|
||||
void Set(const std::string& key, const T& new_value)
|
||||
{
|
||||
Set(key, ValueToString(new_value));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Set(const std::string& key, T newValue, const T defaultValue)
|
||||
void Set(const std::string& key, const T& new_value, const std::common_type_t<T>& default_value)
|
||||
{
|
||||
if (newValue != defaultValue)
|
||||
Set(key, newValue);
|
||||
if (new_value != default_value)
|
||||
Set(key, new_value);
|
||||
else
|
||||
Delete(key);
|
||||
}
|
||||
@ -57,13 +55,17 @@ public:
|
||||
|
||||
bool Get(const std::string& key, std::string* value,
|
||||
const std::string& defaultValue = NULL_STRING) const;
|
||||
bool Get(const std::string& key, int* value, int defaultValue = 0) const;
|
||||
bool Get(const std::string& key, s64* value, s64 default_value = 0) const;
|
||||
bool Get(const std::string& key, u32* value, u32 defaultValue = 0) const;
|
||||
bool Get(const std::string& key, u64* value, u64 default_value = 0) const;
|
||||
bool Get(const std::string& key, bool* value, bool defaultValue = false) const;
|
||||
bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const;
|
||||
bool Get(const std::string& key, double* value, double defaultValue = 0.0) const;
|
||||
template <typename T>
|
||||
bool Get(const std::string& key, T* value,
|
||||
const std::common_type_t<T>& default_value = {}) const
|
||||
{
|
||||
std::string temp;
|
||||
bool retval = Get(key, &temp);
|
||||
if (retval && TryParse(temp, value))
|
||||
return true;
|
||||
*value = default_value;
|
||||
return false;
|
||||
}
|
||||
bool Get(const std::string& key, std::vector<std::string>* values) const;
|
||||
|
||||
void SetLines(const std::vector<std::string>& lines);
|
||||
|
@ -3,6 +3,7 @@
|
||||
// Refer to the license.txt file included.
|
||||
|
||||
#include <algorithm>
|
||||
#include <cinttypes>
|
||||
#include <cstdarg>
|
||||
#include <cstddef>
|
||||
#include <cstdio>
|
||||
@ -291,7 +292,42 @@ bool TryParse(const std::string& str, bool* const output)
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string StringFromBool(bool value)
|
||||
std::string ValueToString(u16 value)
|
||||
{
|
||||
return StringFromFormat("0x%04x", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(u32 value)
|
||||
{
|
||||
return StringFromFormat("0x%08x", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(u64 value)
|
||||
{
|
||||
return StringFromFormat("0x%016" PRIx64, value);
|
||||
}
|
||||
|
||||
std::string ValueToString(float value)
|
||||
{
|
||||
return StringFromFormat("%#.9g", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(double value)
|
||||
{
|
||||
return StringFromFormat("%#.17g", value);
|
||||
}
|
||||
|
||||
std::string ValueToString(int value)
|
||||
{
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
std::string ValueToString(s64 value)
|
||||
{
|
||||
return StringFromFormat("%" PRId64, value);
|
||||
}
|
||||
|
||||
std::string ValueToString(bool value)
|
||||
{
|
||||
return value ? "True" : "False";
|
||||
}
|
||||
|
@ -9,6 +9,7 @@
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "Common/CommonTypes.h"
|
||||
@ -41,8 +42,6 @@ std::string ArrayToString(const u8* data, u32 size, int line_len = 20, bool spac
|
||||
std::string StripSpaces(const std::string& s);
|
||||
std::string StripQuotes(const std::string& s);
|
||||
|
||||
std::string StringFromBool(bool value);
|
||||
|
||||
bool TryParse(const std::string& str, bool* output);
|
||||
bool TryParse(const std::string& str, u16* output);
|
||||
bool TryParse(const std::string& str, u32* output);
|
||||
@ -83,6 +82,20 @@ bool TryParseVector(const std::string& str, std::vector<N>* output, const char d
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string ValueToString(u16 value);
|
||||
std::string ValueToString(u32 value);
|
||||
std::string ValueToString(u64 value);
|
||||
std::string ValueToString(float value);
|
||||
std::string ValueToString(double value);
|
||||
std::string ValueToString(int value);
|
||||
std::string ValueToString(s64 value);
|
||||
std::string ValueToString(bool value);
|
||||
template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
|
||||
std::string ValueToString(T value)
|
||||
{
|
||||
return ValueToString(static_cast<std::underlying_type_t<T>>(value));
|
||||
}
|
||||
|
||||
// Generates an hexdump-like representation of a binary data blob.
|
||||
std::string HexDump(const u8* data, size_t size);
|
||||
|
||||
|
@ -30,7 +30,7 @@ public:
|
||||
|
||||
if (audio_backend.size())
|
||||
m_values.emplace_back(
|
||||
std::make_tuple(Config::MAIN_DSP_HLE.location, StringFromBool(audio_backend == "HLE")));
|
||||
std::make_tuple(Config::MAIN_DSP_HLE.location, ValueToString(audio_backend == "HLE")));
|
||||
|
||||
// Arguments are in the format of <System>.<Section>.<Key>=Value
|
||||
for (const auto& arg : args)
|
||||
|
@ -84,3 +84,24 @@ TEST(StringUtil, UTF8ToSHIFTJIS)
|
||||
EXPECT_STREQ(SHIFTJISToUTF8(UTF8ToSHIFTJIS(kirby_unicode)).c_str(), kirby_unicode.c_str());
|
||||
EXPECT_STREQ(UTF8ToSHIFTJIS(kirby_unicode).c_str(), kirby_sjis.c_str());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void DoRoundTripTest(const std::vector<T>& data)
|
||||
{
|
||||
for (const T& e : data)
|
||||
{
|
||||
const std::string s = ValueToString(e);
|
||||
T out;
|
||||
EXPECT_TRUE(TryParse(s, &out));
|
||||
EXPECT_EQ(e, out);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(StringUtil, ToString_TryParse_Roundtrip)
|
||||
{
|
||||
DoRoundTripTest<bool>({true, false});
|
||||
DoRoundTripTest<int>({0, -1, 1, 123, -123, 123456789, -123456789});
|
||||
DoRoundTripTest<unsigned int>({0u, 1u, 123u, 123456789u, 4023456789u});
|
||||
DoRoundTripTest<float>({0.0f, 1.0f, -1.0f, -0.5f, 0.5f, -1e-3f, 1e-3f, 1e3f, -1e3f});
|
||||
DoRoundTripTest<double>({0.0, 1.0, -1.0, -0.5, 0.5, -1e-3, 1e-3, 1e3, -1e3});
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user