param_package.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-FileCopyrightText: 2017 Citra Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <array>
  4. #include <stdexcept>
  5. #include <utility>
  6. #include <vector>
  7. #include "common/logging/log.h"
  8. #include "common/param_package.h"
  9. #include "common/string_util.h"
  10. namespace Common {
  11. constexpr char KEY_VALUE_SEPARATOR = ':';
  12. constexpr char PARAM_SEPARATOR = ',';
  13. constexpr char ESCAPE_CHARACTER = '$';
  14. constexpr char KEY_VALUE_SEPARATOR_ESCAPE[] = "$0";
  15. constexpr char PARAM_SEPARATOR_ESCAPE[] = "$1";
  16. constexpr char ESCAPE_CHARACTER_ESCAPE[] = "$2";
  17. /// A placeholder for empty param packages to avoid empty strings
  18. /// (they may be recognized as "not set" by some frontend libraries like qt)
  19. constexpr char EMPTY_PLACEHOLDER[] = "[empty]";
  20. ParamPackage::ParamPackage(const std::string& serialized) {
  21. if (serialized == EMPTY_PLACEHOLDER) {
  22. return;
  23. }
  24. std::vector<std::string> pairs;
  25. Common::SplitString(serialized, PARAM_SEPARATOR, pairs);
  26. for (const std::string& pair : pairs) {
  27. std::vector<std::string> key_value;
  28. Common::SplitString(pair, KEY_VALUE_SEPARATOR, key_value);
  29. if (key_value.size() != 2) {
  30. LOG_ERROR(Common, "invalid key pair {}", pair);
  31. continue;
  32. }
  33. for (std::string& part : key_value) {
  34. part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR});
  35. part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR});
  36. part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER});
  37. }
  38. Set(key_value[0], std::move(key_value[1]));
  39. }
  40. }
  41. ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {}
  42. std::string ParamPackage::Serialize() const {
  43. if (data.empty())
  44. return EMPTY_PLACEHOLDER;
  45. std::string result;
  46. for (const auto& pair : data) {
  47. std::array<std::string, 2> key_value{{pair.first, pair.second}};
  48. for (std::string& part : key_value) {
  49. part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE);
  50. part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE);
  51. part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE);
  52. }
  53. result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR;
  54. }
  55. result.pop_back(); // discard the trailing PARAM_SEPARATOR
  56. return result;
  57. }
  58. std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const {
  59. auto pair = data.find(key);
  60. if (pair == data.end()) {
  61. LOG_TRACE(Common, "key '{}' not found", key);
  62. return default_value;
  63. }
  64. return pair->second;
  65. }
  66. int ParamPackage::Get(const std::string& key, int default_value) const {
  67. auto pair = data.find(key);
  68. if (pair == data.end()) {
  69. LOG_TRACE(Common, "key '{}' not found", key);
  70. return default_value;
  71. }
  72. try {
  73. return std::stoi(pair->second);
  74. } catch (const std::logic_error&) {
  75. LOG_ERROR(Common, "failed to convert {} to int", pair->second);
  76. return default_value;
  77. }
  78. }
  79. float ParamPackage::Get(const std::string& key, float default_value) const {
  80. auto pair = data.find(key);
  81. if (pair == data.end()) {
  82. LOG_TRACE(Common, "key {} not found", key);
  83. return default_value;
  84. }
  85. try {
  86. return std::stof(pair->second);
  87. } catch (const std::logic_error&) {
  88. LOG_ERROR(Common, "failed to convert {} to float", pair->second);
  89. return default_value;
  90. }
  91. }
  92. void ParamPackage::Set(const std::string& key, std::string value) {
  93. data.insert_or_assign(key, std::move(value));
  94. }
  95. void ParamPackage::Set(const std::string& key, int value) {
  96. data.insert_or_assign(key, std::to_string(value));
  97. }
  98. void ParamPackage::Set(const std::string& key, float value) {
  99. data.insert_or_assign(key, std::to_string(value));
  100. }
  101. bool ParamPackage::Has(const std::string& key) const {
  102. return data.find(key) != data.end();
  103. }
  104. void ParamPackage::Erase(const std::string& key) {
  105. data.erase(key);
  106. }
  107. void ParamPackage::Clear() {
  108. data.clear();
  109. }
  110. } // namespace Common