summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/CharsetConverter.h
diff options
context:
space:
mode:
Diffstat (limited to 'xbmc/utils/CharsetConverter.h')
-rw-r--r--xbmc/utils/CharsetConverter.h169
1 files changed, 169 insertions, 0 deletions
diff --git a/xbmc/utils/CharsetConverter.h b/xbmc/utils/CharsetConverter.h
new file mode 100644
index 0000000..1c7e014
--- /dev/null
+++ b/xbmc/utils/CharsetConverter.h
@@ -0,0 +1,169 @@
1/*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#pragma once
10
11#include "settings/lib/ISettingCallback.h"
12#include "utils/GlobalsHandling.h"
13
14#include <string>
15#include <utility>
16#include <vector>
17
18class CSetting;
19struct StringSettingOption;
20
21class CCharsetConverter : public ISettingCallback
22{
23public:
24 CCharsetConverter();
25
26 void OnSettingChanged(std::shared_ptr<const CSetting> setting) override;
27
28 static void reset();
29 static void resetSystemCharset();
30 static void reinitCharsetsFromSettings(void);
31
32 static void clear();
33
34 /**
35 * Convert UTF-8 string to UTF-32 string.
36 * No RTL logical-visual transformation is performed.
37 * @param utf8StringSrc is source UTF-8 string to convert
38 * @param utf32StringDst is output UTF-32 string, empty on any error
39 * @param failOnBadChar if set to true function will fail on invalid character,
40 * otherwise invalid character will be skipped
41 * @return true on successful conversion, false on any error
42 */
43 static bool utf8ToUtf32(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool failOnBadChar = true);
44 /**
45 * Convert UTF-8 string to UTF-32 string.
46 * No RTL logical-visual transformation is performed.
47 * @param utf8StringSrc is source UTF-8 string to convert
48 * @param failOnBadChar if set to true function will fail on invalid character,
49 * otherwise invalid character will be skipped
50 * @return converted string on successful conversion, empty string on any error
51 */
52 static std::u32string utf8ToUtf32(const std::string& utf8StringSrc, bool failOnBadChar = true);
53 /**
54 * Convert UTF-8 string to UTF-32 string.
55 * RTL logical-visual transformation is optionally performed.
56 * Use it for readable text, GUI strings etc.
57 * @param utf8StringSrc is source UTF-8 string to convert
58 * @param utf32StringDst is output UTF-32 string, empty on any error
59 * @param bVisualBiDiFlip allow RTL visual-logical transformation if set to true, must be set
60 * to false is logical-visual transformation is already done
61 * @param forceLTRReadingOrder force LTR reading order
62 * @param failOnBadChar if set to true function will fail on invalid character,
63 * otherwise invalid character will be skipped
64 * @return true on successful conversion, false on any error
65 */
66 static bool utf8ToUtf32Visual(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool bVisualBiDiFlip = false, bool forceLTRReadingOrder = false, bool failOnBadChar = false);
67 /**
68 * Convert UTF-32 string to UTF-8 string.
69 * No RTL visual-logical transformation is performed.
70 * @param utf32StringSrc is source UTF-32 string to convert
71 * @param utf8StringDst is output UTF-8 string, empty on any error
72 * @param failOnBadChar if set to true function will fail on invalid character,
73 * otherwise invalid character will be skipped
74 * @return true on successful conversion, false on any error
75 */
76 static bool utf32ToUtf8(const std::u32string& utf32StringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
77 /**
78 * Convert UTF-32 string to UTF-8 string.
79 * No RTL visual-logical transformation is performed.
80 * @param utf32StringSrc is source UTF-32 string to convert
81 * @param failOnBadChar if set to true function will fail on invalid character,
82 * otherwise invalid character will be skipped
83 * @return converted string on successful conversion, empty string on any error
84 */
85 static std::string utf32ToUtf8(const std::u32string& utf32StringSrc, bool failOnBadChar = false);
86 /**
87 * Convert UTF-32 string to wchar_t string (wstring).
88 * No RTL visual-logical transformation is performed.
89 * @param utf32StringSrc is source UTF-32 string to convert
90 * @param wStringDst is output wchar_t string, empty on any error
91 * @param failOnBadChar if set to true function will fail on invalid character,
92 * otherwise invalid character will be skipped
93 * @return true on successful conversion, false on any error
94 */
95 static bool utf32ToW(const std::u32string& utf32StringSrc, std::wstring& wStringDst, bool failOnBadChar = false);
96 /**
97 * Perform logical to visual flip.
98 * @param logicalStringSrc is source string with logical characters order
99 * @param visualStringDst is output string with visual characters order, empty on any error
100 * @param forceLTRReadingOrder force LTR reading order
101 * @param visualToLogicalMap is output mapping of positions in the visual string to the logical string
102 * @return true on success, false otherwise
103 */
104 static bool utf32logicalToVisualBiDi(const std::u32string& logicalStringSrc,
105 std::u32string& visualStringDst,
106 bool forceLTRReadingOrder = false,
107 bool failOnBadString = false,
108 int* visualToLogicalMap = nullptr);
109 /**
110 * Strictly convert wchar_t string (wstring) to UTF-32 string.
111 * No RTL visual-logical transformation is performed.
112 * @param wStringSrc is source wchar_t string to convert
113 * @param utf32StringDst is output UTF-32 string, empty on any error
114 * @param failOnBadChar if set to true function will fail on invalid character,
115 * otherwise invalid character will be skipped
116 * @return true on successful conversion, false on any error
117 */
118 static bool wToUtf32(const std::wstring& wStringSrc, std::u32string& utf32StringDst, bool failOnBadChar = false);
119
120 static bool utf8ToW(const std::string& utf8StringSrc, std::wstring& wStringDst,
121 bool bVisualBiDiFlip = true, bool forceLTRReadingOrder = false,
122 bool failOnBadChar = false);
123
124 static bool utf16LEtoW(const std::u16string& utf16String, std::wstring& wString);
125
126 static bool subtitleCharsetToUtf8(const std::string& stringSrc, std::string& utf8StringDst);
127
128 static bool utf8ToStringCharset(const std::string& utf8StringSrc, std::string& stringDst);
129
130 static bool utf8ToStringCharset(std::string& stringSrcDst);
131 static bool utf8ToSystem(std::string& stringSrcDst, bool failOnBadChar = false);
132 static bool systemToUtf8(const std::string& sysStringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
133
134 static bool utf8To(const std::string& strDestCharset, const std::string& utf8StringSrc, std::string& stringDst);
135 static bool utf8To(const std::string& strDestCharset, const std::string& utf8StringSrc, std::u16string& utf16StringDst);
136 static bool utf8To(const std::string& strDestCharset, const std::string& utf8StringSrc, std::u32string& utf32StringDst);
137
138 static bool ToUtf8(const std::string& strSourceCharset, const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
139
140 static bool wToUTF8(const std::wstring& wStringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
141 static bool utf16BEtoUTF8(const std::u16string& utf16StringSrc, std::string& utf8StringDst);
142 static bool utf16LEtoUTF8(const std::u16string& utf16StringSrc, std::string& utf8StringDst);
143 static bool ucs2ToUTF8(const std::u16string& ucs2StringSrc, std::string& utf8StringDst);
144
145 static bool utf8logicalToVisualBiDi(const std::string& utf8StringSrc, std::string& utf8StringDst, bool failOnBadString = false);
146
147 static bool utf32ToStringCharset(const std::u32string& utf32StringSrc, std::string& stringDst);
148
149 static std::vector<std::string> getCharsetLabels();
150 static std::string getCharsetLabelByName(const std::string& charsetName);
151 static std::string getCharsetNameByLabel(const std::string& charsetLabel);
152
153 static bool unknownToUTF8(std::string& stringSrcDst);
154 static bool unknownToUTF8(const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
155
156 static bool toW(const std::string& stringSrc, std::wstring& wStringDst, const std::string& enc);
157 static bool fromW(const std::wstring& wStringSrc, std::string& stringDst, const std::string& enc);
158
159 static void SettingOptionsCharsetsFiller(std::shared_ptr<const CSetting> setting, std::vector<StringSettingOption>& list, std::string& current, void *data);
160private:
161 static void resetUserCharset(void);
162 static void resetSubtitleCharset(void);
163
164 static const int m_Utf8CharMinSize, m_Utf8CharMaxSize;
165 class CInnerConverter;
166};
167
168XBMC_GLOBAL_REF(CCharsetConverter,g_charsetConverter);
169#define g_charsetConverter XBMC_GLOBAL_USE(CCharsetConverter)