CPPXT-104 - Add exception handling to integer conversions
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.h
index 06f79ee..eb842c4 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2009 Internet2
+/**
+ * Licensed to the University Corporation for Advanced Internet
+ * Development, Inc. (UCAID) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for
+ * additional information regarding copyright ownership.
  *
- * 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
+ * UCAID licenses this file to you 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
+ * 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.
+ * 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.
  */
 
 /**
 
 #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.
      *
      * @param src           the 16-bit string to transcode
      * @param use_malloc    true iff the result should be allocated with malloc, false to use new
-     * @return      a UTF-8 string allocated by the Xerces memory manager
+     * @return      a UTF-8 string allocated by new or malloc
      */
     extern XMLTOOL_API char* toUTF8(const XMLCh* src, bool use_malloc=false);
 
@@ -53,7 +65,7 @@ namespace xmltooling {
      *
      * @param src           the UTF-8 string to transcode
      * @param use_malloc    true iff the result should be allocated with malloc, false to use new
-     * @return      a 16-bit Unicode string allocated by the Xerces memory manager
+     * @return      a 16-bit Unicode string allocated by new or malloc
      */
     extern XMLTOOL_API XMLCh* fromUTF8(const char* src, bool use_malloc=false);
 
@@ -67,6 +79,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.
      *
@@ -80,7 +101,7 @@ namespace xmltooling {
         /**
          * Default constructor.
          */
-        auto_ptr_char() : m_buf(NULL) {
+        auto_ptr_char() : m_buf(nullptr) {
         }
 
         /**
@@ -121,7 +142,7 @@ namespace xmltooling {
          * @return a null-terminated local code page string
          */
         char* release() {
-            char* temp=m_buf; m_buf=NULL; return temp;
+            char* temp=m_buf; m_buf=nullptr; return temp;
         }
 
     private:
@@ -142,7 +163,7 @@ namespace xmltooling {
         /**
          * Default constructor.
          */
-        auto_ptr_XMLCh() : m_buf(NULL) {
+        auto_ptr_XMLCh() : m_buf(nullptr) {
         }
 
         /**
@@ -183,7 +204,7 @@ namespace xmltooling {
          * @return a null-terminated Unicode string
          */
         XMLCh* release() {
-            XMLCh* temp=m_buf; m_buf=NULL; return temp;
+            XMLCh* temp=m_buf; m_buf=nullptr; return temp;
         }
 
     private:
@@ -230,7 +251,7 @@ namespace xmltooling {
          * @return the wrapped pointer
          */
         T* release() {
-            T* temp=m_ptr; m_ptr=NULL; return temp;
+            T* temp=m_ptr; m_ptr=nullptr; return temp;
         }
     };
 };