Add xsi:nil support to objects.
[shibboleth/xmltooling.git] / xmltooling / unicode.h
index 163b8d3..6cc8763 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
  */
 
 /**
- * @file unicode.h
+ * @file xmltooling/unicode.h
  * 
  * Helper classes and types for manipulating Unicode
  */
@@ -29,8 +29,6 @@
 #include <iostream>
 #include <xercesc/util/XMLString.hpp>
 
-using namespace xercesc;
-
 namespace xmltooling {
     
     #ifdef HAVE_GOOD_STL
@@ -43,17 +41,21 @@ namespace xmltooling {
 
     /**
      * Transcodes a 16-bit Unicode string into UTF-8.
-     * @param src   the 16-bit string to transcode
+     * 
+     * @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 
      */
-    extern XMLTOOL_API char* toUTF8(const XMLCh* src);
+    extern XMLTOOL_API char* toUTF8(const XMLCh* src, bool use_malloc=false);
 
     /**
      * Transcodes a UTF-8 string into 16-bit Unicode.
-     * @param src   the UTF-8 string to transcode
+     * 
+     * @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 
      */
-    extern XMLTOOL_API XMLCh* fromUTF8(const char* src);
+    extern XMLTOOL_API XMLCh* fromUTF8(const char* src, bool use_malloc=false);
 
     /**
      * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
@@ -73,41 +75,51 @@ namespace xmltooling {
      */
     class XMLTOOL_API auto_ptr_char
     {
+        MAKE_NONCOPYABLE(auto_ptr_char);
     public:
         /**
          * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
          * @param src   the 16-bit string to transcode and wrap
          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
          */
-        auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
+        auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
+            if (trim && m_buf) xercesc::XMLString::trim(m_buf);
+        }
 
         /**
          * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
          * @param src   the local string to copy and wrap
          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
          */
-        auto_ptr_char(const char* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
+        auto_ptr_char(const char* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
+            if (trim && m_buf) xercesc::XMLString::trim(m_buf);
+        }
 
         /**
          * Destructor frees the wrapped buffer using the Xerces memory manager.
          */
-        ~auto_ptr_char() { XMLString::release(&m_buf); }
+        ~auto_ptr_char() {
+            xercesc::XMLString::release(&m_buf);
+        }
 
         /**
          * Returns the wrapped buffer.
          * @return a null-terminated local code page string
          */
-        const char* get() const { return m_buf; }
+        const char* get() const {
+            return m_buf;
+        }
 
         /**
          * Returns the wrapped buffer and transfers ownership of it to the caller.
          * @return a null-terminated local code page string
          */
-        char* release() { char* temp=m_buf; m_buf=NULL; return temp; }
+        char* release() {
+            char* temp=m_buf; m_buf=NULL; return temp;
+        }
 
     private:    
         char* m_buf;
-    MAKE_NONCOPYABLE(auto_ptr_char);
     };
 
     /**
@@ -119,43 +131,96 @@ namespace xmltooling {
      */
     class XMLTOOL_API auto_ptr_XMLCh
     {
+        MAKE_NONCOPYABLE(auto_ptr_XMLCh);
     public:
         /**
          * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
          * @param src   the local string to transcode and wrap
          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
          */
-        auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
+        auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
+            if (trim && m_buf) xercesc::XMLString::trim(m_buf);
+        }
 
         /**
          * Constructor copies a 16-bit Unicode string and wraps the result.
          * @param src   the Unicode string to copy and wrap
          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
          */
-        auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
+        auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
+            if (trim && m_buf) xercesc::XMLString::trim(m_buf);
+        }
 
         /**
          * Destructor frees the wrapped buffer using the Xerces memory manager.
          */
-        ~auto_ptr_XMLCh() { XMLString::release(&m_buf); }
+        ~auto_ptr_XMLCh() {
+            xercesc::XMLString::release(&m_buf);
+        }
 
         /**
          * Returns the wrapped buffer.
          * @return a null-terminated Unicode string
          */
-        const XMLCh* get() const { return m_buf; }
+        const XMLCh* get() const {
+            return m_buf;
+        }
         
         /**
          * Returns the wrapped buffer and transfers ownership of it to the caller.
          * @return a null-terminated Unicode string
          */
-        XMLCh* release() { XMLCh* temp=m_buf; m_buf=NULL; return temp; }
+        XMLCh* release() {
+            XMLCh* temp=m_buf; m_buf=NULL; return temp;
+        }
 
     private:
         XMLCh* m_buf;
-    MAKE_NONCOPYABLE(auto_ptr_XMLCh);
     };
 
+    /**
+     * An auto_ptr that uses array delete on its contents.
+     *
+     * @param T type of pointer to wrap
+     */
+    template <typename T> class auto_arrayptr
+    {
+        T* m_ptr;
+
+        auto_arrayptr(const auto_arrayptr<T>&);
+        auto_arrayptr<T>& operator=(const auto_arrayptr<T>&);
+    public:
+        /**
+         * Constructor.
+         *
+         * @param ptr pointer to wrap
+         */
+        auto_arrayptr(T* ptr) : m_ptr(ptr) {
+        }
+
+        /**
+         * Destructor, uses array delete operation on wrapped pointer.
+         */
+        ~auto_arrayptr() {
+            delete[] m_ptr;
+        }
+
+        /**
+         * Returns the wrapped pointer.
+         * @return the wrapped pointer
+         */
+        const T* get() const {
+            return m_ptr;
+        }
+
+        /**
+         * Returns the wrapped pointer and transfers ownership of it to the caller.
+         * @return the wrapped pointer
+         */
+        T* release() {
+            T* temp=m_ptr; m_ptr=NULL; return temp;
+        }
+    };
 };
 
 #endif /* __xmltooling_unicode_h__ */