Add char_traits implementation for XMLCh string specialization on strict compilers.
authorScott Cantor <cantor.2@osu.edu>
Wed, 18 Mar 2009 20:29:35 +0000 (20:29 +0000)
committerScott Cantor <cantor.2@osu.edu>
Wed, 18 Mar 2009 20:29:35 +0000 (20:29 +0000)
xmltooling/Makefile.am
xmltooling/char_traits.h [new file with mode: 0644]
xmltooling/unicode.cpp
xmltooling/unicode.h

index 15e4378..52ab907 100644 (file)
@@ -33,6 +33,7 @@ libxmltoolinginclude_HEADERS = \
        AttributeExtensibleXMLObject.h \
        base.h \
        ConcreteXMLObjectBuilder.h \
+       char_traits.h \
        config_pub.h \
        ElementExtensibleXMLObject.h \
        ElementProxy.h \
diff --git a/xmltooling/char_traits.h b/xmltooling/char_traits.h
new file mode 100644 (file)
index 0000000..fac30bb
--- /dev/null
@@ -0,0 +1,170 @@
+/*
+ *  Copyright 2001-2009 Internet2
+ *
+ * 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.
+ */
+
+/**
+ * @file xmltooling/char_traits.h
+ *
+ * Traits template for basic_string<unsigned short> instantiation on strict compilers.
+ */
+
+#ifndef __xmltooling_chartraits_h__
+#define __xmltooling_chartraits_h__
+
+#include <xmltooling/base.h>
+
+#include <cstring>
+#include <algorithm>
+
+namespace xmltooling {
+
+  /// @cond OFF
+
+  template <class _CharT>
+    struct _Char_types
+    {
+      typedef unsigned long   int_type;
+      typedef std::streampos  pos_type;
+      typedef std::streamoff  off_type;
+      typedef std::mbstate_t  state_type;
+    };
+
+
+  template<typename _CharT>
+    struct char_traits
+    {
+      typedef _CharT                                    char_type;
+      typedef typename _Char_types<_CharT>::int_type    int_type;
+      typedef typename _Char_types<_CharT>::pos_type    pos_type;
+      typedef typename _Char_types<_CharT>::off_type    off_type;
+      typedef typename _Char_types<_CharT>::state_type  state_type;
+
+      static void
+      assign(char_type& __c1, const char_type& __c2)
+      { __c1 = __c2; }
+
+      static bool
+      eq(const char_type& __c1, const char_type& __c2)
+      { return __c1 == __c2; }
+
+      static bool
+      lt(const char_type& __c1, const char_type& __c2)
+      { return __c1 < __c2; }
+
+      static int
+      compare(const char_type* __s1, const char_type* __s2, std::size_t __n);
+
+      static std::size_t
+      length(const char_type* __s);
+
+      static const char_type*
+      find(const char_type* __s, std::size_t __n, const char_type& __a);
+
+      static char_type*
+      move(char_type* __s1, const char_type* __s2, std::size_t __n);
+
+      static char_type*
+      copy(char_type* __s1, const char_type* __s2, std::size_t __n);
+
+      static char_type*
+      assign(char_type* __s, std::size_t __n, char_type __a);
+
+      static char_type
+      to_char_type(const int_type& __c)
+      { return static_cast<char_type>(__c); }
+
+      static int_type
+      to_int_type(const char_type& __c)
+      { return static_cast<int_type>(__c); }
+
+      static bool
+      eq_int_type(const int_type& __c1, const int_type& __c2)
+      { return __c1 == __c2; }
+
+      static int_type
+      eof()
+      { return static_cast<int_type>(EOF); }
+
+      static int_type
+      not_eof(const int_type& __c)
+      { return !eq_int_type(__c, eof()) ? __c : to_int_type(char_type()); }
+    };
+
+  template<typename _CharT>
+    int
+    char_traits<_CharT>::
+    compare(const char_type* __s1, const char_type* __s2, std::size_t __n)
+    {
+      for (size_t __i = 0; __i < __n; ++__i)
+    if (lt(__s1[__i], __s2[__i]))
+      return -1;
+    else if (lt(__s2[__i], __s1[__i]))
+      return 1;
+      return 0;
+    }
+
+  template<typename _CharT>
+    std::size_t
+    char_traits<_CharT>::
+    length(const char_type* __p)
+    {
+      std::size_t __i = 0;
+      while (!eq(__p[__i], char_type()))
+        ++__i;
+      return __i;
+    }
+
+  template<typename _CharT>
+    const typename char_traits<_CharT>::char_type*
+    char_traits<_CharT>::
+    find(const char_type* __s, std::size_t __n, const char_type& __a)
+    {
+      for (std::size_t __i = 0; __i < __n; ++__i)
+        if (eq(__s[__i], __a))
+          return __s + __i;
+      return 0;
+    }
+
+  template<typename _CharT>
+    typename char_traits<_CharT>::char_type*
+    char_traits<_CharT>::
+    move(char_type* __s1, const char_type* __s2, std::size_t __n)
+    {
+      return static_cast<_CharT*>(std::memmove(__s1, __s2,
+                           __n * sizeof(char_type)));
+    }
+
+  template<typename _CharT>
+    typename char_traits<_CharT>::char_type*
+    char_traits<_CharT>::
+    copy(char_type* __s1, const char_type* __s2, std::size_t __n)
+    {
+      std::copy(__s2, __s2 + __n, __s1);
+      return __s1;
+    }
+
+  template<typename _CharT>
+    typename char_traits<_CharT>::char_type*
+    char_traits<_CharT>::
+    assign(char_type* __s, std::size_t __n, char_type __a)
+    {
+      std::fill_n(__s, __n, __a);
+      return __s;
+    }
+
+    /// @endcond
+}
+
+#endif // __xmltooling_chartraits_h__
index dc0774c..1e50c9c 100644 (file)
@@ -88,3 +88,8 @@ std::ostream& xmltooling::operator<<(std::ostream& ostr, const XMLCh* s)
     }
     return ostr;
 }
+
+std::ostream& xmltooling::operator<<(std::ostream& ostr, const xstring& s)
+{
+    return ostr << s.c_str();
+}
index 06f79ee..1076abc 100644 (file)
 
 #include <xmltooling/base.h>
 
+#ifndef HAVE_GOOD_STL
+# include <xmltooling/char_traits.h>
+#endif
+
 #include <string>
 #include <iostream>
 #include <xercesc/util/XMLString.hpp>
 
 namespace xmltooling {
 
-    #ifdef HAVE_GOOD_STL
+#ifdef HAVE_GOOD_STL
         /**
          * An STL string type that supports 16-bit Unicode.
-         * Most compilers support this, but various versions of gcc3 do not.
          */
         typedef std::basic_string<XMLCh> xstring;
-    #endif
+#else
+        /**
+         * An STL string type that supports 16-bit Unicode.
+         */
+        typedef std::basic_string< XMLCh,char_traits<XMLCh> > xstring;
+#endif
 
     /**
      * Transcodes a 16-bit Unicode string into UTF-8.
@@ -67,6 +75,15 @@ namespace xmltooling {
     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
 
     /**
+     * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
+     *
+     * @param ostr  stream to write to
+     * @param s     string to write
+     * @return      reference to output stream
+     */
+    extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xstring& s);
+
+    /**
      * A minimal auto_ptr-like class that can copy or transcode a buffer into
      * the local code page and free the result automatically.
      *