2017-01-20 19:30:11 +00:00
|
|
|
// Copyright 2017 Citra Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <array>
|
2018-07-20 22:17:26 +01:00
|
|
|
#include <utility>
|
2017-01-20 19:30:11 +00:00
|
|
|
#include <vector>
|
|
|
|
#include "common/logging/log.h"
|
|
|
|
#include "common/param_package.h"
|
|
|
|
#include "common/string_util.h"
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
|
|
|
constexpr char KEY_VALUE_SEPARATOR = ':';
|
|
|
|
constexpr char PARAM_SEPARATOR = ',';
|
2018-07-20 22:05:46 +01:00
|
|
|
|
2017-01-20 19:30:11 +00:00
|
|
|
constexpr char ESCAPE_CHARACTER = '$';
|
2018-07-20 22:05:46 +01:00
|
|
|
constexpr char KEY_VALUE_SEPARATOR_ESCAPE[] = "$0";
|
|
|
|
constexpr char PARAM_SEPARATOR_ESCAPE[] = "$1";
|
|
|
|
constexpr char ESCAPE_CHARACTER_ESCAPE[] = "$2";
|
2017-01-20 19:30:11 +00:00
|
|
|
|
2018-08-26 09:23:12 +01:00
|
|
|
/// A placeholder for empty param packages to avoid empty strings
|
|
|
|
/// (they may be recognized as "not set" by some frontend libraries like qt)
|
|
|
|
constexpr char EMPTY_PLACEHOLDER[] = "[empty]";
|
|
|
|
|
2017-01-20 19:30:11 +00:00
|
|
|
ParamPackage::ParamPackage(const std::string& serialized) {
|
2018-08-26 09:23:12 +01:00
|
|
|
if (serialized == EMPTY_PLACEHOLDER) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-27 05:38:28 +01:00
|
|
|
const auto pairs = Common::SplitString(serialized, PARAM_SEPARATOR);
|
2017-01-20 19:30:11 +00:00
|
|
|
for (const std::string& pair : pairs) {
|
2023-04-27 05:38:28 +01:00
|
|
|
auto key_value = Common::SplitString(pair, KEY_VALUE_SEPARATOR);
|
2017-01-20 19:30:11 +00:00
|
|
|
if (key_value.size() != 2) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_ERROR(Common, "invalid key pair {}", pair);
|
2017-01-20 19:30:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::string& part : key_value) {
|
|
|
|
part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR});
|
|
|
|
part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR});
|
|
|
|
part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER});
|
|
|
|
}
|
|
|
|
|
2018-07-20 22:17:26 +01:00
|
|
|
Set(key_value[0], std::move(key_value[1]));
|
2017-01-20 19:30:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {}
|
|
|
|
|
|
|
|
std::string ParamPackage::Serialize() const {
|
|
|
|
if (data.empty())
|
2018-08-26 09:23:12 +01:00
|
|
|
return EMPTY_PLACEHOLDER;
|
2017-01-20 19:30:11 +00:00
|
|
|
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
for (const auto& pair : data) {
|
|
|
|
std::array<std::string, 2> key_value{{pair.first, pair.second}};
|
|
|
|
for (std::string& part : key_value) {
|
|
|
|
part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE);
|
|
|
|
part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE);
|
|
|
|
part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE);
|
|
|
|
}
|
|
|
|
result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR;
|
|
|
|
}
|
|
|
|
|
|
|
|
result.pop_back(); // discard the trailing PARAM_SEPARATOR
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const {
|
|
|
|
auto pair = data.find(key);
|
|
|
|
if (pair == data.end()) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Common, "key {} not found", key);
|
2017-01-20 19:30:11 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return pair->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ParamPackage::Get(const std::string& key, int default_value) const {
|
|
|
|
auto pair = data.find(key);
|
|
|
|
if (pair == data.end()) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Common, "key {} not found", key);
|
2017-01-20 19:30:11 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return std::stoi(pair->second);
|
|
|
|
} catch (const std::logic_error&) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_ERROR(Common, "failed to convert {} to int", pair->second);
|
2017-01-20 19:30:11 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float ParamPackage::Get(const std::string& key, float default_value) const {
|
|
|
|
auto pair = data.find(key);
|
|
|
|
if (pair == data.end()) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_DEBUG(Common, "key {} not found", key);
|
2017-01-20 19:30:11 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return std::stof(pair->second);
|
|
|
|
} catch (const std::logic_error&) {
|
2018-06-29 12:18:07 +01:00
|
|
|
LOG_ERROR(Common, "failed to convert {} to float", pair->second);
|
2017-01-20 19:30:11 +00:00
|
|
|
return default_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-20 22:17:26 +01:00
|
|
|
void ParamPackage::Set(const std::string& key, std::string value) {
|
|
|
|
data.insert_or_assign(key, std::move(value));
|
2017-01-20 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParamPackage::Set(const std::string& key, int value) {
|
2018-07-20 22:12:24 +01:00
|
|
|
data.insert_or_assign(key, std::to_string(value));
|
2017-01-20 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParamPackage::Set(const std::string& key, float value) {
|
2018-07-20 22:12:24 +01:00
|
|
|
data.insert_or_assign(key, std::to_string(value));
|
2017-01-20 19:30:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParamPackage::Has(const std::string& key) const {
|
|
|
|
return data.find(key) != data.end();
|
|
|
|
}
|
|
|
|
|
2018-08-26 09:23:12 +01:00
|
|
|
void ParamPackage::Erase(const std::string& key) {
|
|
|
|
data.erase(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParamPackage::Clear() {
|
|
|
|
data.clear();
|
|
|
|
}
|
|
|
|
|
2020-01-29 06:54:39 +00:00
|
|
|
ParamPackage::DataType::iterator ParamPackage::begin() {
|
|
|
|
return data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamPackage::DataType::const_iterator ParamPackage::begin() const {
|
|
|
|
return data.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamPackage::DataType::iterator ParamPackage::end() {
|
|
|
|
return data.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
ParamPackage::DataType::const_iterator ParamPackage::end() const {
|
|
|
|
return data.end();
|
|
|
|
}
|
|
|
|
|
2017-01-20 19:30:11 +00:00
|
|
|
} // namespace Common
|