Pyrogenesis  trunk
iconv.hpp
Go to the documentation of this file.
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  inline ConstPtrHack(const char** ptr_) : ptr(ptr_) {}
38  inline operator const char**() const { return ptr; }
39  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
47 #endif
48 
49 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  return ::iconv_open(tocode, fromcode);
55 #endif
56 }
57 
58 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  return ::iconv(cd, detail::ConstPtrHack(inbuf), inbytesleft, outbuf, outbytesleft);
66 #endif
67 }
68 
69 inline int iconv_close(iconv_t cd)
70 {
71 #ifdef TINYGETTEXT_WITH_SDL
72  return SDL_iconv_close(cd);
73 #else
75 #endif
76 }
77 
78 class IConv
79 {
80 private:
81  std::string to_charset;
82  std::string from_charset;
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 */
ConstPtrHack(const char **ptr_)
Definition: iconv.hpp:37
ConstPtrHack(char **ptr_)
Definition: iconv.hpp:36
Definition: iconv.hpp:78
std::string from_charset
Definition: iconv.hpp:82
::iconv_t iconv_t
Definition: iconv.hpp:46
Definition: iconv.hpp:34
size_t iconv(iconv_t cd, const char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft)
Definition: iconv.hpp:58
Definition: L10n.h:35
std::string to_charset
Definition: iconv.hpp:81
iconv_t iconv_open(const char *tocode, const char *fromcode)
Definition: iconv.hpp:49
iconv_t cd
Definition: iconv.hpp:83
int iconv_close(iconv_t cd)
Definition: iconv.hpp:69
const char ** ptr
Definition: iconv.hpp:35