Date: Thursday, March 2, 2006 @ 17:23:24
  Author: csaba
    Path: /cvsroot/carob/libmysequoia

   Added: include/Converter.hpp (1.1) src/Converter.cpp (1.1)
Modified: include/CarobCommon.hpp (1.1 -> 1.2) src/Makefile.am (1.8 ->
          1.9)

Converter class implementation and header file. This class will enable the 
conversion between a character set to wstring and vicaversa.


-------------------------+
 include/CarobCommon.hpp |    4 +
 include/Converter.hpp   |  103 +++++++++++++++++++++++++++++++
 src/Converter.cpp       |  152 ++++++++++++++++++++++++++++++++++++++++++++++
 src/Makefile.am         |    3 
 4 files changed, 261 insertions(+), 1 deletion(-)


Index: libmysequoia/include/CarobCommon.hpp
diff -u libmysequoia/include/CarobCommon.hpp:1.1 
libmysequoia/include/CarobCommon.hpp:1.2
--- libmysequoia/include/CarobCommon.hpp:1.1    Wed Jan 11 11:25:37 2006
+++ libmysequoia/include/CarobCommon.hpp        Thu Mar  2 17:23:24 2006
@@ -27,6 +27,7 @@
 
 // carob includes
 #include <ResultSetMetaData.hpp>
+#include <Converter.hpp>
 
 // include log4cxx header files.
 #include <log4cxx/logger.h>
@@ -55,6 +56,9 @@
 
   // logger
   static log4cxx::LoggerPtr logger;
+  
+  // conversion between character sets
+  Converter conv;
 };
 
 #endif /*_CAROBCOMMON_HPP*/
