xbmc
CharsetConverter.h
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 
18 class CSetting;
19 struct StringSettingOption;
20 
22 {
23 public:
25 
26  void OnSettingChanged(const 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 
43  static bool utf8ToUtf32(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool failOnBadChar = true);
52  static std::u32string utf8ToUtf32(const std::string& utf8StringSrc, bool failOnBadChar = true);
66  static bool utf8ToUtf32Visual(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool bVisualBiDiFlip = false, bool forceLTRReadingOrder = false, bool failOnBadChar = false);
76  static bool utf32ToUtf8(const std::u32string& utf32StringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
85  static std::string utf32ToUtf8(const std::u32string& utf32StringSrc, bool failOnBadChar = false);
95  static bool utf32ToW(const std::u32string& utf32StringSrc, std::wstring& wStringDst, bool failOnBadChar = false);
104  static bool utf32logicalToVisualBiDi(const std::u32string& logicalStringSrc,
105  std::u32string& visualStringDst,
106  bool forceLTRReadingOrder = false,
107  bool failOnBadString = false,
108  int* visualToLogicalMap = nullptr);
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 
149  static bool utf16BEtoUTF8(const std::u16string& utf16StringSrc, std::string& utf8StringDst);
150 
158  static bool utf16BEtoUTF8(const std::string& utf16StringSrc, std::string& utf8StringDst);
159 
160  static bool utf16LEtoUTF8(const std::u16string& utf16StringSrc, std::string& utf8StringDst);
161  static bool ucs2ToUTF8(const std::u16string& ucs2StringSrc, std::string& utf8StringDst);
162 
170  static bool MacintoshToUTF8(const std::string& macStringSrc, std::string& utf8StringDst);
171 
172  static bool utf8logicalToVisualBiDi(const std::string& utf8StringSrc, std::string& utf8StringDst, bool failOnBadString = false);
173 
179  static bool utf8IsRTLBidiDirection(const std::string& utf8String);
180 
181  static bool utf32ToStringCharset(const std::u32string& utf32StringSrc, std::string& stringDst);
182 
183  static std::vector<std::string> getCharsetLabels();
184  static std::string getCharsetLabelByName(const std::string& charsetName);
185  static std::string getCharsetNameByLabel(const std::string& charsetLabel);
186 
187  static bool unknownToUTF8(std::string& stringSrcDst);
188  static bool unknownToUTF8(const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
189 
190  static bool toW(const std::string& stringSrc, std::wstring& wStringDst, const std::string& enc);
191  static bool fromW(const std::wstring& wStringSrc, std::string& stringDst, const std::string& enc);
192 
193  static void SettingOptionsCharsetsFiller(const std::shared_ptr<const CSetting>& setting,
194  std::vector<StringSettingOption>& list,
195  std::string& current,
196  void* data);
197 
198 private:
199  static void resetUserCharset(void);
200  static void resetSubtitleCharset(void);
201 
202  static const int m_Utf8CharMinSize, m_Utf8CharMaxSize;
203  class CInnerConverter;
204 };
205 
206 XBMC_GLOBAL_REF(CCharsetConverter,g_charsetConverter);
207 #define g_charsetConverter XBMC_GLOBAL_USE(CCharsetConverter)
static bool wToUtf32(const std::wstring &wStringSrc, std::u32string &utf32StringDst, bool failOnBadChar=false)
Strictly convert wchar_t string (wstring) to UTF-32 string.
Definition: CharsetConverter.cpp:724
static bool utf32ToW(const std::u32string &utf32StringSrc, std::wstring &wStringDst, bool failOnBadChar=false)
Convert UTF-32 string to wchar_t string (wstring).
Definition: CharsetConverter.cpp:703
Definition: ISettingCallback.h:16
Setting base class containing all the properties which are common to all settings independent of the ...
Definition: Setting.h:46
static bool utf8IsRTLBidiDirection(const std::string &utf8String)
Check if a string has RTL direction.
Definition: CharsetConverter.cpp:894
static bool utf32logicalToVisualBiDi(const std::u32string &logicalStringSrc, std::u32string &visualStringDst, bool forceLTRReadingOrder=false, bool failOnBadString=false, int *visualToLogicalMap=nullptr)
Perform logical to visual flip.
Definition: CharsetConverter.cpp:713
Definition: SettingDefinitions.h:85
Definition: CharsetConverter.cpp:268
static bool utf16BEtoUTF8(const std::u16string &utf16StringSrc, std::string &utf8StringDst)
Convert UTF-16BE (u16string) string to UTF-8 string. No RTL visual-logical transformation is performe...
Definition: CharsetConverter.cpp:837
static bool utf8ToUtf32(const std::string &utf8StringSrc, std::u32string &utf32StringDst, bool failOnBadChar=true)
Convert UTF-8 string to UTF-32 string.
Definition: CharsetConverter.cpp:666
void OnSettingChanged(const std::shared_ptr< const CSetting > &setting) override
The value of the given setting has changed.
Definition: CharsetConverter.cpp:589
static bool utf32ToUtf8(const std::u32string &utf32StringSrc, std::string &utf8StringDst, bool failOnBadChar=false)
Convert UTF-32 string to UTF-8 string.
Definition: CharsetConverter.cpp:691
static bool utf8ToUtf32Visual(const std::string &utf8StringSrc, std::u32string &utf32StringDst, bool bVisualBiDiFlip=false, bool forceLTRReadingOrder=false, bool failOnBadChar=false)
Convert UTF-8 string to UTF-32 string.
Definition: CharsetConverter.cpp:678
Definition: CharsetConverter.h:21
static bool MacintoshToUTF8(const std::string &macStringSrc, std::string &utf8StringDst)
Convert Macintosh (string) string to UTF-8 string. No RTL visual-logical transformation is performed...
Definition: CharsetConverter.cpp:879