2 * Copyright 2001-2007 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.
18 * @file xmltooling/unicode.h
20 * Helper classes and types for manipulating Unicode
23 #ifndef __xmltooling_unicode_h__
24 #define __xmltooling_unicode_h__
26 #include <xmltooling/base.h>
30 #include <xercesc/util/XMLString.hpp>
32 namespace xmltooling {
36 * An STL string type that supports 16-bit Unicode.
37 * Most compilers support this, but various versions of gcc3 do not.
39 typedef std::basic_string<XMLCh> xstring;
43 * Transcodes a 16-bit Unicode string into UTF-8.
45 * @param src the 16-bit string to transcode
46 * @param use_malloc true iff the result should be allocated with malloc, false to use new
47 * @return a UTF-8 string allocated by the Xerces memory manager
49 extern XMLTOOL_API char* toUTF8(const XMLCh* src, bool use_malloc=false);
52 * Transcodes a UTF-8 string into 16-bit Unicode.
54 * @param src the UTF-8 string to transcode
55 * @param use_malloc true iff the result should be allocated with malloc, false to use new
56 * @return a 16-bit Unicode string allocated by the Xerces memory manager
58 extern XMLTOOL_API XMLCh* fromUTF8(const char* src, bool use_malloc=false);
61 * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
63 * @param ostr stream to write to
64 * @param s string to write
65 * @return reference to output stream
67 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
70 * A minimal auto_ptr-like class that can copy or transcode a buffer into
71 * the local code page and free the result automatically.
73 * Needed because a standard auto_ptr would use delete on the resulting
76 class XMLTOOL_API auto_ptr_char
78 MAKE_NONCOPYABLE(auto_ptr_char);
81 * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
82 * @param src the 16-bit string to transcode and wrap
83 * @param trim trims leading/trailing whitespace from the result (defaults to true)
85 auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
86 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
90 * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
91 * @param src the local string to copy and wrap
92 * @param trim trims leading/trailing whitespace from the result (defaults to true)
94 auto_ptr_char(const char* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
95 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
99 * Destructor frees the wrapped buffer using the Xerces memory manager.
102 xercesc::XMLString::release(&m_buf);
106 * Returns the wrapped buffer.
107 * @return a null-terminated local code page string
109 const char* get() const {
114 * Returns the wrapped buffer and transfers ownership of it to the caller.
115 * @return a null-terminated local code page string
118 char* temp=m_buf; m_buf=NULL; return temp;
126 * A minimal auto_ptr-like class that can copy or transcode a buffer into
127 * 16-bit Unicode and free the result automatically.
129 * Needed because a standard auto_ptr would use delete on the resulting
132 class XMLTOOL_API auto_ptr_XMLCh
134 MAKE_NONCOPYABLE(auto_ptr_XMLCh);
137 * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
138 * @param src the local string to transcode and wrap
139 * @param trim trims leading/trailing whitespace from the result (defaults to true)
141 auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
142 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
146 * Constructor copies a 16-bit Unicode string and wraps the result.
147 * @param src the Unicode string to copy and wrap
148 * @param trim trims leading/trailing whitespace from the result (defaults to true)
150 auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
151 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
155 * Destructor frees the wrapped buffer using the Xerces memory manager.
158 xercesc::XMLString::release(&m_buf);
162 * Returns the wrapped buffer.
163 * @return a null-terminated Unicode string
165 const XMLCh* get() const {
170 * Returns the wrapped buffer and transfers ownership of it to the caller.
171 * @return a null-terminated Unicode string
174 XMLCh* temp=m_buf; m_buf=NULL; return temp;
182 * An auto_ptr that uses array delete on its contents.
184 * @param T type of pointer to wrap
186 template <typename T> class auto_arrayptr
190 auto_arrayptr(const auto_arrayptr<T>&);
191 auto_arrayptr<T>& operator=(const auto_arrayptr<T>&);
196 * @param ptr pointer to wrap
198 auto_arrayptr(T* ptr) : m_ptr(ptr) {
202 * Destructor, uses array delete operation on wrapped pointer.
209 * Returns the wrapped pointer.
210 * @return the wrapped pointer
212 const T* get() const {
217 * Returns the wrapped pointer and transfers ownership of it to the caller.
218 * @return the wrapped pointer
221 T* temp=m_ptr; m_ptr=NULL; return temp;
226 #endif /* __xmltooling_unicode_h__ */