Update copyright.
[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 using namespace xercesc;
33
34 namespace xmltooling {
35     
36     #ifdef HAVE_GOOD_STL
37         /**
38          * An STL string type that supports 16-bit Unicode.
39          * Most compilers support this, but various versions of gcc3 do not.
40          */
41         typedef std::basic_string<XMLCh> xstring;
42     #endif
43
44     /**
45      * Transcodes a 16-bit Unicode string into UTF-8.
46      * @param src   the 16-bit string to transcode
47      * @return      a UTF-8 string allocated by the Xerces memory manager 
48      */
49     extern XMLTOOL_API char* toUTF8(const XMLCh* src);
50
51     /**
52      * Transcodes a UTF-8 string into 16-bit Unicode.
53      * @param src   the UTF-8 string to transcode
54      * @return      a 16-bit Unicode string allocated by the Xerces memory manager 
55      */
56     extern XMLTOOL_API XMLCh* fromUTF8(const char* src);
57
58     /**
59      * Writes a Unicode string to an ASCII stream by transcoding to UTF8.
60      * 
61      * @param ostr  stream to write to
62      * @param s     string to write
63      * @return      reference to output stream
64      */
65     extern XMLTOOL_API std::ostream& operator<<(std::ostream& ostr, const XMLCh* s);
66
67     /**
68      * A minimal auto_ptr-like class that can copy or transcode a buffer into
69      * the local code page and free the result automatically.
70      * 
71      * Needed because a standard auto_ptr would use delete on the resulting
72      * pointer. 
73      */
74     class XMLTOOL_API auto_ptr_char
75     {
76     public:
77         /**
78          * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
79          * @param src   the 16-bit string to transcode and wrap
80          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
81          */
82         auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
83
84         /**
85          * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
86          * @param src   the local string to copy and wrap
87          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
88          */
89         auto_ptr_char(const char* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
90
91         /**
92          * Destructor frees the wrapped buffer using the Xerces memory manager.
93          */
94         ~auto_ptr_char() { XMLString::release(&m_buf); }
95
96         /**
97          * Returns the wrapped buffer.
98          * @return a null-terminated local code page string
99          */
100         const char* get() const { return m_buf; }
101
102         /**
103          * Returns the wrapped buffer and transfers ownership of it to the caller.
104          * @return a null-terminated local code page string
105          */
106         char* release() { char* temp=m_buf; m_buf=NULL; return temp; }
107
108     private:    
109         char* m_buf;
110     MAKE_NONCOPYABLE(auto_ptr_char);
111     };
112
113     /**
114      * A minimal auto_ptr-like class that can copy or transcode a buffer into
115      * 16-bit Unicode and free the result automatically.
116      * 
117      * Needed because a standard auto_ptr would use delete on the resulting
118      * pointer. 
119      */
120     class XMLTOOL_API auto_ptr_XMLCh
121     {
122     public:
123         /**
124          * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
125          * @param src   the local string to transcode and wrap
126          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
127          */
128         auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
129
130         /**
131          * Constructor copies a 16-bit Unicode string and wraps the result.
132          * @param src   the Unicode string to copy and wrap
133          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
134          */
135         auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
136
137         /**
138          * Destructor frees the wrapped buffer using the Xerces memory manager.
139          */
140         ~auto_ptr_XMLCh() { XMLString::release(&m_buf); }
141
142         /**
143          * Returns the wrapped buffer.
144          * @return a null-terminated Unicode string
145          */
146         const XMLCh* get() const { return m_buf; }
147         
148         /**
149          * Returns the wrapped buffer and transfers ownership of it to the caller.
150          * @return a null-terminated Unicode string
151          */
152         XMLCh* release() { XMLCh* temp=m_buf; m_buf=NULL; return temp; }
153
154     private:
155         XMLCh* m_buf;
156     MAKE_NONCOPYABLE(auto_ptr_XMLCh);
157     };
158
159 };
160
161 #endif /* __xmltooling_unicode_h__ */