Set fourth file version digit to signify rebuild.
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.h
1 /**
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.
6  *
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
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 /**
22  * @file xmltooling/unicode.h
23  *
24  * Helper classes and types for manipulating Unicode
25  */
26
27 #ifndef __xmltooling_unicode_h__
28 #define __xmltooling_unicode_h__
29
30 #include <xmltooling/base.h>
31
32 #ifndef HAVE_GOOD_STL
33 # include <xmltooling/char_traits.h>
34 #endif
35
36 #include <string>
37 #include <iostream>
38 #include <xercesc/util/XMLString.hpp>
39
40 namespace xmltooling {
41
42 #ifdef HAVE_GOOD_STL
43         /**
44          * An STL string type that supports 16-bit Unicode.
45          */
46         typedef std::basic_string<XMLCh> xstring;
47 #else
48         /**
49          * An STL string type that supports 16-bit Unicode.
50          */
51         typedef std::basic_string< XMLCh,char_traits<XMLCh> > xstring;
52 #endif
53
54     /**
55      * Transcodes a 16-bit Unicode string into UTF-8.
56      *
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 new or malloc
60      */
61     extern XMLTOOL_API char* toUTF8(const XMLCh* src, bool use_malloc=false);
62
63     /**
64      * Transcodes a UTF-8 string into 16-bit Unicode.
65      *
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 new or malloc
69      */
70     extern XMLTOOL_API XMLCh* fromUTF8(const char* src, bool use_malloc=false);
71
72     /**
73      * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
74      *
75      * @param ostr  stream to write to
76      * @param s     string to write
77      * @return      reference to output stream
78      */
79     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
80
81     /**
82      * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
83      *
84      * @param ostr  stream to write to
85      * @param s     string to write
86      * @return      reference to output stream
87      */
88     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const xstring& s);
89
90     /**
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.
93      *
94      * Needed because a standard auto_ptr would use delete on the resulting
95      * pointer.
96      */
97     class XMLTOOL_API auto_ptr_char
98     {
99         MAKE_NONCOPYABLE(auto_ptr_char);
100     public:
101         /**
102          * Default constructor.
103          */
104         auto_ptr_char() : m_buf(nullptr) {
105         }
106
107         /**
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)
111          */
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);
114         }
115
116         /**
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)
120          */
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);
123         }
124
125         /**
126          * Destructor frees the wrapped buffer using the Xerces memory manager.
127          */
128         ~auto_ptr_char() {
129             xercesc::XMLString::release(&m_buf);
130         }
131
132         /**
133          * Returns the wrapped buffer.
134          * @return a null-terminated local code page string
135          */
136         const char* get() const {
137             return m_buf;
138         }
139
140         /**
141          * Returns the wrapped buffer and transfers ownership of it to the caller.
142          * @return a null-terminated local code page string
143          */
144         char* release() {
145             char* temp=m_buf; m_buf=nullptr; return temp;
146         }
147
148     private:
149         char* m_buf;
150     };
151
152     /**
153      * A minimal auto_ptr-like class that can copy or transcode a buffer into
154      * 16-bit Unicode and free the result automatically.
155      *
156      * Needed because a standard auto_ptr would use delete on the resulting
157      * pointer.
158      */
159     class XMLTOOL_API auto_ptr_XMLCh
160     {
161         MAKE_NONCOPYABLE(auto_ptr_XMLCh);
162     public:
163         /**
164          * Default constructor.
165          */
166         auto_ptr_XMLCh() : m_buf(nullptr) {
167         }
168
169         /**
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)
173          */
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);
176         }
177
178         /**
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)
182          */
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);
185         }
186
187         /**
188          * Destructor frees the wrapped buffer using the Xerces memory manager.
189          */
190         ~auto_ptr_XMLCh() {
191             xercesc::XMLString::release(&m_buf);
192         }
193
194         /**
195          * Returns the wrapped buffer.
196          * @return a null-terminated Unicode string
197          */
198         const XMLCh* get() const {
199             return m_buf;
200         }
201
202         /**
203          * Returns the wrapped buffer and transfers ownership of it to the caller.
204          * @return a null-terminated Unicode string
205          */
206         XMLCh* release() {
207             XMLCh* temp=m_buf; m_buf=nullptr; return temp;
208         }
209
210     private:
211         XMLCh* m_buf;
212     };
213
214     /**
215      * An auto_ptr that uses array delete on its contents.
216      *
217      * @param T type of pointer to wrap
218      */
219     template <typename T> class auto_arrayptr
220     {
221         T* m_ptr;
222
223         auto_arrayptr(const auto_arrayptr<T>&);
224         auto_arrayptr<T>& operator=(const auto_arrayptr<T>&);
225     public:
226         /**
227          * Constructor.
228          *
229          * @param ptr pointer to wrap
230          */
231         auto_arrayptr(T* ptr) : m_ptr(ptr) {
232         }
233
234         /**
235          * Destructor, uses array delete operation on wrapped pointer.
236          */
237         ~auto_arrayptr() {
238             delete[] m_ptr;
239         }
240
241         /**
242          * Returns the wrapped pointer.
243          * @return the wrapped pointer
244          */
245         const T* get() const {
246             return m_ptr;
247         }
248
249         /**
250          * Returns the wrapped pointer and transfers ownership of it to the caller.
251          * @return the wrapped pointer
252          */
253         T* release() {
254             T* temp=m_ptr; m_ptr=nullptr; return temp;
255         }
256     };
257 };
258
259 #endif /* __xmltooling_unicode_h__ */