1 | /* |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
---|
3 | * contributor license agreements. See the NOTICE file distributed with |
---|
4 | * this work for additional information regarding copyright ownership. |
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
---|
6 | * (the "License"); you may not use this file except in compliance with |
---|
7 | * the License. You may obtain a copy of the License at |
---|
8 | * |
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
10 | * |
---|
11 | * Unless required by applicable law or agreed to in writing, software |
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
14 | * See the License for the specific language governing permissions and |
---|
15 | * limitations under the License. |
---|
16 | */ |
---|
17 | |
---|
18 | /* |
---|
19 | * $Id: IconvGNUTransService.hpp 932887 2010-04-11 13:04:59Z borisk $ |
---|
20 | */ |
---|
21 | |
---|
22 | #if !defined(XERCESC_INCLUDE_GUARD_ICONVGNUTRANSSERVICE_HPP) |
---|
23 | #define XERCESC_INCLUDE_GUARD_ICONVGNUTRANSSERVICE_HPP |
---|
24 | |
---|
25 | #include <icxercesc/util/TransService.hpp> |
---|
26 | #include <xercesc/util/Mutexes.hpp> |
---|
27 | |
---|
28 | #include <iconv.h> |
---|
29 | |
---|
30 | XERCES_CPP_NAMESPACE_BEGIN |
---|
31 | |
---|
32 | // --------------------------------------------------------------------------- |
---|
33 | // Libiconv wrapper (low-level conversion utilities collection) |
---|
34 | // --------------------------------------------------------------------------- |
---|
35 | |
---|
36 | class XMLUTIL_EXPORT IconvGNUWrapper |
---|
37 | { |
---|
38 | public: |
---|
39 | // ----------------------------------------------------------------------- |
---|
40 | // Constructors and Destructor |
---|
41 | // ----------------------------------------------------------------------- |
---|
42 | IconvGNUWrapper |
---|
43 | ( |
---|
44 | iconv_t cd_from, |
---|
45 | iconv_t cd_to, |
---|
46 | size_t uchsize, |
---|
47 | unsigned int ubo, |
---|
48 | MemoryManager* manager |
---|
49 | ); |
---|
50 | virtual ~IconvGNUWrapper(); |
---|
51 | |
---|
52 | // Convert "native unicode" character into XMLCh |
---|
53 | void mbcToXMLCh (const char *mbc, XMLCh *toRet) const; |
---|
54 | |
---|
55 | // Convert XMLCh into "native unicode" character |
---|
56 | void xmlChToMbc (XMLCh xch, char *mbc) const; |
---|
57 | |
---|
58 | // Fill array of XMLCh characters with data, supplied in the array |
---|
59 | // of "native unicode" characters. |
---|
60 | XMLCh* mbsToXML ( |
---|
61 | const char* mbs_str, |
---|
62 | XMLCh* xml_str, |
---|
63 | size_t cnt |
---|
64 | ) const; |
---|
65 | |
---|
66 | |
---|
67 | // Fill array of "native unicode" characters with data, supplied |
---|
68 | // in the array of XMLCh characters. |
---|
69 | char* xmlToMbs |
---|
70 | ( |
---|
71 | const XMLCh* xml_str, |
---|
72 | char* mbs_str, |
---|
73 | size_t cnt |
---|
74 | ) const; |
---|
75 | |
---|
76 | // Private data accessors |
---|
77 | inline iconv_t cdTo () const { return fCDTo; } |
---|
78 | inline iconv_t cdFrom () const { return fCDFrom; } |
---|
79 | inline size_t uChSize () const { return fUChSize; } |
---|
80 | inline unsigned int UBO () const { return fUBO; } |
---|
81 | |
---|
82 | protected: |
---|
83 | // The following four functions should called with the fMutex |
---|
84 | // locked. |
---|
85 | // |
---|
86 | |
---|
87 | // Return uppercase equivalent for XMLCh |
---|
88 | XMLCh toUpper (const XMLCh ch); |
---|
89 | |
---|
90 | // Return uppercase equivalent for XMLCh |
---|
91 | XMLCh toLower (const XMLCh ch); |
---|
92 | |
---|
93 | // Wrapper around the iconv() for transcoding from the local charset |
---|
94 | size_t iconvFrom |
---|
95 | ( |
---|
96 | const char *fromPtr, |
---|
97 | size_t *fromLen, |
---|
98 | char **toPtr, |
---|
99 | size_t toLen |
---|
100 | ); |
---|
101 | |
---|
102 | // Wrapper around the iconv() for transcoding to the local charset |
---|
103 | size_t iconvTo |
---|
104 | ( |
---|
105 | const char *fromPtr, |
---|
106 | size_t *fromLen, |
---|
107 | char **toPtr, |
---|
108 | size_t toLen |
---|
109 | ); |
---|
110 | |
---|
111 | protected: |
---|
112 | |
---|
113 | // Hidden constructor |
---|
114 | IconvGNUWrapper(MemoryManager* manager); |
---|
115 | |
---|
116 | // Private data accessors |
---|
117 | inline void setCDTo (iconv_t cd) { fCDTo = cd; } |
---|
118 | inline void setCDFrom (iconv_t cd) { fCDFrom = cd; } |
---|
119 | inline void setUChSize (size_t sz) { fUChSize = sz; } |
---|
120 | inline void setUBO (unsigned int u) { fUBO = u; } |
---|
121 | |
---|
122 | private: |
---|
123 | // ----------------------------------------------------------------------- |
---|
124 | // Unimplemented constructors and operators |
---|
125 | // ----------------------------------------------------------------------- |
---|
126 | IconvGNUWrapper(const IconvGNUWrapper&); |
---|
127 | IconvGNUWrapper& operator=(const IconvGNUWrapper&); |
---|
128 | |
---|
129 | // ----------------------------------------------------------------------- |
---|
130 | // Private data members |
---|
131 | // |
---|
132 | // fCDTo |
---|
133 | // Characterset conversion descriptor TO the local-host encoding |
---|
134 | // fCDFrom |
---|
135 | // Characterset conversion descriptor FROM the local-host encoding |
---|
136 | // fUChSize |
---|
137 | // Sizeof the "native unicode" character in bytes |
---|
138 | // fUBO |
---|
139 | // "Native unicode" characters byte order |
---|
140 | // ----------------------------------------------------------------------- |
---|
141 | size_t fUChSize; |
---|
142 | unsigned int fUBO; |
---|
143 | iconv_t fCDTo; |
---|
144 | iconv_t fCDFrom; |
---|
145 | |
---|
146 | protected: |
---|
147 | XMLMutex fMutex; |
---|
148 | }; |
---|
149 | |
---|
150 | |
---|
151 | |
---|
152 | // --------------------------------------------------------------------------- |
---|
153 | // FreeBSD-specific Transcoding Service implementation |
---|
154 | // --------------------------------------------------------------------------- |
---|
155 | |
---|
156 | class XMLUTIL_EXPORT IconvGNUTransService : public XMLTransService, IconvGNUWrapper |
---|
157 | { |
---|
158 | public : |
---|
159 | // ----------------------------------------------------------------------- |
---|
160 | // Constructors and Destructor |
---|
161 | // ----------------------------------------------------------------------- |
---|
162 | IconvGNUTransService(MemoryManager* manager); |
---|
163 | ~IconvGNUTransService(); |
---|
164 | |
---|
165 | |
---|
166 | // ----------------------------------------------------------------------- |
---|
167 | // Implementation of the virtual transcoding service API |
---|
168 | // ----------------------------------------------------------------------- |
---|
169 | virtual int compareIString |
---|
170 | ( |
---|
171 | const XMLCh* const comp1 |
---|
172 | , const XMLCh* const comp2 |
---|
173 | ); |
---|
174 | |
---|
175 | virtual int compareNIString |
---|
176 | ( |
---|
177 | const XMLCh* const comp1 |
---|
178 | , const XMLCh* const comp2 |
---|
179 | , const XMLSize_t maxChars |
---|
180 | ); |
---|
181 | |
---|
182 | virtual const XMLCh* getId() const; |
---|
183 | |
---|
184 | virtual XMLLCPTranscoder* makeNewLCPTranscoder(MemoryManager* manager); |
---|
185 | |
---|
186 | virtual bool supportsSrcOfs() const; |
---|
187 | |
---|
188 | virtual void upperCase(XMLCh* const toUpperCase); |
---|
189 | virtual void lowerCase(XMLCh* const toUpperCase); |
---|
190 | |
---|
191 | protected : |
---|
192 | // ----------------------------------------------------------------------- |
---|
193 | // Protected virtual methods |
---|
194 | // ----------------------------------------------------------------------- |
---|
195 | virtual XMLTranscoder* makeNewXMLTranscoder |
---|
196 | ( |
---|
197 | const XMLCh* const encodingName |
---|
198 | , XMLTransService::Codes& resValue |
---|
199 | , const XMLSize_t blockSize |
---|
200 | , MemoryManager* const manager |
---|
201 | ); |
---|
202 | |
---|
203 | |
---|
204 | private : |
---|
205 | // ----------------------------------------------------------------------- |
---|
206 | // Unimplemented constructors and operators |
---|
207 | // ----------------------------------------------------------------------- |
---|
208 | IconvGNUTransService(const IconvGNUTransService&); |
---|
209 | IconvGNUTransService& operator=(const IconvGNUTransService&); |
---|
210 | |
---|
211 | |
---|
212 | // ----------------------------------------------------------------------- |
---|
213 | // Private data members |
---|
214 | // |
---|
215 | // fUnicodeCP |
---|
216 | // Unicode encoding schema name |
---|
217 | // ----------------------------------------------------------------------- |
---|
218 | const char* fUnicodeCP; |
---|
219 | |
---|
220 | }; |
---|
221 | |
---|
222 | |
---|
223 | //---------------------------------------------------------------------------- |
---|
224 | // Implementation of the transcoders for arbitrary input characterset is |
---|
225 | // supported ONLY through libiconv interface |
---|
226 | //---------------------------------------------------------------------------- |
---|
227 | |
---|
228 | class XMLUTIL_EXPORT IconvGNUTranscoder : public XMLTranscoder, IconvGNUWrapper |
---|
229 | { |
---|
230 | public : |
---|
231 | // ----------------------------------------------------------------------- |
---|
232 | // Constructors and Destructor |
---|
233 | // ----------------------------------------------------------------------- |
---|
234 | IconvGNUTranscoder(const XMLCh* const encodingName |
---|
235 | , const XMLSize_t blockSize |
---|
236 | , iconv_t cd_from |
---|
237 | , iconv_t cd_to |
---|
238 | , size_t uchsize |
---|
239 | , unsigned int ubo |
---|
240 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
241 | ); |
---|
242 | ~IconvGNUTranscoder(); |
---|
243 | |
---|
244 | |
---|
245 | // ----------------------------------------------------------------------- |
---|
246 | // Implementation of the virtual transcoder interface |
---|
247 | // ----------------------------------------------------------------------- |
---|
248 | virtual XMLSize_t transcodeFrom |
---|
249 | ( |
---|
250 | const XMLByte* const srcData |
---|
251 | , const XMLSize_t srcCount |
---|
252 | , XMLCh* const toFill |
---|
253 | , const XMLSize_t maxChars |
---|
254 | , XMLSize_t& bytesEaten |
---|
255 | , unsigned char* const charSizes |
---|
256 | ); |
---|
257 | |
---|
258 | virtual XMLSize_t transcodeFrom |
---|
259 | ( |
---|
260 | const XMLByte* const srcData |
---|
261 | , const XMLSize_t srcCount |
---|
262 | , XMLBuffer & toFill |
---|
263 | ); |
---|
264 | |
---|
265 | virtual XMLSize_t transcodeTo |
---|
266 | ( |
---|
267 | const XMLCh* const srcData |
---|
268 | , const XMLSize_t srcCount |
---|
269 | , XMLByte* const toFill |
---|
270 | , const XMLSize_t maxBytes |
---|
271 | , XMLSize_t& charsEaten |
---|
272 | , const UnRepOpts options |
---|
273 | ); |
---|
274 | |
---|
275 | virtual bool canTranscodeTo |
---|
276 | ( |
---|
277 | const unsigned int toCheck |
---|
278 | ); |
---|
279 | |
---|
280 | private : |
---|
281 | // ----------------------------------------------------------------------- |
---|
282 | // Unimplemented constructors and operators |
---|
283 | // ----------------------------------------------------------------------- |
---|
284 | IconvGNUTranscoder(); |
---|
285 | IconvGNUTranscoder(const IconvGNUTranscoder&); |
---|
286 | IconvGNUTranscoder& operator=(const IconvGNUTranscoder&); |
---|
287 | }; |
---|
288 | |
---|
289 | |
---|
290 | // --------------------------------------------------------------------------- |
---|
291 | // GNU-specific XMLCh <-> local (host) characterset transcoder |
---|
292 | // --------------------------------------------------------------------------- |
---|
293 | |
---|
294 | class XMLUTIL_EXPORT IconvGNULCPTranscoder : public XMLLCPTranscoder, IconvGNUWrapper |
---|
295 | { |
---|
296 | public : |
---|
297 | // ----------------------------------------------------------------------- |
---|
298 | // Constructors and Destructor |
---|
299 | // ----------------------------------------------------------------------- |
---|
300 | |
---|
301 | IconvGNULCPTranscoder |
---|
302 | ( |
---|
303 | iconv_t from, |
---|
304 | iconv_t to, |
---|
305 | size_t uchsize, |
---|
306 | unsigned int ubo, |
---|
307 | MemoryManager* manager |
---|
308 | ); |
---|
309 | |
---|
310 | protected: |
---|
311 | IconvGNULCPTranscoder(); // Unimplemented |
---|
312 | |
---|
313 | public: |
---|
314 | |
---|
315 | ~IconvGNULCPTranscoder(); |
---|
316 | |
---|
317 | |
---|
318 | // ----------------------------------------------------------------------- |
---|
319 | // Implementation of the virtual transcoder interface |
---|
320 | // ----------------------------------------------------------------------- |
---|
321 | virtual char* transcode(const XMLCh* const toTranscode, |
---|
322 | MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
323 | |
---|
324 | virtual XMLCh* transcode(const char* const toTranscode, |
---|
325 | MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
326 | |
---|
327 | |
---|
328 | // ----------------------------------------------------------------------- |
---|
329 | // DEPRECATED old transcode interface |
---|
330 | // ----------------------------------------------------------------------- |
---|
331 | virtual XMLSize_t calcRequiredSize(const char* const srcText |
---|
332 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
333 | |
---|
334 | virtual XMLSize_t calcRequiredSize(const XMLCh* const srcText |
---|
335 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager); |
---|
336 | |
---|
337 | virtual bool transcode |
---|
338 | ( |
---|
339 | const char* const toTranscode |
---|
340 | , XMLCh* const toFill |
---|
341 | , const XMLSize_t maxChars |
---|
342 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
343 | ); |
---|
344 | |
---|
345 | virtual bool transcode |
---|
346 | ( |
---|
347 | const XMLCh* const toTranscode |
---|
348 | , char* const toFill |
---|
349 | , const XMLSize_t maxChars |
---|
350 | , MemoryManager* const manager = XMLPlatformUtils::fgMemoryManager |
---|
351 | ); |
---|
352 | |
---|
353 | |
---|
354 | private : |
---|
355 | // ----------------------------------------------------------------------- |
---|
356 | // Unimplemented constructors and operators |
---|
357 | // ----------------------------------------------------------------------- |
---|
358 | IconvGNULCPTranscoder(const IconvGNULCPTranscoder&); |
---|
359 | IconvGNULCPTranscoder& operator=(const IconvGNULCPTranscoder&); |
---|
360 | }; |
---|
361 | |
---|
362 | XERCES_CPP_NAMESPACE_END |
---|
363 | |
---|
364 | #endif /* ICONVGNUTRANSSERVICE */ |
---|
365 | |
---|
366 | |
---|