Add default constructors.
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file xmltooling/unicode.h
19  *
20  * Helper classes and types for manipulating Unicode
21  */
22
23 #ifndef __xmltooling_unicode_h__
24 #define __xmltooling_unicode_h__
25
26 #include <xmltooling/base.h>
27
28 #include <string>
29 #include <iostream>
30 #include <xercesc/util/XMLString.hpp>
31
32 namespace xmltooling {
33
34     #ifdef HAVE_GOOD_STL
35         /**
36          * An STL string type that supports 16-bit Unicode.
37          * Most compilers support this, but various versions of gcc3 do not.
38          */
39         typedef std::basic_string<XMLCh> xstring;
40     #endif
41
42     /**
43      * Transcodes a 16-bit Unicode string into UTF-8.
44      *
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
48      */
49     extern XMLTOOL_API char* toUTF8(const XMLCh* src, bool use_malloc=false);
50
51     /**
52      * Transcodes a UTF-8 string into 16-bit Unicode.
53      *
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
57      */
58     extern XMLTOOL_API XMLCh* fromUTF8(const char* src, bool use_malloc=false);
59
60     /**
61      * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
62      *
63      * @param ostr  stream to write to
64      * @param s     string to write
65      * @return      reference to output stream
66      */
67     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
68
69     /**
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.
72      *
73      * Needed because a standard auto_ptr would use delete on the resulting
74      * pointer.
75      */
76     class XMLTOOL_API auto_ptr_char
77     {
78         MAKE_NONCOPYABLE(auto_ptr_char);
79     public:
80         /**
81          * Default constructor.
82          */
83         auto_ptr_char() : m_buf(NULL) {
84         }
85
86         /**
87          * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
88          * @param src   the 16-bit string to transcode and wrap
89          * @param trim  trims leading/trailing whitespace from the result (defaults to true)
90          */
91         auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
92             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
93         }
94
95         /**
96          * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
97          * @param src   the local string to copy and wrap
98          * @param trim  trims leading/trailing whitespace from the result (defaults to true)
99          */
100         auto_ptr_char(const char* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
101             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
102         }
103
104         /**
105          * Destructor frees the wrapped buffer using the Xerces memory manager.
106          */
107         ~auto_ptr_char() {
108             xercesc::XMLString::release(&m_buf);
109         }
110
111         /**
112          * Returns the wrapped buffer.
113          * @return a null-terminated local code page string
114          */
115         const char* get() const {
116             return m_buf;
117         }
118
119         /**
120          * Returns the wrapped buffer and transfers ownership of it to the caller.
121          * @return a null-terminated local code page string
122          */
123         char* release() {
124             char* temp=m_buf; m_buf=NULL; return temp;
125         }
126
127     private:
128         char* m_buf;
129     };
130
131     /**
132      * A minimal auto_ptr-like class that can copy or transcode a buffer into
133      * 16-bit Unicode and free the result automatically.
134      *
135      * Needed because a standard auto_ptr would use delete on the resulting
136      * pointer.
137      */
138     class XMLTOOL_API auto_ptr_XMLCh
139     {
140         MAKE_NONCOPYABLE(auto_ptr_XMLCh);
141     public:
142         /**
143          * Default constructor.
144          */
145         auto_ptr_XMLCh() : m_buf(NULL) {
146         }
147
148         /**
149          * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
150          * @param src   the local string to transcode and wrap
151          * @param trim  trims leading/trailing whitespace from the result (defaults to true)
152          */
153         auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
154             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
155         }
156
157         /**
158          * Constructor copies a 16-bit Unicode string and wraps the result.
159          * @param src   the Unicode string to copy and wrap
160          * @param trim  trims leading/trailing whitespace from the result (defaults to true)
161          */
162         auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
163             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
164         }
165
166         /**
167          * Destructor frees the wrapped buffer using the Xerces memory manager.
168          */
169         ~auto_ptr_XMLCh() {
170             xercesc::XMLString::release(&m_buf);
171         }
172
173         /**
174          * Returns the wrapped buffer.
175          * @return a null-terminated Unicode string
176          */
177         const XMLCh* get() const {
178             return m_buf;
179         }
180
181         /**
182          * Returns the wrapped buffer and transfers ownership of it to the caller.
183          * @return a null-terminated Unicode string
184          */
185         XMLCh* release() {
186             XMLCh* temp=m_buf; m_buf=NULL; return temp;
187         }
188
189     private:
190         XMLCh* m_buf;
191     };
192
193     /**
194      * An auto_ptr that uses array delete on its contents.
195      *
196      * @param T type of pointer to wrap
197      */
198     template <typename T> class auto_arrayptr
199     {
200         T* m_ptr;
201
202         auto_arrayptr(const auto_arrayptr<T>&);
203         auto_arrayptr<T>& operator=(const auto_arrayptr<T>&);
204     public:
205         /**
206          * Constructor.
207          *
208          * @param ptr pointer to wrap
209          */
210         auto_arrayptr(T* ptr) : m_ptr(ptr) {
211         }
212
213         /**
214          * Destructor, uses array delete operation on wrapped pointer.
215          */
216         ~auto_arrayptr() {
217             delete[] m_ptr;
218         }
219
220         /**
221          * Returns the wrapped pointer.
222          * @return the wrapped pointer
223          */
224         const T* get() const {
225             return m_ptr;
226         }
227
228         /**
229          * Returns the wrapped pointer and transfers ownership of it to the caller.
230          * @return the wrapped pointer
231          */
232         T* release() {
233             T* temp=m_ptr; m_ptr=NULL; return temp;
234         }
235     };
236 };
237
238 #endif /* __xmltooling_unicode_h__ */