Branch data Line data Source code
1 : : // file : xsd/cxx/xml/string.ixx
2 : : // copyright : Copyright (c) 2005-2014 Code Synthesis Tools CC
3 : : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 : :
5 : : #ifndef XSD_CXX_XML_STRING_IXX
6 : : #define XSD_CXX_XML_STRING_IXX
7 : :
8 : : #include <xercesc/util/XMLString.hpp>
9 : :
10 : : // If no transcoder has been included, use the default UTF-8.
11 : : //
12 : : #ifndef XSD_CXX_XML_TRANSCODER
13 : : # include <xsd/cxx/xml/char-utf8.hxx>
14 : : #endif
15 : :
16 : : // We sometimes need this functionality even if we are building for
17 : : // wchar_t.
18 : : //
19 : : namespace xsd
20 : : {
21 : : namespace cxx
22 : : {
23 : : namespace xml
24 : : {
25 : : template <>
26 : : inline std::basic_string<char>
27 : 16 : transcode<char> (const XMLCh* s)
28 : : {
29 [ + - ][ - + ]: 16 : if (s == 0 || *s == XMLCh (0))
30 : : return std::basic_string<char> ();
31 : :
32 : : #ifndef XSD_CXX_XML_TRANSCODER_CHAR_LCP
33 : 16 : return char_transcoder::to (s, xercesc::XMLString::stringLen (s));
34 : : #else
35 : : return char_transcoder::to (s);
36 : : #endif
37 : : }
38 : :
39 : : template <>
40 : : inline std::basic_string<char>
41 : : transcode<char> (const XMLCh* s, std::size_t len)
42 : : {
43 [ # # ][ # # ]: 0 : if (s == 0 || len == 0)
44 : : return std::basic_string<char> ();
45 : :
46 [ # # ]: 0 : return char_transcoder::to (s, len);
47 : : }
48 : :
49 : : template <>
50 : : inline XMLCh*
51 : : transcode_to_xmlch (const char* s)
52 : : {
53 : : #ifndef XSD_CXX_XML_TRANSCODER_CHAR_LCP
54 : : return char_transcoder::from (s, std::char_traits<char>::length (s));
55 : : #else
56 : : return char_transcoder::from (s);
57 : : #endif
58 : : }
59 : :
60 : : template <>
61 : : inline XMLCh*
62 : : transcode_to_xmlch (const std::basic_string<char>& s)
63 : : {
64 : : #ifndef XSD_CXX_XML_TRANSCODER_CHAR_LCP
65 [ # # ][ # # ]: 4 : return char_transcoder::from (s.c_str (), s.length ());
[ # # ][ + - ]
[ + - ]
66 : : #else
67 : : return char_transcoder::from (s.c_str ());
68 : : #endif
69 : : }
70 : : }
71 : : }
72 : : }
73 : :
74 : : #endif // XSD_CXX_XML_STRING_IXX
75 : :
76 : :
77 : : #if defined(XSD_USE_CHAR) || !defined(XSD_USE_WCHAR)
78 : :
79 : : #ifndef XSD_CXX_XML_STRING_IXX_CHAR
80 : : #define XSD_CXX_XML_STRING_IXX_CHAR
81 : :
82 : : #endif // XSD_CXX_XML_STRING_IXX_CHAR
83 : : #endif // XSD_USE_CHAR
84 : :
85 : :
86 : : #if defined(XSD_USE_WCHAR) || !defined(XSD_USE_CHAR)
87 : :
88 : : #ifndef XSD_CXX_XML_STRING_IXX_WCHAR
89 : : #define XSD_CXX_XML_STRING_IXX_WCHAR
90 : :
91 : : namespace xsd
92 : : {
93 : : namespace cxx
94 : : {
95 : : namespace xml
96 : : {
97 : : namespace bits
98 : : {
99 : : template <typename W, std::size_t S>
100 : : struct wchar_transcoder;
101 : :
102 : : // Specialization for 2-byte wchar_t (resulting encoding is UTF-16).
103 : : //
104 : : template <typename W>
105 : : struct wchar_transcoder<W, 2>
106 : : {
107 : : static std::basic_string<W>
108 : : to (const XMLCh* s, std::size_t length);
109 : :
110 : : static XMLCh*
111 : : from (const W* s, std::size_t length);
112 : : };
113 : :
114 : :
115 : : // Specialization for 4-byte wchar_t (resulting encoding is UCS-4).
116 : : //
117 : : template <typename W>
118 : : struct wchar_transcoder<W, 4>
119 : : {
120 : : static std::basic_string<W>
121 : : to (const XMLCh* s, std::size_t length);
122 : :
123 : : static XMLCh*
124 : : from (const W* s, std::size_t length);
125 : : };
126 : : }
127 : :
128 : : template <>
129 : : inline std::basic_string<wchar_t>
130 : : transcode<wchar_t> (const XMLCh* s)
131 : : {
132 : : if (s == 0)
133 : : return std::basic_string<wchar_t> ();
134 : :
135 : : return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::to (
136 : : s, xercesc::XMLString::stringLen (s));
137 : : }
138 : :
139 : : template <>
140 : : inline std::basic_string<wchar_t>
141 : : transcode<wchar_t> (const XMLCh* s, std::size_t len)
142 : : {
143 : : if (s == 0 || len == 0)
144 : : return std::basic_string<wchar_t> ();
145 : :
146 : : return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::to (
147 : : s, len);
148 : : }
149 : :
150 : : template <>
151 : : inline XMLCh*
152 : : transcode_to_xmlch (const wchar_t* s)
153 : : {
154 : : return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::from (
155 : : s, std::char_traits<wchar_t>::length (s));
156 : : }
157 : :
158 : : template <>
159 : : inline XMLCh*
160 : : transcode_to_xmlch (const std::basic_string<wchar_t>& s)
161 : : {
162 : : return bits::wchar_transcoder<wchar_t, sizeof (wchar_t)>::from (
163 : : s.c_str (), s.length ());
164 : : }
165 : : }
166 : : }
167 : : }
168 : :
169 : : #endif // XSD_CXX_XML_STRING_IXX_WCHAR
170 : : #endif // XSD_USE_WCHAR
|