Line data Source code
1 : // tinygettext - A gettext replacement that works directly on .po files
2 : // Copyright (c) 2006 Ingo Ruhnke <grumbel@gmail.com>
3 : //
4 : // This software is provided 'as-is', without any express or implied
5 : // warranty. In no event will the authors be held liable for any damages
6 : // arising from the use of this software.
7 : //
8 : // Permission is granted to anyone to use this software for any purpose,
9 : // including commercial applications, and to alter it and redistribute it
10 : // freely, subject to the following restrictions:
11 : //
12 : // 1. The origin of this software must not be misrepresented; you must not
13 : // claim that you wrote the original software. If you use this software
14 : // in a product, an acknowledgement in the product documentation would be
15 : // appreciated but is not required.
16 : // 2. Altered source versions must be plainly marked as such, and must not be
17 : // misrepresented as being the original software.
18 : // 3. This notice may not be removed or altered from any source distribution.
19 :
20 : #ifndef HEADER_TINYGETTEXT_ICONV_HPP
21 : #define HEADER_TINYGETTEXT_ICONV_HPP
22 :
23 : #include <string>
24 :
25 : #ifdef TINYGETTEXT_WITH_SDL
26 : # include "SDL.h"
27 : #else
28 : # include <iconv.h>
29 : #endif
30 :
31 : namespace tinygettext {
32 :
33 : namespace detail {
34 : struct ConstPtrHack {
35 : const char** ptr;
36 : inline ConstPtrHack(char** ptr_) : ptr(const_cast<const char**>(ptr_)) {}
37 0 : inline ConstPtrHack(const char** ptr_) : ptr(ptr_) {}
38 : inline operator const char**() const { return ptr; }
39 0 : inline operator char**() const { return const_cast<char**>(ptr); }
40 : };
41 : } // namespace detail
42 :
43 : #ifdef TINYGETTEXT_WITH_SDL
44 : using iconv_t = ::SDL_iconv_t;
45 : #else
46 : using iconv_t = ::iconv_t;
47 : #endif
48 :
49 0 : inline iconv_t iconv_open(const char* tocode, const char* fromcode)
50 : {
51 : #ifdef TINYGETTEXT_WITH_SDL
52 : return SDL_iconv_open(tocode, fromcode);
53 : #else
54 0 : return ::iconv_open(tocode, fromcode);
55 : #endif
56 : }
57 :
58 0 : inline size_t iconv(iconv_t cd,
59 : const char** inbuf, size_t* inbytesleft,
60 : char** outbuf, size_t* outbytesleft)
61 : {
62 : #ifdef TINYGETTEXT_WITH_SDL
63 : return SDL_iconv(cd, inbuf, inbytesleft, outbuf, outbytesleft);
64 : #else
65 0 : return ::iconv(cd, detail::ConstPtrHack(inbuf), inbytesleft, outbuf, outbytesleft);
66 : #endif
67 : }
68 :
69 0 : inline int iconv_close(iconv_t cd)
70 : {
71 : #ifdef TINYGETTEXT_WITH_SDL
72 : return SDL_iconv_close(cd);
73 : #else
74 0 : return ::iconv_close(cd);
75 : #endif
76 : }
77 :
78 : class IConv
79 : {
80 : private:
81 : std::string to_charset;
82 : std::string from_charset;
83 : iconv_t cd;
84 :
85 : public:
86 : IConv();
87 : IConv(const std::string& fromcode, const std::string& tocode);
88 : ~IConv();
89 :
90 : void set_charsets(const std::string& fromcode, const std::string& tocode);
91 : std::string convert(const std::string& text);
92 :
93 : private:
94 : IConv (const IConv&);
95 : IConv& operator= (const IConv&);
96 : };
97 :
98 : } // namespace tinygettext
99 :
100 : #endif
101 :
102 : /* EOF */
|