summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt5
-rw-r--r--src/common/detached_tasks.cpp41
-rw-r--r--src/common/detached_tasks.h40
-rw-r--r--src/common/file_util.h2
-rw-r--r--src/common/logging/text_formatter.cpp2
-rw-r--r--src/common/param_package.cpp18
-rw-r--r--src/common/param_package.h2
-rw-r--r--src/common/string_util.cpp134
-rw-r--r--src/common/string_util.h21
-rw-r--r--src/common/web_result.h24
10 files changed, 136 insertions, 153 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index 6a3f1fe08..d0e506689 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -29,7 +29,7 @@ if ($ENV{CI})
29 if (BUILD_VERSION) 29 if (BUILD_VERSION)
30 # This leaves a trailing space on the last word, but we actually want that 30 # This leaves a trailing space on the last word, but we actually want that
31 # because of how it's styled in the title bar. 31 # because of how it's styled in the title bar.
32 set(BUILD_FULLNAME "${REPO_NAME} #${BUILD_VERSION} ") 32 set(BUILD_FULLNAME "${REPO_NAME} ${BUILD_VERSION} ")
33 else() 33 else()
34 set(BUILD_FULLNAME "") 34 set(BUILD_FULLNAME "")
35 endif() 35 endif()
@@ -41,6 +41,8 @@ configure_file("${CMAKE_CURRENT_SOURCE_DIR}/scm_rev.cpp.in" "${CMAKE_CURRENT_SOU
41add_library(common STATIC 41add_library(common STATIC
42 alignment.h 42 alignment.h
43 assert.h 43 assert.h
44 detached_tasks.cpp
45 detached_tasks.h
44 bit_field.h 46 bit_field.h
45 bit_set.h 47 bit_set.h
46 cityhash.cpp 48 cityhash.cpp
@@ -87,6 +89,7 @@ add_library(common STATIC
87 timer.cpp 89 timer.cpp
88 timer.h 90 timer.h
89 vector_math.h 91 vector_math.h
92 web_result.h
90) 93)
91 94
92if(ARCHITECTURE_x86_64) 95if(ARCHITECTURE_x86_64)
diff --git a/src/common/detached_tasks.cpp b/src/common/detached_tasks.cpp
new file mode 100644
index 000000000..a347d9e02
--- /dev/null
+++ b/src/common/detached_tasks.cpp
@@ -0,0 +1,41 @@
1// Copyright 2018 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <thread>
6#include "common/assert.h"
7#include "common/detached_tasks.h"
8
9namespace Common {
10
11DetachedTasks* DetachedTasks::instance = nullptr;
12
13DetachedTasks::DetachedTasks() {
14 ASSERT(instance == nullptr);
15 instance = this;
16}
17
18void DetachedTasks::WaitForAllTasks() {
19 std::unique_lock<std::mutex> lock(mutex);
20 cv.wait(lock, [this]() { return count == 0; });
21}
22
23DetachedTasks::~DetachedTasks() {
24 std::unique_lock<std::mutex> lock(mutex);
25 ASSERT(count == 0);
26 instance = nullptr;
27}
28
29void DetachedTasks::AddTask(std::function<void()> task) {
30 std::unique_lock<std::mutex> lock(instance->mutex);
31 ++instance->count;
32 std::thread([task{std::move(task)}]() {
33 task();
34 std::unique_lock<std::mutex> lock(instance->mutex);
35 --instance->count;
36 std::notify_all_at_thread_exit(instance->cv, std::move(lock));
37 })
38 .detach();
39}
40
41} // namespace Common
diff --git a/src/common/detached_tasks.h b/src/common/detached_tasks.h
new file mode 100644
index 000000000..5dd8fc27b
--- /dev/null
+++ b/src/common/detached_tasks.h
@@ -0,0 +1,40 @@
1// Copyright 2018 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 <condition_variable>
8#include <functional>
9
10namespace Common {
11
12/**
13 * A background manager which ensures that all detached task is finished before program exits.
14 *
15 * Some tasks, telemetry submission for example, prefer executing asynchronously and don't care
16 * about the result. These tasks are suitable for std::thread::detach(). However, this is unsafe if
17 * the task is launched just before the program exits (which is a common case for telemetry), so we
18 * need to block on these tasks on program exit.
19 *
20 * To make detached task safe, a single DetachedTasks object should be placed in the main(), and
21 * call WaitForAllTasks() after all program execution but before global/static variable destruction.
22 * Any potentially unsafe detached task should be executed via DetachedTasks::AddTask.
23 */
24class DetachedTasks {
25public:
26 DetachedTasks();
27 ~DetachedTasks();
28 void WaitForAllTasks();
29
30 static void AddTask(std::function<void()> task);
31
32private:
33 static DetachedTasks* instance;
34
35 std::condition_variable cv;
36 std::mutex mutex;
37 int count = 0;
38};
39
40} // namespace Common
diff --git a/src/common/file_util.h b/src/common/file_util.h
index 3d8fe6264..571503d2a 100644
--- a/src/common/file_util.h
+++ b/src/common/file_util.h
@@ -285,7 +285,7 @@ private:
285template <typename T> 285template <typename T>
286void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) { 286void OpenFStream(T& fstream, const std::string& filename, std::ios_base::openmode openmode) {
287#ifdef _MSC_VER 287#ifdef _MSC_VER
288 fstream.open(Common::UTF8ToTStr(filename).c_str(), openmode); 288 fstream.open(Common::UTF8ToUTF16W(filename).c_str(), openmode);
289#else 289#else
290 fstream.open(filename.c_str(), openmode); 290 fstream.open(filename.c_str(), openmode);
291#endif 291#endif
diff --git a/src/common/logging/text_formatter.cpp b/src/common/logging/text_formatter.cpp
index 05437c137..6a0605c63 100644
--- a/src/common/logging/text_formatter.cpp
+++ b/src/common/logging/text_formatter.cpp
@@ -31,7 +31,7 @@ std::string FormatLogMessage(const Entry& entry) {
31} 31}
32 32
33void PrintMessage(const Entry& entry) { 33void PrintMessage(const Entry& entry) {
34 auto str = FormatLogMessage(entry) + '\n'; 34 const auto str = FormatLogMessage(entry).append(1, '\n');
35 fputs(str.c_str(), stderr); 35 fputs(str.c_str(), stderr);
36} 36}
37 37
diff --git a/src/common/param_package.cpp b/src/common/param_package.cpp
index 9526ca0c6..b916b4866 100644
--- a/src/common/param_package.cpp
+++ b/src/common/param_package.cpp
@@ -20,7 +20,15 @@ constexpr char KEY_VALUE_SEPARATOR_ESCAPE[] = "$0";
20constexpr char PARAM_SEPARATOR_ESCAPE[] = "$1"; 20constexpr char PARAM_SEPARATOR_ESCAPE[] = "$1";
21constexpr char ESCAPE_CHARACTER_ESCAPE[] = "$2"; 21constexpr char ESCAPE_CHARACTER_ESCAPE[] = "$2";
22 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)
25constexpr char EMPTY_PLACEHOLDER[] = "[empty]";
26
23ParamPackage::ParamPackage(const std::string& serialized) { 27ParamPackage::ParamPackage(const std::string& serialized) {
28 if (serialized == EMPTY_PLACEHOLDER) {
29 return;
30 }
31
24 std::vector<std::string> pairs; 32 std::vector<std::string> pairs;
25 Common::SplitString(serialized, PARAM_SEPARATOR, pairs); 33 Common::SplitString(serialized, PARAM_SEPARATOR, pairs);
26 34
@@ -46,7 +54,7 @@ ParamPackage::ParamPackage(std::initializer_list<DataType::value_type> list) : d
46 54
47std::string ParamPackage::Serialize() const { 55std::string ParamPackage::Serialize() const {
48 if (data.empty()) 56 if (data.empty())
49 return ""; 57 return EMPTY_PLACEHOLDER;
50 58
51 std::string result; 59 std::string result;
52 60
@@ -120,4 +128,12 @@ bool ParamPackage::Has(const std::string& key) const {
120 return data.find(key) != data.end(); 128 return data.find(key) != data.end();
121} 129}
122 130
131void ParamPackage::Erase(const std::string& key) {
132 data.erase(key);
133}
134
135void ParamPackage::Clear() {
136 data.clear();
137}
138
123} // namespace Common 139} // namespace Common
diff --git a/src/common/param_package.h b/src/common/param_package.h
index 7842cd4ef..6a0a9b656 100644
--- a/src/common/param_package.h
+++ b/src/common/param_package.h
@@ -32,6 +32,8 @@ public:
32 void Set(const std::string& key, int value); 32 void Set(const std::string& key, int value);
33 void Set(const std::string& key, float value); 33 void Set(const std::string& key, float value);
34 bool Has(const std::string& key) const; 34 bool Has(const std::string& key) const;
35 void Erase(const std::string& key);
36 void Clear();
35 37
36private: 38private:
37 DataType data; 39 DataType data;
diff --git a/src/common/string_util.cpp b/src/common/string_util.cpp
index c9a5425a7..731d1db34 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -5,6 +5,7 @@
5#include <algorithm> 5#include <algorithm>
6#include <cctype> 6#include <cctype>
7#include <cerrno> 7#include <cerrno>
8#include <codecvt>
8#include <cstdio> 9#include <cstdio>
9#include <cstdlib> 10#include <cstdlib>
10#include <cstring> 11#include <cstring>
@@ -13,11 +14,7 @@
13#include "common/string_util.h" 14#include "common/string_util.h"
14 15
15#ifdef _WIN32 16#ifdef _WIN32
16#include <codecvt>
17#include <windows.h> 17#include <windows.h>
18#include "common/common_funcs.h"
19#else
20#include <iconv.h>
21#endif 18#endif
22 19
23namespace Common { 20namespace Common {
@@ -195,11 +192,9 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
195 return result; 192 return result;
196} 193}
197 194
198#ifdef _WIN32
199
200std::string UTF16ToUTF8(const std::u16string& input) { 195std::string UTF16ToUTF8(const std::u16string& input) {
201#if _MSC_VER >= 1900 196#ifdef _MSC_VER
202 // Workaround for missing char16_t/char32_t instantiations in MSVC2015 197 // Workaround for missing char16_t/char32_t instantiations in MSVC2017
203 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert; 198 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
204 std::basic_string<__int16> tmp_buffer(input.cbegin(), input.cend()); 199 std::basic_string<__int16> tmp_buffer(input.cbegin(), input.cend());
205 return convert.to_bytes(tmp_buffer); 200 return convert.to_bytes(tmp_buffer);
@@ -210,8 +205,8 @@ std::string UTF16ToUTF8(const std::u16string& input) {
210} 205}
211 206
212std::u16string UTF8ToUTF16(const std::string& input) { 207std::u16string UTF8ToUTF16(const std::string& input) {
213#if _MSC_VER >= 1900 208#ifdef _MSC_VER
214 // Workaround for missing char16_t/char32_t instantiations in MSVC2015 209 // Workaround for missing char16_t/char32_t instantiations in MSVC2017
215 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert; 210 std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
216 auto tmp_buffer = convert.from_bytes(input); 211 auto tmp_buffer = convert.from_bytes(input);
217 return std::u16string(tmp_buffer.cbegin(), tmp_buffer.cend()); 212 return std::u16string(tmp_buffer.cbegin(), tmp_buffer.cend());
@@ -221,6 +216,7 @@ std::u16string UTF8ToUTF16(const std::string& input) {
221#endif 216#endif
222} 217}
223 218
219#ifdef _WIN32
224static std::wstring CPToUTF16(u32 code_page, const std::string& input) { 220static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
225 const auto size = 221 const auto size =
226 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0); 222 MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
@@ -261,124 +257,6 @@ std::wstring UTF8ToUTF16W(const std::string& input) {
261 return CPToUTF16(CP_UTF8, input); 257 return CPToUTF16(CP_UTF8, input);
262} 258}
263 259
264std::string SHIFTJISToUTF8(const std::string& input) {
265 return UTF16ToUTF8(CPToUTF16(932, input));
266}
267
268std::string CP1252ToUTF8(const std::string& input) {
269 return UTF16ToUTF8(CPToUTF16(1252, input));
270}
271
272#else
273
274template <typename T>
275static std::string CodeToUTF8(const char* fromcode, const std::basic_string<T>& input) {
276 iconv_t const conv_desc = iconv_open("UTF-8", fromcode);
277 if ((iconv_t)(-1) == conv_desc) {
278 LOG_ERROR(Common, "Iconv initialization failure [{}]: {}", fromcode, strerror(errno));
279 iconv_close(conv_desc);
280 return {};
281 }
282
283 const std::size_t in_bytes = sizeof(T) * input.size();
284 // Multiply by 4, which is the max number of bytes to encode a codepoint
285 const std::size_t out_buffer_size = 4 * in_bytes;
286
287 std::string out_buffer(out_buffer_size, '\0');
288
289 auto src_buffer = &input[0];
290 std::size_t src_bytes = in_bytes;
291 auto dst_buffer = &out_buffer[0];
292 std::size_t dst_bytes = out_buffer.size();
293
294 while (0 != src_bytes) {
295 std::size_t const iconv_result =
296 iconv(conv_desc, (char**)(&src_buffer), &src_bytes, &dst_buffer, &dst_bytes);
297
298 if (static_cast<std::size_t>(-1) == iconv_result) {
299 if (EILSEQ == errno || EINVAL == errno) {
300 // Try to skip the bad character
301 if (0 != src_bytes) {
302 --src_bytes;
303 ++src_buffer;
304 }
305 } else {
306 LOG_ERROR(Common, "iconv failure [{}]: {}", fromcode, strerror(errno));
307 break;
308 }
309 }
310 }
311
312 std::string result;
313 out_buffer.resize(out_buffer_size - dst_bytes);
314 out_buffer.swap(result);
315
316 iconv_close(conv_desc);
317
318 return result;
319}
320
321std::u16string UTF8ToUTF16(const std::string& input) {
322 iconv_t const conv_desc = iconv_open("UTF-16LE", "UTF-8");
323 if ((iconv_t)(-1) == conv_desc) {
324 LOG_ERROR(Common, "Iconv initialization failure [UTF-8]: {}", strerror(errno));
325 iconv_close(conv_desc);
326 return {};
327 }
328
329 const std::size_t in_bytes = sizeof(char) * input.size();
330 // Multiply by 4, which is the max number of bytes to encode a codepoint
331 const std::size_t out_buffer_size = 4 * sizeof(char16_t) * in_bytes;
332
333 std::u16string out_buffer(out_buffer_size, char16_t{});
334
335 char* src_buffer = const_cast<char*>(&input[0]);
336 std::size_t src_bytes = in_bytes;
337 char* dst_buffer = (char*)(&out_buffer[0]);
338 std::size_t dst_bytes = out_buffer.size();
339
340 while (0 != src_bytes) {
341 std::size_t const iconv_result =
342 iconv(conv_desc, &src_buffer, &src_bytes, &dst_buffer, &dst_bytes);
343
344 if (static_cast<std::size_t>(-1) == iconv_result) {
345 if (EILSEQ == errno || EINVAL == errno) {
346 // Try to skip the bad character
347 if (0 != src_bytes) {
348 --src_bytes;
349 ++src_buffer;
350 }
351 } else {
352 LOG_ERROR(Common, "iconv failure [UTF-8]: {}", strerror(errno));
353 break;
354 }
355 }
356 }
357
358 std::u16string result;
359 out_buffer.resize(out_buffer_size - dst_bytes);
360 out_buffer.swap(result);
361
362 iconv_close(conv_desc);
363
364 return result;
365}
366
367std::string UTF16ToUTF8(const std::u16string& input) {
368 return CodeToUTF8("UTF-16LE", input);
369}
370
371std::string CP1252ToUTF8(const std::string& input) {
372 // return CodeToUTF8("CP1252//TRANSLIT", input);
373 // return CodeToUTF8("CP1252//IGNORE", input);
374 return CodeToUTF8("CP1252", input);
375}
376
377std::string SHIFTJISToUTF8(const std::string& input) {
378 // return CodeToUTF8("CP932", input);
379 return CodeToUTF8("SJIS", input);
380}
381
382#endif 260#endif
383 261
384std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len) { 262std::string StringFromFixedZeroTerminatedBuffer(const char* buffer, std::size_t max_len) {
diff --git a/src/common/string_util.h b/src/common/string_util.h
index dcca6bc38..32bf6a19c 100644
--- a/src/common/string_util.h
+++ b/src/common/string_util.h
@@ -72,31 +72,10 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
72std::string UTF16ToUTF8(const std::u16string& input); 72std::string UTF16ToUTF8(const std::u16string& input);
73std::u16string UTF8ToUTF16(const std::string& input); 73std::u16string UTF8ToUTF16(const std::string& input);
74 74
75std::string CP1252ToUTF8(const std::string& str);
76std::string SHIFTJISToUTF8(const std::string& str);
77
78#ifdef _WIN32 75#ifdef _WIN32
79std::string UTF16ToUTF8(const std::wstring& input); 76std::string UTF16ToUTF8(const std::wstring& input);
80std::wstring UTF8ToUTF16W(const std::string& str); 77std::wstring UTF8ToUTF16W(const std::string& str);
81 78
82#ifdef _UNICODE
83inline std::string TStrToUTF8(const std::wstring& str) {
84 return UTF16ToUTF8(str);
85}
86
87inline std::wstring UTF8ToTStr(const std::string& str) {
88 return UTF8ToUTF16W(str);
89}
90#else
91inline std::string TStrToUTF8(const std::string& str) {
92 return str;
93}
94
95inline std::string UTF8ToTStr(const std::string& str) {
96 return str;
97}
98#endif
99
100#endif 79#endif
101 80
102/** 81/**
diff --git a/src/common/web_result.h b/src/common/web_result.h
new file mode 100644
index 000000000..969926674
--- /dev/null
+++ b/src/common/web_result.h
@@ -0,0 +1,24 @@
1// Copyright 2018 yuzu 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 <string>
8
9namespace Common {
10struct WebResult {
11 enum class Code : u32 {
12 Success,
13 InvalidURL,
14 CredentialsMissing,
15 LibError,
16 HttpError,
17 WrongContent,
18 NoWebservice,
19 };
20 Code result_code;
21 std::string result_string;
22 std::string returned_data;
23};
24} // namespace Common