summaryrefslogtreecommitdiff
path: root/src/common/param_package.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/param_package.cpp')
-rw-r--r--src/common/param_package.cpp120
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
11namespace Common {
12
13constexpr char KEY_VALUE_SEPARATOR = ':';
14constexpr char PARAM_SEPARATOR = ',';
15constexpr char ESCAPE_CHARACTER = '$';
16const std::string KEY_VALUE_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '0'};
17const std::string PARAM_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '1'};
18const std::string ESCAPE_CHARACTER_ESCAPE{ESCAPE_CHARACTER, '2'};
19
20ParamPackage::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
42ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : data(list) {}
43
44std::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
64std::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
74int 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
89float 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
104void ParamPackage::Set(const std::string& key, const std::string& value) {
105 data[key] = value;
106}
107
108void ParamPackage::Set(const std::string& key, int value) {
109 data[key] = std::to_string(value);
110}
111
112void ParamPackage::Set(const std::string& key, float value) {
113 data[key] = std::to_string(value);
114}
115
116bool ParamPackage::Has(const std::string& key) const {
117 return data.find(key) != data.end();
118}
119
120} // namespace Common