gcc const fix, converted linefeeds
[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 #if !defined(__xmltooling_unicode_h__)
24 #define __xmltooling_unicode_h__
25
26 #include <string>
27 #include <xercesc/util/XMLString.hpp>
28 #include <xmltooling/base.h>
29
30 using namespace xercesc;
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      * A minimal auto_ptr-like class that can copy or transcode a buffer into
58      * the local code page and free the result automatically.
59      * 
60      * Needed because a standard auto_ptr would use delete on the resulting
61      * pointer. 
62      */
63     class XMLTOOL_API auto_ptr_char
64     {
65     public:
66         /**
67          * Constructor transcodes a 16-bit Unicode string into the local code page (NOT UTF-8) and wraps the result.
68          * @param src   the 16-bit string to transcode and wrap
69          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
70          */
71         auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
72
73         /**
74          * Constructor copies a local code page (NOT UTF-8) string and wraps the result.
75          * @param src   the local string to copy and wrap
76          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
77          */
78         auto_ptr_char(const char* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
79
80         /**
81          * Destructor frees the wrapped buffer using the Xerces memory manager.
82          */
83         ~auto_ptr_char() { XMLString::release(&m_buf); }
84
85         /**
86          * Returns the wrapped buffer.
87          * @return a null-terminated local code page string
88          */
89         const char* get() const { return m_buf; }
90
91         /**
92          * Returns the wrapped buffer and transfers ownership of it to the caller.
93          * @return a null-terminated local code page string
94          */
95         char* release() { char* temp=m_buf; m_buf=NULL; return temp; }
96
97     private:    
98         char* m_buf;
99     MAKE_NONCOPYABLE(auto_ptr_char);
100     };
101
102     /**
103      * A minimal auto_ptr-like class that can copy or transcode a buffer into
104      * 16-bit Unicode and free the result automatically.
105      * 
106      * Needed because a standard auto_ptr would use delete on the resulting
107      * pointer. 
108      */
109     class XMLTOOL_API auto_ptr_XMLCh
110     {
111     public:
112         /**
113          * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
114          * @param src   the local string to transcode and wrap
115          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
116          */
117         auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
118
119         /**
120          * Constructor copies a 16-bit Unicode string and wraps the result.
121          * @param src   the Unicode string to copy and wrap
122          * @param trim  trims leading/trailing whitespace from the result (defaults to true) 
123          */
124         auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
125
126         /**
127          * Destructor frees the wrapped buffer using the Xerces memory manager.
128          */
129         ~auto_ptr_XMLCh() { XMLString::release(&m_buf); }
130
131         /**
132          * Returns the wrapped buffer.
133          * @return a null-terminated Unicode string
134          */
135         const XMLCh* get() const { return m_buf; }
136         
137         /**
138          * Returns the wrapped buffer and transfers ownership of it to the caller.
139          * @return a null-terminated Unicode string
140          */
141         XMLCh* release() { XMLCh* temp=m_buf; m_buf=NULL; return temp; }
142
143     private:
144         XMLCh* m_buf;
145     MAKE_NONCOPYABLE(auto_ptr_XMLCh);
146     };
147
148 };
149
150 #endif /* __xmltooling_unicode_h__ */