IConv.h

Go to the documentation of this file.
00001 #ifndef _GTKUTIL_ICONV_H
00002 #define _GTKUTIL_ICONV_H
00003 
00004 #include <glib/gconvert.h>
00005 #include <glib/gmessages.h>
00006 #include <glib/gunicode.h>
00007 #include <glib/gmem.h>
00008 #include <string>
00009 
00010 #include <cassert>
00011 
00012 namespace gtkutil
00013 {
00019     class IConv
00020     {
00021         private:
00022             GIConv gobject_;
00023 
00024         public:
00029             IConv (const std::string& to_codeset, const std::string& from_codeset) :
00030                 gobject_(g_iconv_open(to_codeset.c_str(), from_codeset.c_str()))
00031             {
00032                 if (gobject_ == reinterpret_cast<GIConv> (-1)) {
00033                     GError* gerror = 0;
00034 
00035                     // Abuse g_convert() to create a GError object.  This may seem a weird
00036                     // thing to do, but it gives us consistently translated error messages
00037                     // at no further cost.
00038                     g_convert("", 0, to_codeset.c_str(), from_codeset.c_str(), 0, 0, &gerror);
00039 
00040                     // If this should ever fail we're fucked.
00041                     assert(gerror != 0);
00042                 }
00043             }
00044 
00047             ~IConv ()
00048             {
00049                 g_iconv_close(gobject_);
00050             }
00051 
00062             size_t iconv (char** inbuf, gsize* inbytes_left, char** outbuf, gsize* outbytes_left)
00063             {
00064                 return g_iconv(gobject_, inbuf, inbytes_left, outbuf, outbytes_left);
00065             }
00066 
00072             void reset ()
00073             {
00074                 // Apparently iconv() on Solaris <= 7 segfaults if you pass in
00075                 // NULL for anything but inbuf; work around that. (NULL outbuf
00076                 // or NULL *outbuf is allowed by Unix98.)
00077 
00078                 char* outbuf = 0;
00079                 gsize inbytes_left = 0;
00080                 gsize outbytes_left = 0;
00081 
00082                 g_iconv(gobject_, 0, &inbytes_left, &outbuf, &outbytes_left);
00083             }
00084 
00090             std::string convert (const std::string& str)
00091             {
00092                 gsize bytes_written = 0;
00093                 GError* gerror = 0;
00094 
00095                 char * const buf = g_convert_with_iconv(str.data(), static_cast<gssize> (str.size()), gobject_, 0,
00096                         &bytes_written, &gerror);
00097 
00098                 if (gerror) {
00099                     return "";
00100                 }
00101 
00102                 std::string returnValue(buf, bytes_written);
00103                 g_free(buf);
00104 
00105                 return returnValue;
00106             }
00107 
00108             GIConv gobj ()
00109             {
00110                 return gobject_;
00111             }
00112 
00117             static std::string localeToUTF8 (const std::string& input)
00118             {
00119                 gsize bytes_written = 0;
00120                 GError* gerror = 0;
00121 
00122                 char * const buf = g_locale_to_utf8(input.data(), static_cast<gssize> (input.size()), 0,
00123                         &bytes_written, &gerror);
00124 
00125                 if (gerror)
00126                     return "";
00127 
00128                 std::string returnValue(buf, bytes_written);
00129                 g_free(buf);
00130 
00131                 return returnValue;
00132             }
00133 
00138             static std::string localeFromUTF8 (const std::string& input)
00139             {
00140                 gsize bytes_written = 0;
00141                 GError* gerror = 0;
00142 
00143                 char * const buf = g_locale_from_utf8(input.data(), static_cast<gssize> (input.size()), 0,
00144                         &bytes_written, &gerror);
00145 
00146                 if (gerror)
00147                     return "";
00148 
00149                 std::string returnValue(buf, bytes_written);
00150                 g_free(buf);
00151 
00152                 return returnValue;
00153             }
00154 
00159             static std::string filenameToUTF8 (const std::string& input)
00160             {
00161                 gsize bytes_written = 0;
00162                 GError* gerror = 0;
00163 
00164                 char * const buf = g_filename_to_utf8(input.data(), static_cast<gssize> (input.size()), 0,
00165                         &bytes_written, &gerror);
00166 
00167                 if (gerror)
00168                     return "";
00169 
00170                 std::string returnValue(buf, bytes_written);
00171                 g_free(buf);
00172 
00173                 return returnValue;
00174             }
00175     };
00176 
00177 } // namespace gtkutil
00178 
00179 #endif /* _GTKUTIL_ICONV_H */

Generated by  doxygen 1.6.2