Stop defaulting in xercesc namespace.
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.h
1 /*
2  *  Copyright 2001-2007 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 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      * @param src   the 16-bit string to transcode
45      * @return      a UTF-8 string allocated by the Xerces memory manager 
46      */
47     extern XMLTOOL_API char* toUTF8(const XMLCh* src);
48
49     /**
50      * Transcodes a UTF-8 string into 16-bit Unicode.
51      * @param src   the UTF-8 string to transcode
52      * @return      a 16-bit Unicode string allocated by the Xerces memory manager 
53      */
54     extern XMLTOOL_API XMLCh* fromUTF8(const char* src);
55
56     /**
57      * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
58      * 
59      * @param ostr  stream to write to
60      * @param s     string to write
61      * @return      reference to output stream
62      */
63     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
64
65     /**
66      * A minimal auto_ptr-like class that can copy or transcode a buffer into
67      * the local code page and free the result automatically.
68      * 
69      * Needed because a standard auto_ptr would use delete on the resulting
70      * pointer. 
71      */
72     class XMLTOOL_API auto_ptr_char
73     {
74         MAKE_NONCOPYABLE(auto_ptr_char);
75     public:
76         /**
77          * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
78          * @param src   the 16-bit string to transcode and wrap
79          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
80          */
81         auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
82             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
83         }
84
85         /**
86          * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
87          * @param src   the local string to copy and wrap
88          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
89          */
90         auto_ptr_char(const char* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
91             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
92         }
93
94         /**
95          * Destructor frees the wrapped buffer using the Xerces memory manager.
96          */
97         ~auto_ptr_char() {
98             xercesc::XMLString::release(&m_buf);
99         }
100
101         /**
102          * Returns the wrapped buffer.
103          * @return a null-terminated local code page string
104          */
105         const char* get() const {
106             return m_buf;
107         }
108
109         /**
110          * Returns the wrapped buffer and transfers ownership of it to the caller.
111          * @return a null-terminated local code page string
112          */
113         char* release() {
114             char* temp=m_buf; m_buf=NULL; return temp;
115         }
116
117     private:    
118         char* m_buf;
119     };
120
121     /**
122      * A minimal auto_ptr-like class that can copy or transcode a buffer into
123      * 16-bit Unicode and free the result automatically.
124      * 
125      * Needed because a standard auto_ptr would use delete on the resulting
126      * pointer. 
127      */
128     class XMLTOOL_API auto_ptr_XMLCh
129     {
130         MAKE_NONCOPYABLE(auto_ptr_XMLCh);
131     public:
132         /**
133          * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
134          * @param src   the local string to transcode and wrap
135          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
136          */
137         auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(xercesc::XMLString::transcode(src)) {
138             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
139         }
140
141         /**
142          * Constructor copies a 16-bit Unicode string and wraps the result.
143          * @param src   the Unicode string to copy and wrap
144          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
145          */
146         auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(xercesc::XMLString::replicate(src)) {
147             if (trim && m_buf) xercesc::XMLString::trim(m_buf);
148         }
149
150         /**
151          * Destructor frees the wrapped buffer using the Xerces memory manager.
152          */
153         ~auto_ptr_XMLCh() {
154             xercesc::XMLString::release(&m_buf);
155         }
156
157         /**
158          * Returns the wrapped buffer.
159          * @return a null-terminated Unicode string
160          */
161         const XMLCh* get() const {
162             return m_buf;
163         }
164         
165         /**
166          * Returns the wrapped buffer and transfers ownership of it to the caller.
167          * @return a null-terminated Unicode string
168          */
169         XMLCh* release() {
170             XMLCh* temp=m_buf; m_buf=NULL; return temp;
171         }
172
173     private:
174         XMLCh* m_buf;
175     };
176
177 };
178
179 #endif /* __xmltooling_unicode_h__ */