diff options
Diffstat (limited to 'src/FbTk')
-rw-r--r-- | src/FbTk/FbString.cc | 49 | ||||
-rw-r--r-- | src/FbTk/FbString.hh | 20 |
2 files changed, 67 insertions, 2 deletions
diff --git a/src/FbTk/FbString.cc b/src/FbTk/FbString.cc index 93f6aa7..3ed1c70 100644 --- a/src/FbTk/FbString.cc +++ b/src/FbTk/FbString.cc | |||
@@ -54,6 +54,8 @@ static iconv_t *iconv_convs = 0; | |||
54 | static int iconv_convs[CONVSIZE]; | 54 | static int iconv_convs[CONVSIZE]; |
55 | #endif // HAVE_ICONV | 55 | #endif // HAVE_ICONV |
56 | 56 | ||
57 | static std::string locale_codeset; | ||
58 | |||
57 | /// Initialise all of the iconv conversion descriptors | 59 | /// Initialise all of the iconv conversion descriptors |
58 | void init() { | 60 | void init() { |
59 | setlocale(LC_CTYPE, ""); | 61 | setlocale(LC_CTYPE, ""); |
@@ -65,9 +67,9 @@ void init() { | |||
65 | iconv_convs = new iconv_t[CONVSIZE]; | 67 | iconv_convs = new iconv_t[CONVSIZE]; |
66 | 68 | ||
67 | #ifdef CODESET | 69 | #ifdef CODESET |
68 | std::string locale_codeset = nl_langinfo(CODESET); | 70 | locale_codeset = nl_langinfo(CODESET); |
69 | #else // openbsd doesnt have this (yet?) | 71 | #else // openbsd doesnt have this (yet?) |
70 | std::string locale_codeset = ""; | 72 | locale_codeset = ""; |
71 | std::string locale = setlocale(LC_CTYPE, NULL); | 73 | std::string locale = setlocale(LC_CTYPE, NULL); |
72 | size_t pos = locale.find('.'); | 74 | size_t pos = locale.find('.'); |
73 | if (pos != std::string::npos) | 75 | if (pos != std::string::npos) |
@@ -229,4 +231,47 @@ bool haveUTF8() { | |||
229 | 231 | ||
230 | }; // end namespace StringUtil | 232 | }; // end namespace StringUtil |
231 | 233 | ||
234 | StringConvertor::StringConvertor(EncodingTarget target): m_iconv((iconv_t)(-1)) { | ||
235 | #ifdef HAVE_ICONV | ||
236 | if (target == ToLocaleStr) | ||
237 | m_destencoding = FbStringUtil::locale_codeset; | ||
238 | else | ||
239 | m_destencoding = "UTF-8"; | ||
240 | #endif | ||
241 | } | ||
242 | |||
243 | StringConvertor::~StringConvertor() { | ||
244 | #ifdef HAVE_ICONV | ||
245 | if (m_iconv != ((iconv_t)-1)) | ||
246 | iconv_close(m_iconv); | ||
247 | #endif | ||
248 | } | ||
249 | |||
250 | bool StringConvertor::setSource(const std::string &encoding) { | ||
251 | #ifdef HAVE_ICONV | ||
252 | std::string tempenc = encoding; | ||
253 | if (encoding == "") | ||
254 | tempenc = FbStringUtil::locale_codeset; | ||
255 | |||
256 | iconv_t newiconv = iconv_open(m_destencoding.c_str(), tempenc.c_str()); | ||
257 | if (newiconv == ((iconv_t)(-1))) | ||
258 | return false; | ||
259 | else { | ||
260 | iconv_close(m_iconv); | ||
261 | m_iconv = newiconv; | ||
262 | return true; | ||
263 | } | ||
264 | #else | ||
265 | return false; | ||
266 | #endif | ||
267 | } | ||
268 | |||
269 | std::string StringConvertor::recode(const std::string &src) { | ||
270 | #ifdef HAVE_ICONV | ||
271 | return FbStringUtil::recode(m_iconv, src); | ||
272 | #else | ||
273 | return src; | ||
274 | #endif | ||
275 | } | ||
276 | |||
232 | }; // end namespace FbTk | 277 | }; // end namespace FbTk |
diff --git a/src/FbTk/FbString.hh b/src/FbTk/FbString.hh index 011d47a..cfa44d9 100644 --- a/src/FbTk/FbString.hh +++ b/src/FbTk/FbString.hh | |||
@@ -52,6 +52,26 @@ std::string FbStrToLocale(const FbString &src); | |||
52 | bool haveUTF8(); | 52 | bool haveUTF8(); |
53 | 53 | ||
54 | } // namespace FbStringUtil | 54 | } // namespace FbStringUtil |
55 | |||
56 | class StringConvertor { | ||
57 | public: | ||
58 | |||
59 | enum EncodingTarget { ToFbString, ToLocaleStr }; | ||
60 | |||
61 | StringConvertor(EncodingTarget target); | ||
62 | ~StringConvertor(); | ||
63 | |||
64 | bool setSource(const std::string &encoding); | ||
65 | void reset() { m_iconv = ((iconv_t)(-1)); } | ||
66 | |||
67 | std::string recode(const std::string &src); | ||
68 | |||
69 | private: | ||
70 | iconv_t m_iconv; | ||
71 | |||
72 | std::string m_destencoding; | ||
73 | }; | ||
74 | |||
55 | } // namespace FbTk | 75 | } // namespace FbTk |
56 | 76 | ||
57 | #endif // FBTK_FBSTRING_HH | 77 | #endif // FBTK_FBSTRING_HH |