2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * @file xmltooling/unicode.h
24 * Helper classes and types for manipulating Unicode
27 #ifndef __xmltooling_unicode_h__
28 #define __xmltooling_unicode_h__
30 #include <xmltooling/base.h>
33 # include <xmltooling/char_traits.h>
38 #include <xercesc/util/XMLString.hpp>
40 namespace xmltooling {
44 * An STL string type that supports 16-bit Unicode.
46 typedef std::basic_string<XMLCh> xstring;
49 * An STL string type that supports 16-bit Unicode.
51 typedef std::basic_string< XMLCh,char_traits<XMLCh> > xstring;
55 * Transcodes a 16-bit Unicode string into UTF-8.
57 * @param src the 16-bit string to transcode
58 * @param use_malloc true iff the result should be allocated with malloc, false to use new
59 * @return a UTF-8 string allocated by the Xerces memory manager
61 extern XMLTOOL_API char* toUTF8(const XMLCh* src, bool use_malloc=false);
64 * Transcodes a UTF-8 string into 16-bit Unicode.
66 * @param src the UTF-8 string to transcode
67 * @param use_malloc true iff the result should be allocated with malloc, false to use new
68 * @return a 16-bit Unicode string allocated by the Xerces memory manager
70 extern XMLTOOL_API XMLCh* fromUTF8(const char* src, bool use_malloc=false);
73 * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
75 * @param ostr stream to write to
76 * @param s string to write
77 * @return reference to output stream
79 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
82 * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
84 * @param ostr stream to write to
85 * @param s string to write
86 * @return reference to output stream
88 extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xstring& s);
91 * A minimal auto_ptr-like class that can copy or transcode a buffer into
92 * the local code page and free the result automatically.
94 * Needed because a standard auto_ptr would use delete on the resulting
97 class XMLTOOL_API auto_ptr_char
99 MAKE_NONCOPYABLE(auto_ptr_char);
102 * Default constructor.
104 auto_ptr_char() : m_buf(nullptr) {
108 * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
109 * @param src the 16-bit string to transcode and wrap
110 * @param trim trims leading/trailing whitespace from the result (defaults to true)
112 auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
113 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
117 * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
118 * @param src the local string to copy and wrap
119 * @param trim trims leading/trailing whitespace from the result (defaults to true)
121 auto_ptr_char(const char* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
122 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
126 * Destructor frees the wrapped buffer using the Xerces memory manager.
129 xercesc::XMLString::release(&m_buf);
133 * Returns the wrapped buffer.
134 * @return a null-terminated local code page string
136 const char* get() const {
141 * Returns the wrapped buffer and transfers ownership of it to the caller.
142 * @return a null-terminated local code page string
145 char* temp=m_buf; m_buf=nullptr; return temp;
153 * A minimal auto_ptr-like class that can copy or transcode a buffer into
154 * 16-bit Unicode and free the result automatically.
156 * Needed because a standard auto_ptr would use delete on the resulting
159 class XMLTOOL_API auto_ptr_XMLCh
161 MAKE_NONCOPYABLE(auto_ptr_XMLCh);
164 * Default constructor.
166 auto_ptr_XMLCh() : m_buf(nullptr) {
170 * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
171 * @param src the local string to transcode and wrap
172 * @param trim trims leading/trailing whitespace from the result (defaults to true)
174 auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
175 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
179 * Constructor copies a 16-bit Unicode string and wraps the result.
180 * @param src the Unicode string to copy and wrap
181 * @param trim trims leading/trailing whitespace from the result (defaults to true)
183 auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
184 if (trim && m_buf) xercesc::XMLString::trim(m_buf);
188 * Destructor frees the wrapped buffer using the Xerces memory manager.
191 xercesc::XMLString::release(&m_buf);
195 * Returns the wrapped buffer.
196 * @return a null-terminated Unicode string
198 const XMLCh* get() const {
203 * Returns the wrapped buffer and transfers ownership of it to the caller.
204 * @return a null-terminated Unicode string
207 XMLCh* temp=m_buf; m_buf=nullptr; return temp;
215 * An auto_ptr that uses array delete on its contents.
217 * @param T type of pointer to wrap
219 template <typename T> class auto_arrayptr
223 auto_arrayptr(const auto_arrayptr<T>&);
224 auto_arrayptr<T>& operator=(const auto_arrayptr<T>&);
229 * @param ptr pointer to wrap
231 auto_arrayptr(T* ptr) : m_ptr(ptr) {
235 * Destructor, uses array delete operation on wrapped pointer.
242 * Returns the wrapped pointer.
243 * @return the wrapped pointer
245 const T* get() const {
250 * Returns the wrapped pointer and transfers ownership of it to the caller.
251 * @return the wrapped pointer
254 T* temp=m_ptr; m_ptr=nullptr; return temp;
259 #endif /* __xmltooling_unicode_h__ */