param_package.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2017 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <array>
  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. ParamPackage::ParamPackage(const std::string& serialized) {
  18. std::vector<std::string> pairs;
  19. Common::SplitString(serialized, PARAM_SEPARATOR, pairs);
  20. for (const std::string& pair : pairs) {
  21. std::vector<std::string> key_value;
  22. Common::SplitString(pair, KEY_VALUE_SEPARATOR, key_value);
  23. if (key_value.size() != 2) {
  24. LOG_ERROR(Common, "invalid key pair {}", pair);
  25. continue;
  26. }
  27. for (std::string& part : key_value) {
  28. part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR});
  29. part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR});
  30. part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER});
  31. }
  32. Set(key_value[0], std::move(key_value[1]));
  33. }
  34. }
  35. ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {}
  36. std::string ParamPackage::Serialize() const {
  37. if (data.empty())
  38. return "";
  39. std::string result;
  40. for (const auto& pair : data) {
  41. std::array<std::string, 2> key_value{{pair.first, pair.second}};
  42. for (std::string& part : key_value) {
  43. part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE);
  44. part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE);
  45. part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE);
  46. }
  47. result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR;
  48. }
  49. result.pop_back(); // discard the trailing PARAM_SEPARATOR
  50. return result;
  51. }
  52. std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const {
  53. auto pair = data.find(key);
  54. if (pair == data.end()) {
  55. LOG_DEBUG(Common, "key '{}' not found", key);
  56. return default_value;
  57. }
  58. return pair->second;
  59. }
  60. int ParamPackage::Get(const std::string& key, int default_value) const {
  61. auto pair = data.find(key);
  62. if (pair == data.end()) {
  63. LOG_DEBUG(Common, "key '{}' not found", key);
  64. return default_value;
  65. }
  66. try {
  67. return std::stoi(pair->second);
  68. } catch (const std::logic_error&) {
  69. LOG_ERROR(Common, "failed to convert {} to int", pair->second);
  70. return default_value;
  71. }
  72. }
  73. float ParamPackage::Get(const std::string& key, float default_value) const {
  74. auto pair = data.find(key);
  75. if (pair == data.end()) {
  76. LOG_DEBUG(Common, "key {} not found", key);
  77. return default_value;
  78. }
  79. try {
  80. return std::stof(pair->second);
  81. } catch (const std::logic_error&) {
  82. LOG_ERROR(Common, "failed to convert {} to float", pair->second);
  83. return default_value;
  84. }
  85. }
  86. void ParamPackage::Set(const std::string& key, std::string value) {
  87. data.insert_or_assign(key, std::move(value));
  88. }
  89. void ParamPackage::Set(const std::string& key, int value) {
  90. data.insert_or_assign(key, std::to_string(value));
  91. }
  92. void ParamPackage::Set(const std::string& key, float value) {
  93. data.insert_or_assign(key, std::to_string(value));
  94. }
  95. bool ParamPackage::Has(const std::string& key) const {
  96. return data.find(key) != data.end();
  97. }
  98. } // namespace Common