1 | // file : xsd/cxx/xml/string.ixx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | #ifndef XSD_CXX_XML_STRING_IXX |
---|
7 | #define XSD_CXX_XML_STRING_IXX |
---|
8 | |
---|
9 | #include <cassert> |
---|
10 | #include <cstring> // std::memcpy |
---|
11 | |
---|
12 | #include <xercesc/util/XMLString.hpp> |
---|
13 | #include <xsd/cxx/xml/std-memory-manager.hxx> |
---|
14 | |
---|
15 | // We sometimes need this functionality even if we are building for |
---|
16 | // wchar_t. |
---|
17 | // |
---|
18 | namespace xsd |
---|
19 | { |
---|
20 | namespace cxx |
---|
21 | { |
---|
22 | namespace xml |
---|
23 | { |
---|
24 | #ifndef XSD_USE_LCP |
---|
25 | namespace bits |
---|
26 | { |
---|
27 | // UTF-16 to/from UTF-8 transcoder. |
---|
28 | // |
---|
29 | template <typename C> |
---|
30 | struct char_transcoder |
---|
31 | { |
---|
32 | static std::basic_string<C> |
---|
33 | to (const XMLCh* s, std::size_t length); |
---|
34 | |
---|
35 | static XMLCh* |
---|
36 | from (const C* s, std::size_t length); |
---|
37 | |
---|
38 | private: |
---|
39 | static const unsigned char first_byte_mask_[5]; |
---|
40 | }; |
---|
41 | } |
---|
42 | #endif |
---|
43 | |
---|
44 | template <> |
---|
45 | inline std::basic_string<char> |
---|
46 | transcode<char> (const XMLCh* s) |
---|
47 | { |
---|
48 | if (s == 0) |
---|
49 | return std::basic_string<char> (); |
---|
50 | |
---|
51 | #ifndef XSD_USE_LCP |
---|
52 | return bits::char_transcoder<char>::to ( |
---|
53 | s, xercesc::XMLString::stringLen (s)); |
---|
54 | #else |
---|
55 | // Use Xerces-C++ local code page transcoding. |
---|
56 | // |
---|
57 | std_memory_manager mm; |
---|
58 | auto_array<char, std_memory_manager> r ( |
---|
59 | xercesc::XMLString::transcode (s, &mm), mm); |
---|
60 | return std::basic_string<char> (r.get ()); |
---|
61 | #endif |
---|
62 | } |
---|
63 | |
---|
64 | template <> |
---|
65 | inline std::basic_string<char> |
---|
66 | transcode<char> (const XMLCh* s, std::size_t len) |
---|
67 | { |
---|
68 | if (s == 0 || len == 0) |
---|
69 | return std::basic_string<char> (); |
---|
70 | |
---|
71 | #ifndef XSD_USE_LCP |
---|
72 | // Convert UTF-16 to UTF-8 |
---|
73 | // |
---|
74 | return bits::char_transcoder<char>::to (s, len); |
---|
75 | #else |
---|
76 | // Use Xerces-C++ local code page transcoding. |
---|
77 | // |
---|
78 | auto_array<XMLCh> tmp (new XMLCh[len + 1]); |
---|
79 | std::memcpy (tmp.get (), s, len * sizeof (XMLCh)); |
---|
80 | tmp[len] = XMLCh (0); |
---|
81 | |
---|
82 | std_memory_manager mm; |
---|
83 | auto_array<char, std_memory_manager> r ( |
---|
84 | xercesc::XMLString::transcode (tmp.get (), &mm), mm); |
---|
85 | |
---|
86 | tmp.reset (); |
---|
87 | |
---|
88 | return std::basic_string<char> (r.get ()); |
---|
89 | #endif |
---|
90 | } |
---|
91 | |
---|
92 | template <> |
---|
93 | inline XMLCh* |
---|
94 | transcode_to_xmlch (const char* s) |
---|
95 | { |
---|
96 | #ifndef XSD_USE_LCP |
---|
97 | // Convert UTF-8 to UTF-16 |
---|
98 | // |
---|
99 | return bits::char_transcoder<char>::from ( |
---|
100 | s, std::char_traits<char>::length (s)); |
---|
101 | #else |
---|
102 | // Use Xerces-C++ local code page transcoding. |
---|
103 | // |
---|
104 | std_memory_manager mm; |
---|
105 | return xercesc::XMLString::transcode (s, &mm); |
---|
106 | #endif |
---|
107 | } |
---|
108 | |
---|
109 | template <> |
---|
110 | inline XMLCh* |
---|
111 | transcode_to_xmlch (const std::basic_string<char>& s) |
---|
112 | { |
---|
113 | #ifndef XSD_USE_LCP |
---|
114 | // Convert UTF-8 to UTF-16 |
---|
115 | // |
---|
116 | return bits::char_transcoder<char>::from ( |
---|
117 | s.c_str (), s.length ()); |
---|
118 | #else |
---|
119 | // Use Xerces-C++ local code page transcoding. |
---|
120 | // |
---|
121 | std_memory_manager mm; |
---|
122 | return xercesc::XMLString::transcode (s.c_str (), &mm); |
---|
123 | #endif |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | #endif // XSD_CXX_XML_STRING_IXX |
---|
130 | |
---|
131 | |
---|
132 | #if defined(XSD_USE_CHAR) || !defined(XSD_USE_WCHAR) |
---|
133 | |
---|
134 | #ifndef XSD_CXX_XML_STRING_IXX_CHAR |
---|
135 | #define XSD_CXX_XML_STRING_IXX_CHAR |
---|
136 | |
---|
137 | #endif // XSD_CXX_XML_STRING_IXX_CHAR |
---|
138 | #endif // XSD_USE_CHAR |
---|
139 | |
---|
140 | |
---|
141 | #if defined(XSD_USE_WCHAR) || !defined(XSD_USE_CHAR) |
---|
142 | |
---|
143 | #ifndef XSD_CXX_XML_STRING_IXX_WCHAR |
---|
144 | #define XSD_CXX_XML_STRING_IXX_WCHAR |
---|
145 | |
---|
146 | namespace xsd |
---|
147 | { |
---|
148 | namespace cxx |
---|
149 | { |
---|
150 | namespace xml |
---|
151 | { |
---|
152 | namespace bits |
---|
153 | { |
---|
154 | template <typename W, std::size_t S> |
---|
155 | struct wchar_transcoder; |
---|
156 | |
---|
157 | // Specialization for 2-byte wchar_t (resulting encoding is UTF-16). |
---|
158 | // |
---|
159 | template <typename W> |
---|
160 | struct wchar_transcoder<W, 2> |
---|
161 | { |
---|
162 | static std::basic_string<W> |
---|
163 | to (const XMLCh* s, std::size_t length); |
---|
164 | |
---|
165 | static XMLCh* |
---|
166 | from (const W* s, std::size_t length); |
---|
167 | }; |
---|
168 | |
---|
169 | |
---|
170 | // Specialization for 4-byte wchar_t (resulting encoding is UCS-4). |
---|
171 | // |
---|
172 | template <typename W> |
---|
173 | struct wchar_transcoder<W, 4> |
---|
174 | { |
---|
175 | static std::basic_string<W> |
---|
176 | to (const XMLCh* s, std::size_t length); |
---|
177 | |
---|
178 | static XMLCh* |
---|
179 | from (const W* s, std::size_t length); |
---|
180 | }; |
---|
181 | } |
---|
182 | |
---|
183 | template <> |
---|
184 | inline std::basic_string<wchar_t> |
---|
185 | transcode<wchar_t> (const XMLCh* s) |
---|
186 | { |
---|
187 | if (s == 0) |
---|
188 | return std::basic_string<wchar_t> (); |
---|
189 | |
---|
190 | return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::to ( |
---|
191 | s, xercesc::XMLString::stringLen (s)); |
---|
192 | } |
---|
193 | |
---|
194 | template <> |
---|
195 | inline std::basic_string<wchar_t> |
---|
196 | transcode<wchar_t> (const XMLCh* s, std::size_t len) |
---|
197 | { |
---|
198 | if (s == 0 || len == 0) |
---|
199 | return std::basic_string<wchar_t> (); |
---|
200 | |
---|
201 | return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::to ( |
---|
202 | s, len); |
---|
203 | } |
---|
204 | |
---|
205 | template <> |
---|
206 | inline XMLCh* |
---|
207 | transcode_to_xmlch (const wchar_t* s) |
---|
208 | { |
---|
209 | return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::from ( |
---|
210 | s, std::char_traits<wchar_t>::length (s)); |
---|
211 | } |
---|
212 | |
---|
213 | template <> |
---|
214 | inline XMLCh* |
---|
215 | transcode_to_xmlch (const std::basic_string<wchar_t>& s) |
---|
216 | { |
---|
217 | return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::from ( |
---|
218 | s.c_str (), s.length ()); |
---|
219 | } |
---|
220 | } |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | #endif // XSD_CXX_XML_STRING_IXX_WCHAR |
---|
225 | #endif // XSD_USE_WCHAR |
---|