Initial check-in
[shibboleth/cpp-xmltooling.git] / xmltooling / unicode.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file unicode.h\r
19  * \r
20  * Helper classes and types for manipulating Unicode\r
21  */\r
22  \r
23 #if !defined(__xmltooling_unicode_h__)\r
24 #define __xmltooling_unicode_h__\r
25 \r
26 #include <string>\r
27 #include <xercesc/util/XMLString.hpp>\r
28 #include <xmltooling/base.h>\r
29 \r
30 using namespace xercesc;\r
31 \r
32 namespace xmltooling {\r
33     \r
34     #ifdef HAVE_GOOD_STL\r
35         /**\r
36          * An STL string type that supports 16-bit Unicode.\r
37          * Most compilers support this, but various versions of gcc3 do not.\r
38          */\r
39         typedef std::basic_string<XMLCh> xstring;\r
40     #endif\r
41 \r
42     /**\r
43      * Transcodes a 16-bit Unicode string into UTF-8.\r
44      * @param src   the 16-bit string to transcode\r
45      * @return      a UTF-8 string allocated by the Xerces memory manager \r
46      */\r
47     extern XMLTOOL_API char* toUTF8(const XMLCh* src);\r
48 \r
49     /**\r
50      * Transcodes a UTF-8 string into 16-bit Unicode.\r
51      * @param src   the UTF-8 string to transcode\r
52      * @return      a 16-bit Unicode string allocated by the Xerces memory manager \r
53      */\r
54     extern XMLTOOL_API XMLCh* fromUTF8(const char* src);\r
55 \r
56     /**\r
57      * A minimal auto_ptr-like class that can copy or transcode a buffer into\r
58      * the local code page and free the result automatically.\r
59      * \r
60      * Needed because a standard auto_ptr would use delete on the resulting\r
61      * pointer. \r
62      */\r
63     class XMLTOOL_API auto_ptr_char\r
64     {\r
65     public:\r
66         /**\r
67          * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.\r
68          * @param src   the 16-bit string to transcode and wrap\r
69          * @param trim  trims leading/trailing whitespace from the result (defaults to true) \r
70          */\r
71         auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}\r
72 \r
73         /**\r
74          * Constructor copies a local code page (NOT UTF-8) string and wraps the result.\r
75          * @param src   the local string to copy and wrap\r
76          * @param trim  trims leading/trailing whitespace from the result (defaults to true) \r
77          */\r
78         auto_ptr_char(const char* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}\r
79 \r
80         /**\r
81          * Destructor frees the wrapped buffer using the Xerces memory manager.\r
82          */\r
83         ~auto_ptr_char() { XMLString::release(&m_buf); }\r
84 \r
85         /**\r
86          * Returns the wrapped buffer.\r
87          * @return a null-terminated local code page string\r
88          */\r
89         const char* get() const { return m_buf; }\r
90 \r
91         /**\r
92          * Returns the wrapped buffer and transfers ownership of it to the caller.\r
93          * @return a null-terminated local code page string\r
94          */\r
95         char* release() { char* temp=m_buf; m_buf=NULL; return temp; }\r
96 \r
97     private:\r
98         auto_ptr_char(const auto_ptr_char&);\r
99         auto_ptr_char& operator=(const auto_ptr_char&);\r
100         \r
101         char* m_buf;\r
102     };\r
103 \r
104     /**\r
105      * A minimal auto_ptr-like class that can copy or transcode a buffer into\r
106      * 16-bit Unicode and free the result automatically.\r
107      * \r
108      * Needed because a standard auto_ptr would use delete on the resulting\r
109      * pointer. \r
110      */\r
111     class XMLTOOL_API auto_ptr_XMLCh\r
112     {\r
113     public:\r
114         /**\r
115          * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.\r
116          * @param src   the local string to transcode and wrap\r
117          * @param trim  trims leading/trailing whitespace from the result (defaults to true) \r
118          */\r
119         auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}\r
120 \r
121         /**\r
122          * Constructor copies a 16-bit Unicode string and wraps the result.\r
123          * @param src   the Unicode string to copy and wrap\r
124          * @param trim  trims leading/trailing whitespace from the result (defaults to true) \r
125          */\r
126         auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}\r
127 \r
128         /**\r
129          * Destructor frees the wrapped buffer using the Xerces memory manager.\r
130          */\r
131         ~auto_ptr_XMLCh() { XMLString::release(&m_buf); }\r
132 \r
133         /**\r
134          * Returns the wrapped buffer.\r
135          * @return a null-terminated Unicode string\r
136          */\r
137         const XMLCh* get() const { return m_buf; }\r
138         \r
139         /**\r
140          * Returns the wrapped buffer and transfers ownership of it to the caller.\r
141          * @return a null-terminated Unicode string\r
142          */\r
143         XMLCh* release() { XMLCh* temp=m_buf; m_buf=NULL; return temp; }\r
144 \r
145     private:\r
146         auto_ptr_XMLCh(const auto_ptr_XMLCh&);\r
147         auto_ptr_XMLCh& operator=(const auto_ptr_XMLCh&);\r
148 \r
149         XMLCh* m_buf;\r
150     };\r
151 \r
152 };\r
153 \r
154 #endif /* __xmltooling_unicode_h__ */\r