diff options
| author | 2017-03-17 14:59:39 -0400 | |
|---|---|---|
| committer | 2017-03-17 14:59:39 -0400 | |
| commit | 423ab5e2bcf5a522e5f412447c05f648df57a14c (patch) | |
| tree | 1e60eaeffa59229254a47f885d2fe2cbbdc1a5c0 /src/common/param_package.cpp | |
| parent | Merge pull request #2618 from wwylele/log-less-filename (diff) | |
| parent | qt/config_input: don't connect for null button (diff) | |
| download | yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.gz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.tar.xz yuzu-423ab5e2bcf5a522e5f412447c05f648df57a14c.zip | |
Merge pull request #2497 from wwylele/input-2
Refactor input emulation & add SDL gamepad support
Diffstat (limited to 'src/common/param_package.cpp')
| -rw-r--r-- | src/common/param_package.cpp | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/src/common/param_package.cpp b/src/common/param_package.cpp new file mode 100644 index 000000000..3a6ef8c27 --- /dev/null +++ b/src/common/param_package.cpp | |||
| @@ -0,0 +1,120 @@ | |||
| 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 <vector> | ||
| 7 | #include "common/logging/log.h" | ||
| 8 | #include "common/param_package.h" | ||
| 9 | #include "common/string_util.h" | ||
| 10 | |||
| 11 | namespace Common { | ||
| 12 | |||
| 13 | constexpr char KEY_VALUE_SEPARATOR = ':'; | ||
| 14 | constexpr char PARAM_SEPARATOR = ','; | ||
| 15 | constexpr char ESCAPE_CHARACTER = '$'; | ||
| 16 | const std::string KEY_VALUE_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '0'}; | ||
| 17 | const std::string PARAM_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '1'}; | ||
| 18 | const std::string ESCAPE_CHARACTER_ESCAPE{ESCAPE_CHARACTER, '2'}; | ||
| 19 | |||
| 20 | ParamPackage::ParamPackage(const std::string& serialized) { | ||
| 21 | std::vector<std::string> pairs; | ||
| 22 | Common::SplitString(serialized, PARAM_SEPARATOR, pairs); | ||
| 23 | |||
| 24 | for (const std::string& pair : pairs) { | ||
| 25 | std::vector<std::string> key_value; | ||
| 26 | Common::SplitString(pair, KEY_VALUE_SEPARATOR, key_value); | ||
| 27 | if (key_value.size() != 2) { | ||
| 28 | LOG_ERROR(Common, "invalid key pair %s", pair.c_str()); | ||
| 29 | continue; | ||
| 30 | } | ||
| 31 | |||
| 32 | for (std::string& part : key_value) { | ||
| 33 | part = Common::ReplaceAll(part, KEY_VALUE_SEPARATOR_ESCAPE, {KEY_VALUE_SEPARATOR}); | ||
| 34 | part = Common::ReplaceAll(part, PARAM_SEPARATOR_ESCAPE, {PARAM_SEPARATOR}); | ||
| 35 | part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER}); | ||
| 36 | } | ||
| 37 | |||
| 38 | Set(key_value[0], key_value[1]); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {} | ||
| 43 | |||
| 44 | std::string ParamPackage::Serialize() const { | ||
| 45 | if (data.empty()) | ||
| 46 | return ""; | ||
| 47 | |||
| 48 | std::string result; | ||
| 49 | |||
| 50 | for (const auto& pair : data) { | ||
| 51 | std::array<std::string, 2> key_value{{pair.first, pair.second}}; | ||
| 52 | for (std::string& part : key_value) { | ||
| 53 | part = Common::ReplaceAll(part, {ESCAPE_CHARACTER}, ESCAPE_CHARACTER_ESCAPE); | ||
| 54 | part = Common::ReplaceAll(part, {PARAM_SEPARATOR}, PARAM_SEPARATOR_ESCAPE); | ||
| 55 | part = Common::ReplaceAll(part, {KEY_VALUE_SEPARATOR}, KEY_VALUE_SEPARATOR_ESCAPE); | ||
| 56 | } | ||
| 57 | result += key_value[0] + KEY_VALUE_SEPARATOR + key_value[1] + PARAM_SEPARATOR; | ||
| 58 | } | ||
| 59 | |||
| 60 | result.pop_back(); // discard the trailing PARAM_SEPARATOR | ||
| 61 | return result; | ||
| 62 | } | ||
| 63 | |||
| 64 | std::string ParamPackage::Get(const std::string& key, const std::string& default_value) const { | ||
| 65 | auto pair = data.find(key); | ||
| 66 | if (pair == data.end()) { | ||
| 67 | LOG_DEBUG(Common, "key %s not found", key.c_str()); | ||
| 68 | return default_value; | ||
| 69 | } | ||
| 70 | |||
| 71 | return pair->second; | ||
| 72 | } | ||
| 73 | |||
| 74 | int ParamPackage::Get(const std::string& key, int default_value) const { | ||
| 75 | auto pair = data.find(key); | ||
| 76 | if (pair == data.end()) { | ||
| 77 | LOG_DEBUG(Common, "key %s not found", key.c_str()); | ||
| 78 | return default_value; | ||
| 79 | } | ||
| 80 | |||
| 81 | try { | ||
| 82 | return std::stoi(pair->second); | ||
| 83 | } catch (const std::logic_error&) { | ||
| 84 | LOG_ERROR(Common, "failed to convert %s to int", pair->second.c_str()); | ||
| 85 | return default_value; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | float ParamPackage::Get(const std::string& key, float default_value) const { | ||
| 90 | auto pair = data.find(key); | ||
| 91 | if (pair == data.end()) { | ||
| 92 | LOG_DEBUG(Common, "key %s not found", key.c_str()); | ||
| 93 | return default_value; | ||
| 94 | } | ||
| 95 | |||
| 96 | try { | ||
| 97 | return std::stof(pair->second); | ||
| 98 | } catch (const std::logic_error&) { | ||
| 99 | LOG_ERROR(Common, "failed to convert %s to float", pair->second.c_str()); | ||
| 100 | return default_value; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | void ParamPackage::Set(const std::string& key, const std::string& value) { | ||
| 105 | data[key] = value; | ||
| 106 | } | ||
| 107 | |||
| 108 | void ParamPackage::Set(const std::string& key, int value) { | ||
| 109 | data[key] = std::to_string(value); | ||
| 110 | } | ||
| 111 | |||
| 112 | void ParamPackage::Set(const std::string& key, float value) { | ||
| 113 | data[key] = std::to_string(value); | ||
| 114 | } | ||
| 115 | |||
| 116 | bool ParamPackage::Has(const std::string& key) const { | ||
| 117 | return data.find(key) != data.end(); | ||
| 118 | } | ||
| 119 | |||
| 120 | } // namespace Common | ||