1 #ifndef CCHARSET_CONVERTER
2 #define CCHARSET_CONVERTER
5 * Copyright (C) 2005-2013 Team XBMC
8 * This Program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * This Program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with XBMC; see the file COPYING. If not, see
20 * <http://www.gnu.org/licenses/>.
24 #include "settings/lib/ISettingCallback.h"
25 #include "threads/CriticalSection.h"
26 #include "utils/GlobalsHandling.h"
27 #include "utils/uXstrings.h"
34 class CCharsetConverter : public ISettingCallback
39 virtual void OnSettingChanged(const CSetting* setting);
42 static void resetSystemCharset();
43 static void reinitCharsetsFromSettings(void);
48 * Convert UTF-8 string to UTF-32 string.
49 * No RTL logical-visual transformation is performed.
50 * @param utf8StringSrc is source UTF-8 string to convert
51 * @param utf32StringDst is output UTF-32 string, empty on any error
52 * @param failOnBadChar if set to true function will fail on invalid character,
53 * otherwise invalid character will be skipped
54 * @return true on successful conversion, false on any error
56 static bool utf8ToUtf32(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool failOnBadChar = true);
58 * Convert UTF-8 string to UTF-32 string.
59 * No RTL logical-visual transformation is performed.
60 * @param utf8StringSrc is source UTF-8 string to convert
61 * @param failOnBadChar if set to true function will fail on invalid character,
62 * otherwise invalid character will be skipped
63 * @return converted string on successful conversion, empty string on any error
65 static std::u32string utf8ToUtf32(const std::string& utf8StringSrc, bool failOnBadChar = true);
67 * Convert UTF-8 string to UTF-32 string.
68 * RTL logical-visual transformation is optionally performed.
69 * Use it for readable text, GUI strings etc.
70 * @param utf8StringSrc is source UTF-8 string to convert
71 * @param utf32StringDst is output UTF-32 string, empty on any error
72 * @param bVisualBiDiFlip allow RTL visual-logical transformation if set to true, must be set
73 * to false is logical-visual transformation is already done
74 * @param forceLTRReadingOrder force LTR reading order
75 * @param failOnBadChar if set to true function will fail on invalid character,
76 * otherwise invalid character will be skipped
77 * @return true on successful conversion, false on any error
79 static bool utf8ToUtf32Visual(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool bVisualBiDiFlip = false, bool forceLTRReadingOrder = false, bool failOnBadChar = false);
81 * Convert UTF-32 string to UTF-8 string.
82 * No RTL visual-logical transformation is performed.
83 * @param utf32StringSrc is source UTF-32 string to convert
84 * @param utf8StringDst is output UTF-8 string, empty on any error
85 * @param failOnBadChar if set to true function will fail on invalid character,
86 * otherwise invalid character will be skipped
87 * @return true on successful conversion, false on any error
89 static bool utf32ToUtf8(const std::u32string& utf32StringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
91 * Convert UTF-32 string to UTF-8 string.
92 * No RTL visual-logical transformation is performed.
93 * @param utf32StringSrc is source UTF-32 string to convert
94 * @param failOnBadChar if set to true function will fail on invalid character,
95 * otherwise invalid character will be skipped
96 * @return converted string on successful conversion, empty string on any error
98 static std::string utf32ToUtf8(const std::u32string& utf32StringSrc, bool failOnBadChar = false);
100 * Convert UTF-32 string to wchar_t string (wstring).
101 * No RTL visual-logical transformation is performed.
102 * @param utf32StringSrc is source UTF-32 string to convert
103 * @param wStringDst is output wchar_t string, empty on any error
104 * @param failOnBadChar if set to true function will fail on invalid character,
105 * otherwise invalid character will be skipped
106 * @return true on successful conversion, false on any error
108 static bool utf32ToW(const std::u32string& utf32StringSrc, std::wstring& wStringDst, bool failOnBadChar = false);
110 * Perform logical to visual flip.
111 * @param logicalStringSrc is source string with logical characters order
112 * @param visualStringDst is output string with visual characters order, empty on any error
113 * @param forceLTRReadingOrder force LTR reading order
114 * @return true on success, false otherwise
116 static bool utf32logicalToVisualBiDi(const std::u32string& logicalStringSrc, std::u32string& visualStringDst, bool forceLTRReadingOrder = false, bool failOnBadString = false);
118 * Strictly convert wchar_t string (wstring) to UTF-32 string.
119 * No RTL visual-logical transformation is performed.
120 * @param wStringSrc is source wchar_t string to convert
121 * @param utf32StringDst is output UTF-32 string, empty on any error
122 * @param failOnBadChar if set to true function will fail on invalid character,
123 * otherwise invalid character will be skipped
124 * @return true on successful conversion, false on any error
126 static bool wToUtf32(const std::wstring& wStringSrc, std::u32string& utf32StringDst, bool failOnBadChar = false);
128 static bool utf8ToW(const std::string& utf8StringSrc, std::wstring& wStringDst,
129 bool bVisualBiDiFlip = true, bool forceLTRReadingOrder = false,
130 bool failOnBadChar = false);
132 static bool utf16LEtoW(const std::u16string& utf16String, std::wstring& wString);
134 static bool subtitleCharsetToUtf8(const std::string& stringSrc, std::string& utf8StringDst);
136 static bool utf8ToStringCharset(const std::string& utf8StringSrc, std::string& stringDst);
138 static bool utf8ToStringCharset(std::string& stringSrcDst);
139 static bool utf8ToSystem(std::string& stringSrcDst, bool failOnBadChar = false);
140 static bool systemToUtf8(const std::string& sysStringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
142 static bool utf8To(const std::string& strDestCharset, const std::string& utf8StringSrc, std::string& stringDst);
143 static bool utf8To(const std::string& strDestCharset, const std::string& utf8StringSrc, std::u16string& utf16StringDst);
144 static bool utf8To(const std::string& strDestCharset, const std::string& utf8StringSrc, std::u32string& utf32StringDst);
146 static bool ToUtf8(const std::string& strSourceCharset, const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
148 static bool wToUTF8(const std::wstring& wStringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
149 static bool utf16BEtoUTF8(const std::u16string& utf16StringSrc, std::string& utf8StringDst);
150 static bool utf16LEtoUTF8(const std::u16string& utf16StringSrc, std::string& utf8StringDst);
151 static bool ucs2ToUTF8(const std::u16string& ucs2StringSrc, std::string& utf8StringDst);
153 static bool utf8logicalToVisualBiDi(const std::string& utf8StringSrc, std::string& utf8StringDst, bool failOnBadString = false);
155 static bool utf32ToStringCharset(const std::u32string& utf32StringSrc, std::string& stringDst);
157 static std::vector<std::string> getCharsetLabels();
158 static std::string getCharsetLabelByName(const std::string& charsetName);
159 static std::string getCharsetNameByLabel(const std::string& charsetLabel);
161 static bool unknownToUTF8(std::string& stringSrcDst);
162 static bool unknownToUTF8(const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
164 static bool toW(const std::string& stringSrc, std::wstring& wStringDst, const std::string& enc);
165 static bool fromW(const std::wstring& wStringSrc, std::string& stringDst, const std::string& enc);
167 static void SettingOptionsCharsetsFiller(const CSetting* setting, std::vector< std::pair<std::string, std::string> >& list, std::string& current);
169 static void resetUserCharset(void);
170 static void resetSubtitleCharset(void);
171 static void resetKaraokeCharset(void);
173 static const int m_Utf8CharMinSize, m_Utf8CharMaxSize;
174 class CInnerConverter;
177 XBMC_GLOBAL(CCharsetConverter,g_charsetConverter);