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