Date: Monday, December 19, 2005 @ 16:48:46
  Author: marc
    Path: /cvsroot/carob/carob

Modified: include/Common.hpp (1.23 -> 1.24) src/Common.cpp (1.16 -> 1.17)

Ditch old C-code in toString() and fromString(); now using C++ Codecs. Related 
to CAROB-49


--------------------+
 include/Common.hpp |   15 +++++++++++---
 src/Common.cpp     |   52 ---------------------------------------------------
 2 files changed, 13 insertions(+), 54 deletions(-)


Index: carob/include/Common.hpp
diff -u carob/include/Common.hpp:1.23 carob/include/Common.hpp:1.24
--- carob/include/Common.hpp:1.23       Mon Dec 19 16:33:33 2005
+++ carob/include/Common.hpp    Mon Dec 19 16:48:46 2005
@@ -130,6 +130,10 @@
   { return utf8_codec.decode(in); }
   static std::string toUTF8(const std::wstring& in) throw (CodecException)
   { return utf8_codec.encode(in); }
+  static std::wstring fromString(const std::string& in) throw (CodecException)
+  { return user_codec.decode(in); }
+  static std::string toString(const std::wstring& in) throw (CodecException)
+  { return user_codec.encode(in); }
 private:
   static const CarobNS::MBSCodec utf8_codec;
   // user-defined locale (typically set using LANG, LC_etc)
@@ -138,18 +142,23 @@
 
 /**
  * Default (locale) String to Wide-String conversion.
- * FIXME: throw exception on error
  * @param in original string
  * @return converted wstring
  */
-std::wstring fromString(const std::string& in);
+inline std::wstring fromString(const std::string& in) throw (CodecException)
+{
+  return StaticCodecs::fromString(in);
+}
 /**
  * Wide-String to default (locale) String conversion.
  * FIXME: throw exception on error
  * @param in original wide string
  * @return converted string
  */
-std::string toString(const std::wstring& in);
+inline std::string toString(const std::wstring& in) throw (CodecException)
+{
+  return StaticCodecs::toString(in);
+}
 
 inline std::wstring fromUTF8(const std::string& in) throw (CodecException)
 {
Index: carob/src/Common.cpp
diff -u carob/src/Common.cpp:1.16 carob/src/Common.cpp:1.17
--- carob/src/Common.cpp:1.16   Mon Dec 19 15:06:49 2005
+++ carob/src/Common.cpp        Mon Dec 19 16:48:46 2005
@@ -110,58 +110,8 @@
 }
 
 const CarobNS::MBSCodec 
CarobNS::StaticCodecs::utf8_codec(trylocale(NAME_OF_ANY_UTF8_LOCALE_AVAILABLE));
+const CarobNS::MBSCodec CarobNS::StaticCodecs::user_codec;
 
-wstring CarobNS::fromString(const string& in)
-{
-  bool ret = true;
-  const char* inarray = in.c_str();
-  // cater for the worst case (pure ASCII), plus the trailing zero
-  unsigned destWArrayLen = in.length() + 1;
-  wchar_t* destwarray = new wchar_t[destWArrayLen];
-  
-  mbstate_t    mbstate = {0};
-  
-  // FIXME: this C function considers with NULL-terminated strings
-  // replace this code by a codecvt
-  size_t countConverted = mbsrtowcs(destwarray, &inarray, destWArrayLen, 
&mbstate);
-  if (countConverted == (size_t)-1 || inarray != NULL)
-  {
-    ret = false;
-  }
-  else  
-  {
-    ; // out = destwarray;
-  }
-  wstring wsret(destwarray);
-  delete[] destwarray;
-  return wsret;
-  // return ret; FIXME throws instead
-}
-
-string CarobNS::toString(const wstring& in)
-{
-  bool ret = true;
-  const wchar_t* inwarray = in.c_str();
-  unsigned destMBArrayLen = MB_CUR_MAX * in.length() + 1;
-  char* destarray = new char[destMBArrayLen];
-  mbstate_t mbstate = {0};
-  
-  // FIXME: this C function considers with NULL-terminated strings
-  // replace this code by a codecvt
-  size_t countConverted = wcsrtombs(destarray, &inwarray, destMBArrayLen, 
&mbstate);
-  if (countConverted == (size_t)-1 || inwarray != NULL)
-  {
-    ret = false;
-  }
-  else  
-  {
-    ; // out = destarray;
-  }
-  string sret(destarray);
-  delete[] destarray;
-  return sret;
-  // return ret; FIXME throws instead
-}
 
 wstring CarobNS::toWString(const int& arg)
 {

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to