2 * Copyright 2001-2006 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 * Helper classes and types for manipulating Unicode
23 #ifndef __xmltooling_unicode_h__
24 #define __xmltooling_unicode_h__
26 #include <xmltooling/base.h>
29 #include <xercesc/util/XMLString.hpp>
31 using namespace xercesc;
33 namespace xmltooling {
37 * An STL string type that supports 16-bit Unicode.
38 * Most compilers support this, but various versions of gcc3 do not.
40 typedef std::basic_string<XMLCh> xstring;
44 * Transcodes a 16-bit Unicode string into UTF-8.
45 * @param src the 16-bit string to transcode
46 * @return a UTF-8 string allocated by the Xerces memory manager
48 extern XMLTOOL_API char* toUTF8(const XMLCh* src);
51 * Transcodes a UTF-8 string into 16-bit Unicode.
52 * @param src the UTF-8 string to transcode
53 * @return a 16-bit Unicode string allocated by the Xerces memory manager
55 extern XMLTOOL_API XMLCh* fromUTF8(const char* src);
58 * A minimal auto_ptr-like class that can copy or transcode a buffer into
59 * the local code page and free the result automatically.
61 * Needed because a standard auto_ptr would use delete on the resulting
64 class XMLTOOL_API auto_ptr_char
68 * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
69 * @param src the 16-bit string to transcode and wrap
70 * @param trim trims leading/trailing whitespace from the result (defaults to true)
72 auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
75 * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
76 * @param src the local string to copy and wrap
77 * @param trim trims leading/trailing whitespace from the result (defaults to true)
79 auto_ptr_char(const char* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
82 * Destructor frees the wrapped buffer using the Xerces memory manager.
84 ~auto_ptr_char() { XMLString::release(&m_buf); }
87 * Returns the wrapped buffer.
88 * @return a null-terminated local code page string
90 const char* get() const { return m_buf; }
93 * Returns the wrapped buffer and transfers ownership of it to the caller.
94 * @return a null-terminated local code page string
96 char* release() { char* temp=m_buf; m_buf=NULL; return temp; }
100 MAKE_NONCOPYABLE(auto_ptr_char);
104 * A minimal auto_ptr-like class that can copy or transcode a buffer into
105 * 16-bit Unicode and free the result automatically.
107 * Needed because a standard auto_ptr would use delete on the resulting
110 class XMLTOOL_API auto_ptr_XMLCh
114 * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
115 * @param src the local string to transcode and wrap
116 * @param trim trims leading/trailing whitespace from the result (defaults to true)
118 auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
121 * Constructor copies a 16-bit Unicode string and wraps the result.
122 * @param src the Unicode string to copy and wrap
123 * @param trim trims leading/trailing whitespace from the result (defaults to true)
125 auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
128 * Destructor frees the wrapped buffer using the Xerces memory manager.
130 ~auto_ptr_XMLCh() { XMLString::release(&m_buf); }
133 * Returns the wrapped buffer.
134 * @return a null-terminated Unicode string
136 const XMLCh* get() const { return m_buf; }
139 * Returns the wrapped buffer and transfers ownership of it to the caller.
140 * @return a null-terminated Unicode string
142 XMLCh* release() { XMLCh* temp=m_buf; m_buf=NULL; return temp; }
146 MAKE_NONCOPYABLE(auto_ptr_XMLCh);
151 #endif /* __xmltooling_unicode_h__ */