source: icXML/icXML-devel/src/icxercesc/util/XML88591Transcoder.cpp

Last change on this file was 3103, checked in by cameron, 6 years ago

Initial imports for icXML v0.9

File size: 6.7 KB
Line 
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// ---------------------------------------------------------------------------
20//  Includes
21// ---------------------------------------------------------------------------
22#include <xercesc/util/TranscodingException.hpp>
23#include <icxercesc/util/XML88591Transcoder.hpp>
24#include <icxercesc/util/XMLString.hpp>
25#include <string.h>
26
27XERCES_CPP_NAMESPACE_BEGIN
28
29// ---------------------------------------------------------------------------
30//  XML88591Transcoder: Constructors and Destructor
31// ---------------------------------------------------------------------------
32XML88591Transcoder::XML88591Transcoder( const   XMLCh* const    encodingName
33                                        , const XMLSize_t       blockSize
34                                        , MemoryManager* const  manager) :
35
36    XMLTranscoder(encodingName, blockSize, manager)
37{
38}
39
40
41XML88591Transcoder::~XML88591Transcoder()
42{
43}
44
45
46// ---------------------------------------------------------------------------
47//  XML88591Transcoder: Implementation of the transcoder API
48// ---------------------------------------------------------------------------
49XMLSize_t
50XML88591Transcoder::transcodeFrom(  const   XMLByte* const       srcData
51                                    , const XMLSize_t            srcCount
52                                    ,       XMLCh* const         toFill
53                                    , const XMLSize_t            maxChars
54                                    ,       XMLSize_t&           bytesEaten
55                                    ,       unsigned char* const charSizes)
56{
57    //
58    //  Calculate the max chars we can do here. Its the lesser of the
59    //  max output chars and the number of bytes in the source.
60    //
61    const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
62
63    //
64    //  Loop through the bytes to do and convert over each byte. Its just
65    //  a cast to the wide char type.
66    //
67    const XMLByte*  srcPtr = srcData;
68    XMLCh*          destPtr = toFill;
69    const XMLByte*  srcEnd = srcPtr + countToDo;
70    while (srcPtr < srcEnd)
71        *destPtr++ = XMLCh(*srcPtr++);
72
73    // Set the bytes eaten, and set the char size array to the fixed size
74    bytesEaten = countToDo;
75    memset(charSizes, 1, countToDo);
76
77    // Return the chars we transcoded
78    return countToDo;
79}
80
81XMLSize_t
82XML88591Transcoder::transcodeFrom(  const   XMLByte* const       srcData
83                                    , const XMLSize_t            srcCount
84                                    ,       XMLCh* const         toFill
85                                    , const XMLSize_t            maxChars
86                                    ,       XMLSize_t&           bytesEaten)
87{
88    //
89    //  Calculate the max chars we can do here. Its the lesser of the
90    //  max output chars and the number of bytes in the source.
91    //
92    const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
93
94    //
95    //  Loop through the bytes to do and convert over each byte. Its just
96    //  a cast to the wide char type.
97    //
98    const XMLByte*  srcPtr = srcData;
99    XMLCh*          destPtr = toFill;
100    const XMLByte*  srcEnd = srcPtr + countToDo;
101    while (srcPtr < srcEnd)
102        *destPtr++ = XMLCh(*srcPtr++);
103
104    // Set the bytes eaten, and set the char size array to the fixed size
105    bytesEaten = countToDo;
106
107    // Return the chars we transcoded
108    return countToDo;
109}
110
111XMLSize_t
112XML88591Transcoder::transcodeFrom
113(
114      const XMLByte* const          srcData
115    , const XMLSize_t               srcCount
116    ,       XMLBuffer &             toFill
117)
118{
119
120    toFill.reset();
121    toFill.ensureCapacity(srcCount);
122
123    //
124    //  Loop through the bytes to do and convert over each byte. Its just
125    //  a cast to the wide char type.
126    //
127
128    const XMLByte*  srcPtr = srcData;
129
130    XMLCh*          destPtr = toFill.getRawBuffer();
131    const XMLByte*  srcEnd = srcPtr + srcCount;
132
133    while (srcPtr < srcEnd)
134    {
135        *destPtr++ = XMLCh(*srcPtr++);
136    }
137
138    toFill.setLen(srcCount);
139
140    // Return the chars we transcoded
141    return srcCount;
142}
143
144XMLSize_t
145XML88591Transcoder::transcodeTo(const   XMLCh* const    srcData
146                                , const XMLSize_t       srcCount
147                                ,       XMLByte* const  toFill
148                                , const XMLSize_t       maxBytes
149                                ,       XMLSize_t&      charsEaten
150                                , const UnRepOpts       options)
151{
152    //
153    //  Calculate the max chars we can do here. Its the lesser of the
154    //  max output bytes and the number of chars in the source.
155    //
156    const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes;
157
158    //
159    //  Loop through the bytes to do and convert over each byte. Its just
160    //  a downcast of the wide char, checking for unrepresentable chars.
161    //
162    const XMLCh*    srcPtr  = srcData;
163    const XMLCh*    srcEnd  = srcPtr + countToDo;
164    XMLByte*        destPtr = toFill;
165    while (srcPtr < srcEnd)
166    {
167        // If its legal, take it and jump back to top
168        if (*srcPtr < 256)
169        {
170            *destPtr++ = XMLByte(*srcPtr++);
171            continue;
172        }
173
174        //
175        //  Its not representable so use a replacement char. According to
176        //  the options, either throw or use the replacement.
177        //
178        if (options == UnRep_Throw)
179        {
180            XMLCh tmpBuf[17];
181            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
182            ThrowXMLwithMemMgr2
183            (
184                TranscodingException
185                , XMLExcepts::Trans_Unrepresentable
186                , tmpBuf
187                , getEncodingName()
188                , getMemoryManager()
189            );
190        }
191        *destPtr++ = 0x1A;
192        srcPtr++;
193    }
194
195    // Set the chars eaten
196    charsEaten = countToDo;
197
198    // Return the bytes we transcoded
199    return countToDo;
200}
201
202
203bool XML88591Transcoder::canTranscodeTo(const unsigned int toCheck)
204{
205    return (toCheck < 256);
206}
207
208XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.