Index: libmysequoia/include/Converter.hpp
diff -u /dev/null libmysequoia/include/Converter.hpp:1.1
--- /dev/null   Thu Mar  2 17:23:24 2006
+++ libmysequoia/include/Converter.hpp  Thu Mar  2 17:23:24 2006
@@ -0,0 +1,103 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Initial developer(s): Csaba Simon, Zsolt Simon
+ * Contributor(s): 
+ */
+
+#ifndef _CONVERTER_HPP
+#define _CONVERTER_HPP
+
+#include <iconv.h>
+#include <string>
+
+const int CONVERTER_BUFFER_SIZE = 1024;
+
+class ConverterException
+{
+public:
+  /**
+   * Constructor.
+   * @param s The error message.
+   */
+  ConverterException(std::string s) : message(s) {};
+
+  /**
+   * Return the error message.
+   * @return a string containing the error message
+   */
+  const std::string&  description() const { return message; }
+
+private:
+  std::string message;
+};
+
+class Converter
+{
+public:
+  /**
+   * Default constructor.
+   */
+  Converter();
+
+  /**
+   * Constructor.
+   * @param code The character code from/to the conversion will be.
+   */
+  Converter(const std::string &code);
+
+  /**
+   * Destructor.
+   */
+  ~Converter();
+
+  /**
+   * Set the character code from/to the conversion will be.
+   * @param code The character code from/to the conversion will be.
+   * @return true if succesfull
+   *         false for invalid character codes.
+   */
+  bool set_code(const std::string &code);
+
+  /**
+   * Convert from wstring to string.
+   * @param w the wstring.
+   * @return a string.
+   */
+  std::string from_wstring(const std::wstring &w);
+
+  /**
+   * Convert from string to wstring.
+   * @param s the string.
+   * @return a wstring.
+   */
+  std::wstring to_wstring(const std::string &s);
+
+private:
+  /**
+   * Close the conversion descriptors
+   */
+  void close();
+
+private:
+  /**
+   * the conversion descriptors
+   */
+  iconv_t cd_from, cd_to;
+};
+
+#endif /*_CONVERTER_HPP*/
Index: libmysequoia/src/Converter.cpp
diff -u /dev/null libmysequoia/src/Converter.cpp:1.1
--- /dev/null   Thu Mar  2 17:23:24 2006
+++ libmysequoia/src/Converter.cpp      Thu Mar  2 17:23:24 2006
@@ -0,0 +1,152 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005-2006 Continuent, Inc.
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Initial developer(s): Zsolt Simon, Csaba Simon
+ * Contributor(s): 
+ */
+
+#include <Converter.hpp>
+
+#include <errno.h>
+
+using namespace std;
+
+Converter::Converter() : cd_from(iconv_t(-1)), cd_to(iconv_t(-1))
+{
+}
+
+Converter::Converter(const string &code) : cd_from(iconv_t(-1)), 
cd_to(iconv_t(-1))
+{
+  if (!set_code(code))
+    throw ConverterException("Missing encodings.");
+}
+
+Converter::~Converter()
+{
+  close();
+}
+
+bool
+Converter::set_code(const string &code)
+{
+  close();
+  cd_from = iconv_open(code.c_str(), "WCHAR_T");
+  cd_to = iconv_open("WCHAR_T", code.c_str());
+  return ((cd_from != iconv_t(-1)) && (cd_to != iconv_t(-1)));
+}
+
+void
+Converter::close()
+{
+  if (cd_from != iconv_t(-1))
+  {
+    iconv_close(cd_from);
+    cd_from = iconv_t(-1);
+  }
+
+  if (cd_to != iconv_t(-1))
+  {
+    iconv_close(cd_to);
+    cd_to = iconv_t(-1);
+  }
+}
+
+std::wstring
+Converter::to_wstring(const std::string &s)
+{
+  if (s.empty())
+    return std::wstring();
+
+  std::wstring result;
+
+  char *inbuf = (char *)s.data();
+  size_t inbytesleft = s.length();
+  
+  wchar_t convertbuf[CONVERTER_BUFFER_SIZE];
+  char *outbuf;
+  size_t outbytesleft;
+  
+  while (inbytesleft > 0)
+  {
+    outbuf = (char *)convertbuf;
+    outbytesleft = sizeof(convertbuf) - sizeof(convertbuf[0]);
+    
+    size_t ret = iconv(cd_to, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+    if (ret == size_t(-1))
+    {
+      switch(errno)
+      {
+        case E2BIG:
+          break;
+        case EILSEQ:
+          throw ConverterException("An invalid multibyte sequence has been 
encountered in the input.");
+          break;
+        case EINVAL:
+        default:
+          throw ConverterException("An incomplete multibyte sequence has been 
encountered in the input.");
+          break;
+      }
+    }
+    *((wchar_t *)outbuf) = L'\0';
+    result += convertbuf;
+  }
+  
+  return result;
+}
+
+std::string
+Converter::from_wstring(const std::wstring &w)
+{
+  if (w.empty())
+    return std::string();
+  
+  std::string result;
+
+  char *inbuf = (char *)w.data();
+  size_t inbytesleft = w.length() * sizeof(wchar_t);
+  
+  char convertbuf[CONVERTER_BUFFER_SIZE];
+  char *outbuf;
+  size_t outbytesleft;
+  
+  while (inbytesleft > 0)
+  {
+    outbuf = convertbuf;
+    outbytesleft = sizeof(convertbuf) - sizeof(convertbuf[0]);
+    
+    size_t ret = iconv(cd_from, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
+    if (ret == size_t(-1))
+    {
+      switch(errno)
+      {
+        case E2BIG:
+          break;
+        case EILSEQ:
+          throw ConverterException("An invalid multibyte sequence has been 
encountered in the input.");
+          break;
+        case EINVAL:
+        default:
+          throw ConverterException("An incomplete multibyte sequence has been 
encountered in the input.");
+          break;
+      }
+    }
+    *outbuf = '\0';
+    result += convertbuf;
+  }
+  
+  return result;  
+}
Index: libmysequoia/src/Makefile.am
diff -u libmysequoia/src/Makefile.am:1.8 libmysequoia/src/Makefile.am:1.9
--- libmysequoia/src/Makefile.am:1.8    Mon Feb 13 13:26:25 2006
+++ libmysequoia/src/Makefile.am        Thu Mar  2 17:23:24 2006
@@ -27,7 +27,8 @@
                            CarobStmt.cpp \
                            MySQLAPI.cpp \
                            Utils.cpp \
-                           IniParser.cpp
+                           IniParser.cpp \
+                           Converter.cpp
 
 libmysequoia_la_CXXFLAGS = @MYSQL_CFLAGS@ @GCOV_CFLAGS@
 libmysequoia_la_LDFLAGS  = -version-info 0:0:0 @GCOV_LDADD@ @LOG4CXX_LDADD@

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

Reply via email to