ace_wchar.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // $Id: ace_wchar.inl 80826 2008-03-04 14:51:23Z wotte $
00004 
00005 // These are always inlined
00006 // FUZZ: disable check_for_inline
00007 
00008 #if defined (ACE_HAS_WCHAR)
00009 
00010 #if !defined (ACE_WIN32)
00011 #  include /**/ <string.h>             // Need to see strlen()
00012 #endif /* ACE_WIN32 */
00013 
00014 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00015 
00016 inline
00017 ACE_Wide_To_Ascii::~ACE_Wide_To_Ascii (void)
00018 {
00019   delete [] this->s_;
00020 }
00021 
00022 inline char *
00023 ACE_Wide_To_Ascii::char_rep (void)
00024 {
00025   return this->s_;
00026 }
00027 
00028 inline char *
00029 ACE_Wide_To_Ascii::convert (const wchar_t *wstr)
00030 {
00031   // Short circuit null pointer case
00032   if (wstr == 0)
00033     return 0;
00034 
00035 # if defined (ACE_WIN32)
00036   UINT const cp = GetACP ();  // Codepage
00037   int const len = ::WideCharToMultiByte (cp,
00038                                          0,
00039                                          wstr,
00040                                          -1,
00041                                          0,
00042                                          0,
00043                                          0,
00044                                          0);
00045 # elif defined (ACE_LACKS_WCSLEN)
00046   const wchar_t * wtemp = wstr;
00047   while ((*wtemp) != 0) // Hopefully the string is null terminated!
00048     ++wtemp;
00049 
00050   int const len = wtemp - wstr + 1;
00051 # else  /* ACE_WIN32 */
00052   size_t const len = ::wcslen (wstr) + 1;
00053 # endif /* ACE_WIN32 */
00054 
00055 #if !defined (ACE_HAS_ICONV)
00056   char *str = new char[len];
00057 #endif
00058 
00059 # if defined (ACE_WIN32)
00060   ::WideCharToMultiByte (cp, 0, wstr, -1, str, len, 0, 0);
00061 # elif defined (ACE_VXWORKS)
00062   ::wcstombs (str, wstr, len);
00063 # elif defined (ACE_HAS_ICONV)
00064   wchar_t * wstri = const_cast<wchar_t*> (wstr);
00065   size_t lensi = ACE_MAX_ICONV_BUFFER;
00066   size_t lenwi = len * sizeof(wchar_t);
00067   char buf[ACE_MAX_ICONV_BUFFER];
00068   char *stri = buf;
00069 
00070   size_t hr = iconv (ACE_Wide_To_Ascii_iconv_env, (char**)&wstri, &lenwi, &stri, &lensi);
00071   if ((hr==size_t(-1))||(lensi==ACE_MAX_ICONV_BUFFER))
00072     {
00073       char *str=new char[len];
00074       for (size_t i = 0; i < len; i++)
00075         {
00076           wchar_t *t = const_cast <wchar_t *> (wstr);
00077           str[i] = static_cast<char> (*(t + i));
00078         }
00079 
00080       return str;
00081     }
00082   char *str = new char[ACE_MAX_ICONV_BUFFER-lensi];
00083   ::memcpy(str, buf, ACE_MAX_ICONV_BUFFER-lensi);
00084 # else /* ACE_HAS_ICONV */
00085   for (size_t i = 0; i < len; ++i)
00086     {
00087       wchar_t *t = const_cast <wchar_t *> (wstr);
00088       str[i] = static_cast<char> (*(t + i));
00089     }
00090 # endif /* ACE_WIN32 */
00091   return str;
00092 }
00093 
00094 inline
00095 ACE_Wide_To_Ascii::ACE_Wide_To_Ascii (const wchar_t *s)
00096 {
00097 #if defined(ACE_HAS_ICONV)
00098   if (ACE_Wide_To_Ascii_iconv_env == 0)
00099     {
00100       ACE_Wide_To_Ascii_iconv_env = iconv_open("", "WCHAR_T");
00101     }
00102 #endif
00103   s_ = ACE_Wide_To_Ascii::convert (s);
00104 }
00105 
00106 inline
00107 ACE_Ascii_To_Wide::~ACE_Ascii_To_Wide (void)
00108 {
00109   delete [] this->s_;
00110 }
00111 
00112 inline wchar_t *
00113 ACE_Ascii_To_Wide::wchar_rep (void)
00114 {
00115   return this->s_;
00116 }
00117 
00118 inline wchar_t *
00119 ACE_Ascii_To_Wide::convert (const char *str)
00120 {
00121   // Short circuit null pointer case
00122   if (str == 0)
00123     return 0;
00124 
00125 # if defined (ACE_WIN32)
00126   UINT const cp = GetACP ();  // Codepage
00127   int const len = ::MultiByteToWideChar (cp, 0, str, -1, 0, 0);
00128 # else /* ACE_WIN32 */
00129   size_t const len = strlen (str) + 1;
00130 # endif /* ACE_WIN32 */
00131 
00132 #if !defined (ACE_HAS_ICONV)
00133   wchar_t *wstr = new wchar_t[len];
00134 #endif
00135 
00136 # if defined (ACE_WIN32)
00137   ::MultiByteToWideChar (cp, 0, str, -1, wstr, len);
00138 # elif defined (ACE_VXWORKS)
00139   ::mbstowcs (wstr, str, len);
00140 # elif defined (ACE_HAS_ICONV) /* ACE_VXWORKS */
00141   char *stri = const_cast<char*>(str);
00142   size_t lensi = len;
00143   size_t lenwi = ACE_MAX_ICONV_BUFFER;
00144   wchar_t buf[ACE_MAX_ICONV_BUFFER/sizeof(wchar_t)];
00145   wchar_t *wstri=buf;
00146 
00147   size_t hr=iconv(ACE_Ascii_To_Wide_iconv_env, &stri, &lensi, (char**)&wstri, &lenwi);
00148   if((hr==size_t(-1))||(lenwi==ACE_MAX_ICONV_BUFFER)){
00149   wchar_t *wstr=new wchar_t[len*sizeof(wchar_t)];
00150   for (size_t i = 0; i < len; i++){
00151     char *t = const_cast<char *> (str);
00152     wstr[i] = static_cast<wchar_t> (*((unsigned char*)t + i));
00153   }
00154 
00155   return wstr;
00156   }
00157   wchar_t *wstr=new wchar_t[(ACE_MAX_ICONV_BUFFER-lenwi)/sizeof(wchar_t)];
00158   ::memcpy(wstr,buf,ACE_MAX_ICONV_BUFFER-lenwi);
00159 # else /* ACE_HAS_ICONV */
00160   for (size_t i = 0; i < len; ++i)
00161     {
00162       char *t = const_cast<char *> (str);
00163       wstr[i] = static_cast<wchar_t> (*((unsigned char*)(t + i)));
00164     }
00165 # endif /* ACE_WIN32 */
00166   return wstr;
00167 }
00168 
00169 inline
00170 ACE_Ascii_To_Wide::ACE_Ascii_To_Wide (const char *s)
00171 {
00172 #if defined(ACE_HAS_ICONV)
00173   if (ACE_Ascii_To_Wide_iconv_env == 0)
00174     {
00175       ACE_Ascii_To_Wide_iconv_env = iconv_open("WCHAR_T", "");
00176     }
00177 #endif
00178   s_ = ACE_Ascii_To_Wide::convert (s);
00179 }
00180 
00181 ACE_END_VERSIONED_NAMESPACE_DECL
00182 
00183 #endif /* ACE_HAS_WCHAR */

Generated on Tue Feb 2 17:18:38 2010 for ACE by  doxygen 1.4.7