/ src / common / param_package.cpp
param_package.cpp
  1  // Copyright 2017 Citra Emulator Project
  2  // Licensed under GPLv2 or any later version
  3  // Refer to the license.txt file included.
  4  
  5  #include <array>
  6  #include <stdexcept>
  7  #include <utility>
  8  #include <vector>
  9  #include "common/logging/log.h"
 10  #include "common/param_package.h"
 11  #include "common/string_util.h"
 12  
 13  namespace Common {
 14  
 15  constexpr char KEY_VALUE_SEPARATOR = ':';
 16  constexpr char PARAM_SEPARATOR = ',';
 17  
 18  constexpr char ESCAPE_CHARACTER = '$';
 19  constexpr char KEY_VALUE_SEPARATOR_ESCAPE[] = "$0";
 20  constexpr char PARAM_SEPARATOR_ESCAPE[] = "$1";
 21  constexpr char ESCAPE_CHARACTER_ESCAPE[] = "$2";
 22  
 23  /// A placeholder for empty param packages to avoid empty strings
 24  /// (they may be recognized as "not set" by some frontend libraries like qt)
 25  constexpr char EMPTY_PLACEHOLDER[] = "[empty]";
 26  
 27  ParamPackage::ParamPackage(const std::string& serialized) {
 28      if (serialized == EMPTY_PLACEHOLDER) {
 29          return;
 30      }
 31  
 32      const auto pairs = Common::SplitString(serialized, PARAM_SEPARATOR);
 33      for (const std::string& pair : pairs) {
 34          auto key_value = Common::SplitString(pair, KEY_VALUE_SEPARATOR);
 35          if (key_value.size() != 2) {
 36              LOG_ERROR(Common, "invalid key pair {}", pair);
 37              continue;
 38          }
 39  
 40          for (std::string& part : key_value) {
 41              part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR});
 42              part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR});
 43              part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER});
 44          }
 45  
 46          Set(key_value[0], std::move(key_value[1]));
 47      }
 48  }
 49  
 50  ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {}
 51  
 52  std::string ParamPackage::Serialize() const {
 53      if (data.empty())
 54          return EMPTY_PLACEHOLDER;
 55  
 56      std::string result;
 57  
 58      for (const auto& pair : data) {
 59          std::array<std::string, 2> key_value{{pair.first, pair.second}};
 60          for (std::string& part : key_value) {
 61              part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE);
 62              part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE);
 63              part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE);
 64          }
 65          result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR;
 66      }
 67  
 68      result.pop_back(); // discard the trailing PARAM_SEPARATOR
 69      return result;
 70  }
 71  
 72  std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const {
 73      auto pair = data.find(key);
 74      if (pair == data.end()) {
 75          LOG_DEBUG(Common, "key {} not found", key);
 76          return default_value;
 77      }
 78  
 79      return pair->second;
 80  }
 81  
 82  int ParamPackage::Get(const std::string& key, int default_value) const {
 83      auto pair = data.find(key);
 84      if (pair == data.end()) {
 85          LOG_DEBUG(Common, "key {} not found", key);
 86          return default_value;
 87      }
 88  
 89      try {
 90          return std::stoi(pair->second);
 91      } catch (const std::logic_error&) {
 92          LOG_ERROR(Common, "failed to convert {} to int", pair->second);
 93          return default_value;
 94      }
 95  }
 96  
 97  float ParamPackage::Get(const std::string& key, float default_value) const {
 98      auto pair = data.find(key);
 99      if (pair == data.end()) {
100          LOG_DEBUG(Common, "key {} not found", key);
101          return default_value;
102      }
103  
104      try {
105          return std::stof(pair->second);
106      } catch (const std::logic_error&) {
107          LOG_ERROR(Common, "failed to convert {} to float", pair->second);
108          return default_value;
109      }
110  }
111  
112  void ParamPackage::Set(const std::string& key, std::string value) {
113      data.insert_or_assign(key, std::move(value));
114  }
115  
116  void ParamPackage::Set(const std::string& key, int value) {
117      data.insert_or_assign(key, std::to_string(value));
118  }
119  
120  void ParamPackage::Set(const std::string& key, float value) {
121      data.insert_or_assign(key, std::to_string(value));
122  }
123  
124  bool ParamPackage::Has(const std::string& key) const {
125      return data.find(key) != data.end();
126  }
127  
128  void ParamPackage::Erase(const std::string& key) {
129      data.erase(key);
130  }
131  
132  void ParamPackage::Clear() {
133      data.clear();
134  }
135  
136  ParamPackage::DataType::iterator ParamPackage::begin() {
137      return data.begin();
138  }
139  
140  ParamPackage::DataType::const_iterator ParamPackage::begin() const {
141      return data.begin();
142  }
143  
144  ParamPackage::DataType::iterator ParamPackage::end() {
145      return data.end();
146  }
147  
148  ParamPackage::DataType::const_iterator ParamPackage::end() const {
149      return data.end();
150  }
151  
152  } // namespace Common