Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / utils / CharsetConverter.h
1 #ifndef CCHARSET_CONVERTER
2 #define CCHARSET_CONVERTER
3
4 /*
5  *      Copyright (C) 2005-2013 Team XBMC
6  *      http://xbmc.org
7  *
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)
11  *  any later version.
12  *
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.
17  *
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/>.
21  *
22  */
23
24 #include "settings/lib/ISettingCallback.h"
25 #include "threads/CriticalSection.h"
26 #include "utils/GlobalsHandling.h"
27 #include "utils/uXstrings.h"
28
29 #include <string>
30 #include <vector>
31
32 class CSetting;
33
34 class CCharsetConverter : public ISettingCallback
35 {
36 public:
37   CCharsetConverter();
38
39   virtual void OnSettingChanged(const CSetting* setting);
40
41   static void reset();
42   static void resetSystemCharset();
43   static void reinitCharsetsFromSettings(void);
44
45   static void clear();
46
47   /**
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
55    */
56   static bool utf8ToUtf32(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool failOnBadChar = true);
57   /**
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
64    */
65   static std::u32string utf8ToUtf32(const std::string& utf8StringSrc, bool failOnBadChar = true);
66   /**
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
78    */
79   static bool utf8ToUtf32Visual(const std::string& utf8StringSrc, std::u32string& utf32StringDst, bool bVisualBiDiFlip = false, bool forceLTRReadingOrder = false, bool failOnBadChar = false);
80   /**
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
88    */
89   static bool utf32ToUtf8(const std::u32string& utf32StringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
90   /**
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
97    */
98   static std::string utf32ToUtf8(const std::u32string& utf32StringSrc, bool failOnBadChar = false);
99   /**
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
107    */
108   static bool utf32ToW(const std::u32string& utf32StringSrc, std::wstring& wStringDst, bool failOnBadChar = false);
109   /**
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
115    */
116   static bool utf32logicalToVisualBiDi(const std::u32string& logicalStringSrc, std::u32string& visualStringDst, bool forceLTRReadingOrder = false, bool failOnBadString = false);
117   /**
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
125    */
126   static bool wToUtf32(const std::wstring& wStringSrc, std::u32string& utf32StringDst, bool failOnBadChar = false);
127
128   static bool utf8ToW(const std::string& utf8StringSrc, std::wstring& wStringDst,
129                 bool bVisualBiDiFlip = true, bool forceLTRReadingOrder = false,
130                 bool failOnBadChar = false);
131
132   static bool utf16LEtoW(const std::u16string& utf16String, std::wstring& wString);
133
134   static bool subtitleCharsetToUtf8(const std::string& stringSrc, std::string& utf8StringDst);
135
136   static bool utf8ToStringCharset(const std::string& utf8StringSrc, std::string& stringDst);
137
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);
141
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);
145
146   static bool ToUtf8(const std::string& strSourceCharset, const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
147
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);
152
153   static bool utf8logicalToVisualBiDi(const std::string& utf8StringSrc, std::string& utf8StringDst, bool failOnBadString = false);
154
155   static bool utf32ToStringCharset(const std::u32string& utf32StringSrc, std::string& stringDst);
156
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);
160
161   static bool unknownToUTF8(std::string& stringSrcDst);
162   static bool unknownToUTF8(const std::string& stringSrc, std::string& utf8StringDst, bool failOnBadChar = false);
163
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);
166
167   static void SettingOptionsCharsetsFiller(const CSetting* setting, std::vector< std::pair<std::string, std::string> >& list, std::string& current);
168 private:
169   static void resetUserCharset(void);
170   static void resetSubtitleCharset(void);
171   static void resetKaraokeCharset(void);
172
173   static const int m_Utf8CharMinSize, m_Utf8CharMaxSize;
174   class CInnerConverter;
175 };
176
177 XBMC_GLOBAL(CCharsetConverter,g_charsetConverter);
178
179 #endif