summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/logging/backend.cpp1
-rw-r--r--src/common/logging/log.h1
-rw-r--r--src/common/param_package.cpp120
-rw-r--r--src/common/param_package.h40
5 files changed, 164 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 8a6170257..13277a5c2 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -35,6 +35,7 @@ set(SRCS
35 memory_util.cpp 35 memory_util.cpp
36 microprofile.cpp 36 microprofile.cpp
37 misc.cpp 37 misc.cpp
38 param_package.cpp
38 scm_rev.cpp 39 scm_rev.cpp
39 string_util.cpp 40 string_util.cpp
40 symbols.cpp 41 symbols.cpp
@@ -66,6 +67,7 @@ set(HEADERS
66 memory_util.h 67 memory_util.h
67 microprofile.h 68 microprofile.h
68 microprofileui.h 69 microprofileui.h
70 param_package.h
69 platform.h 71 platform.h
70 quaternion.h 72 quaternion.h
71 scm_rev.h 73 scm_rev.h
diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp
index 737e1d57f..42f6a9918 100644
--- a/src/common/logging/backend.cpp
+++ b/src/common/logging/backend.cpp
@@ -71,6 +71,7 @@ namespace Log {
71 CLS(Audio) \ 71 CLS(Audio) \
72 SUB(Audio, DSP) \ 72 SUB(Audio, DSP) \
73 SUB(Audio, Sink) \ 73 SUB(Audio, Sink) \
74 CLS(Input) \
74 CLS(Loader) 75 CLS(Loader)
75 76
76// GetClassName is a macro defined by Windows.h, grrr... 77// GetClassName is a macro defined by Windows.h, grrr...
diff --git a/src/common/logging/log.h b/src/common/logging/log.h
index 4b0f8ff03..1b905f66c 100644
--- a/src/common/logging/log.h
+++ b/src/common/logging/log.h
@@ -89,6 +89,7 @@ enum class Class : ClassType {
89 Audio_DSP, ///< The HLE implementation of the DSP 89 Audio_DSP, ///< The HLE implementation of the DSP
90 Audio_Sink, ///< Emulator audio output backend 90 Audio_Sink, ///< Emulator audio output backend
91 Loader, ///< ROM loader 91 Loader, ///< ROM loader
92 Input, ///< Input emulation
92 Count ///< Total number of logging classes 93 Count ///< Total number of logging classes
93}; 94};
94 95
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
diff --git a/src/common/param_package.h b/src/common/param_package.h
new file mode 100644
index 000000000..c4c11b221
--- /dev/null
+++ b/src/common/param_package.h
@@ -0,0 +1,40 @@
1// Copyright 2017 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#pragma once
6
7#include <initializer_list>
8#include <string>
9#include <unordered_map>
10
11namespace Common {
12
13/// A string-based key-value container supporting serializing to and deserializing from a string
14class ParamPackage {
15public:
16 using DataType = std::unordered_map<std::string, std::string>;
17
18 ParamPackage() = default;
19 explicit ParamPackage(const std::string& serialized);
20 ParamPackage(std::initializer_list<DataType::value_type> list);
21 ParamPackage(const ParamPackage& other) = default;
22 ParamPackage(ParamPackage&& other) = default;
23
24 ParamPackage& operator=(const ParamPackage& other) = default;
25 ParamPackage& operator=(ParamPackage&& other) = default;
26
27 std::string Serialize() const;
28 std::string Get(const std::string& key, const std::string& default_value) const;
29 int Get(const std::string& key, int default_value) const;
30 float Get(const std::string& key, float default_value) const;
31 void Set(const std::string& key, const std::string& value);
32 void Set(const std::string& key, int value);
33 void Set(const std::string& key, float value);
34 bool Has(const std::string& key) const;
35
36private:
37 DataType data;
38};
39
40} // namespace Common