2 * Copyright 2001-2006 Internet2
\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
8 * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * Helper classes and types for manipulating Unicode
\r
23 #if !defined(__xmltooling_unicode_h__)
\r
24 #define __xmltooling_unicode_h__
\r
27 #include <xercesc/util/XMLString.hpp>
\r
28 #include <xmltooling/base.h>
\r
30 using namespace xercesc;
\r
32 namespace xmltooling {
\r
34 #ifdef HAVE_GOOD_STL
\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
39 typedef std::basic_string<XMLCh> xstring;
\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
47 extern XMLTOOL_API char* toUTF8(const XMLCh* src);
\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
54 extern XMLTOOL_API XMLCh* fromUTF8(const char* src);
\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
60 * Needed because a standard auto_ptr would use delete on the resulting
\r
63 class XMLTOOL_API auto_ptr_char
\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
71 auto_ptr_char(const XMLCh* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
\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
78 auto_ptr_char(const char* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
\r
81 * Destructor frees the wrapped buffer using the Xerces memory manager.
\r
83 ~auto_ptr_char() { XMLString::release(&m_buf); }
\r
86 * Returns the wrapped buffer.
\r
87 * @return a null-terminated local code page string
\r
89 const char* get() const { return m_buf; }
\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
95 char* release() { char* temp=m_buf; m_buf=NULL; return temp; }
\r
99 MAKE_NONCOPYABLE(auto_ptr_char);
\r
103 * A minimal auto_ptr-like class that can copy or transcode a buffer into
\r
104 * 16-bit Unicode and free the result automatically.
\r
106 * Needed because a standard auto_ptr would use delete on the resulting
\r
109 class XMLTOOL_API auto_ptr_XMLCh
\r
113 * Constructor transcodes a local code page (NOT UTF-8) string into 16-bit Unicode and wraps the result.
\r
114 * @param src the local string to transcode and wrap
\r
115 * @param trim trims leading/trailing whitespace from the result (defaults to true)
\r
117 auto_ptr_XMLCh(const char* src, bool trim=true) : m_buf(XMLString::transcode(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
\r
120 * Constructor copies a 16-bit Unicode string and wraps the result.
\r
121 * @param src the Unicode string to copy and wrap
\r
122 * @param trim trims leading/trailing whitespace from the result (defaults to true)
\r
124 auto_ptr_XMLCh(const XMLCh* src, bool trim=true) : m_buf(XMLString::replicate(src)) {if (trim && m_buf) XMLString::trim(m_buf);}
\r
127 * Destructor frees the wrapped buffer using the Xerces memory manager.
\r
129 ~auto_ptr_XMLCh() { XMLString::release(&m_buf); }
\r
132 * Returns the wrapped buffer.
\r
133 * @return a null-terminated Unicode string
\r
135 const XMLCh* get() const { return m_buf; }
\r
138 * Returns the wrapped buffer and transfers ownership of it to the caller.
\r
139 * @return a null-terminated Unicode string
\r
141 XMLCh* release() { XMLCh* temp=m_buf; m_buf=NULL; return temp; }
\r
145 MAKE_NONCOPYABLE(auto_ptr_XMLCh);
\r
150 #endif /* __xmltooling_unicode_h__ */
\r