iconvConvert.cc
Go to the documentation of this file.
00001 /* iconvConvert.cc
00002  */
00003 #include "osl/misc/iconvConvert.h"
00004 #include <cstdlib>
00005 #include <cstring>
00006 #ifndef _WIN32
00007 #include <iconv.h>
00008 
00009 struct osl::misc::IconvConvert::IconvCD
00010 {
00011   iconv_t cd;
00012   IconvCD(const std::string& fromcode, const std::string& tocode)
00013   {
00014     cd = iconv_open(tocode.c_str(), fromcode.c_str());
00015   }
00016   ~IconvCD()
00017   {
00018     iconv_close(cd);
00019   }
00020 };
00021 
00022 
00023 std::string osl::misc::
00024 IconvConvert::langToIconvCode(const std::string& lang)
00025 {
00026   if (lang.empty())
00027     return "";
00028   const bool euc_jp =
00029     (lang.find("jp") != lang.npos || lang.find("JP") != lang.npos)
00030     && (lang.find("euc") != lang.npos || lang.find("EUC") != lang.npos);
00031   if (euc_jp)
00032     return "EUC-JP";
00033   const bool shift_jis =
00034     (lang.find("sjis") != lang.npos || lang.find("SJIS") != lang.npos);
00035   if (shift_jis)
00036     return "SHIFT_JIS";
00037   const bool utf8 =
00038     (lang.find("UTF-8") != lang.npos || lang.find("UTF8") != lang.npos);
00039   if (utf8)
00040     return "UTF-8";
00041   return "";
00042 }
00043 
00044 std::string osl::misc::
00045 IconvConvert::eucToLang(const std::string& src)
00046 {
00047   static const char *lang = getenv("LANG");
00048   if (! lang)
00049     return "";
00050   static const std::string tocode = langToIconvCode(lang);
00051   if (tocode.empty())
00052     return "";
00053   if ("EUC-JP" == tocode)
00054     return src;
00055   IconvCD cd("EUC-JP", tocode);
00056   return convert(cd, src);
00057 }
00058 
00059 std::string osl::misc::
00060 IconvConvert::convert(const std::string& fromcode,
00061                       const std::string& tocode,
00062                       const std::string& src)
00063 {
00064   if (fromcode == tocode)
00065     return src;
00066   IconvCD cd(fromcode, tocode);
00067   return convert(cd, src);
00068 }
00069 
00070 std::string osl::misc::
00071 IconvConvert::convert(IconvCD& cd, const std::string& src)
00072 {
00073   const char * inbuf = src.c_str();
00074   char outbuf[1024], *outbufptr = outbuf;
00075   size_t inbytesleft = src.size(), outbytesleft = 1024;
00076 #if (defined __linux__ || defined __APPLE__)
00077   typedef char ** iconv_inbuf_t;
00078 #else
00079   typedef const char ** iconv_inbuf_t;
00080 #endif
00081   std::string ret;
00082   int success;
00083   while ((success = iconv(cd.cd, (iconv_inbuf_t)&inbuf, &inbytesleft, 
00084                           &outbufptr, &outbytesleft)) >= 0
00085          && inbytesleft > 0)
00086     if (outbufptr - outbuf >= 512)
00087     {
00088       ret += std::string(outbuf, outbufptr);
00089       outbufptr = outbuf;
00090     }
00091   if (success == -1)
00092     return "";
00093   ret += std::string(outbuf, outbufptr);
00094   return ret;
00095 }
00096 
00097 #endif /* _WIN32 */
00098 // ;;; Local Variables:
00099 // ;;; mode:c++
00100 // ;;; c-basic-offset:2
00101 // ;;; End:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines