source: icXML/icXML-devel/src/icxercesc/util/XMLASCIITranscoder.cpp @ 2721

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

Fix imports in icXML modified Xerces files

File size: 6.8 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 <icxercesc/util/XMLASCIITranscoder.hpp>
23#include <icxercesc/util/XMLString.hpp>
24#include <xercesc/util/TranscodingException.hpp>
25#include <string.h>
26
27XERCES_CPP_NAMESPACE_BEGIN
28
29// ---------------------------------------------------------------------------
30//  XMLASCIITranscoder: Constructors and Destructor
31// ---------------------------------------------------------------------------
32XMLASCIITranscoder::XMLASCIITranscoder( const   XMLCh* const   encodingName
33                                        , const XMLSize_t      blockSize
34                                        , MemoryManager* const manager) :
35
36    XMLTranscoder(encodingName, blockSize, manager)
37{
38}
39
40
41XMLASCIITranscoder::~XMLASCIITranscoder()
42{
43}
44
45
46// ---------------------------------------------------------------------------
47//  XMLASCIITranscoder: Implementation of the transcoder API
48// ---------------------------------------------------------------------------
49XMLSize_t
50XMLASCIITranscoder::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 source byte count.
60    //
61    const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
62
63    //
64    //  Now loop through that many source chars and just cast each one
65    //  over to the XMLCh format. Check each source that its really a
66    //  valid ASCI char.
67    //
68    const XMLByte*  srcPtr = srcData;
69    XMLCh*          outPtr = toFill;
70    XMLSize_t       countDone = 0;
71    for (; countDone < countToDo; countDone++)
72    {
73        // Do the optimistic work up front
74        if (*srcPtr < 0x80)
75        {
76            *outPtr++ = XMLCh(*srcPtr++);
77            continue;
78        }
79
80        //
81        //  We got non source encoding char. If we got more than 32 chars,
82        //  the just break out. We'll come back here later to hit this again
83        //  and give an error much closer to the real source position.
84        //
85        if (countDone > 32)
86            break;
87
88        XMLCh tmpBuf[17];
89        XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
90        ThrowXMLwithMemMgr2
91        (
92            TranscodingException
93            , XMLExcepts::Trans_Unrepresentable
94            , tmpBuf
95            , getEncodingName()
96            , getMemoryManager()
97        );
98    }
99
100    // Set the bytes we ate
101    bytesEaten = countDone;
102
103    // Set the char sizes to the fixed size
104    memset(charSizes, 1, countDone);
105
106    // Return the chars we transcoded
107    return countDone;
108}
109
110XMLSize_t XMLASCIITranscoder::transcodeFrom
111(
112      const XMLByte* const          srcData
113    , const XMLSize_t               srcCount
114    ,       XMLBuffer &             toFill
115)
116{
117    //
118    //  Now loop through that many source chars and just cast each one
119    //  over to the XMLCh format. Check each source that its really a
120    //  valid ASCI char.
121    //
122
123    toFill.reset();
124    toFill.ensureCapacity(srcCount);
125
126    const XMLByte * srcPtr = srcData;
127    const XMLByte * const endPtr = srcPtr + srcCount;
128    XMLCh*          outPtr = toFill.getRawBuffer();
129
130    while (srcPtr < endPtr)
131    {
132        // Do the optimistic work up front
133        if (likely(*srcPtr < 0x80))
134        {
135            *outPtr++ = XMLCh(*srcPtr++);
136        }
137        else
138        {
139            XMLCh tmpBuf[17];
140            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
141            ThrowXMLwithMemMgr2
142            (
143                TranscodingException
144                , XMLExcepts::Trans_Unrepresentable
145                , tmpBuf
146                , getEncodingName()
147                , getMemoryManager()
148            );
149        }
150    }
151
152    toFill.setLen(srcCount);
153
154    // Return the chars we transcoded
155    return srcCount;
156
157}
158
159XMLSize_t
160XMLASCIITranscoder::transcodeTo(const   XMLCh* const    srcData
161                                , const XMLSize_t       srcCount
162                                ,       XMLByte* const  toFill
163                                , const XMLSize_t       maxBytes
164                                ,       XMLSize_t&      charsEaten
165                                , const UnRepOpts       options)
166{
167    //
168    //  Calculate the max chars we can do here. Its the lesser of the
169    //  max output chars and the source byte count.
170    //
171    const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes;
172
173    const XMLCh*    srcPtr = srcData;
174    XMLByte*        outPtr = toFill;
175    for (XMLSize_t index = 0; index < countToDo; index++)
176    {
177        // If its legal, do it and jump back to the top
178        if (*srcPtr < 0x80)
179        {
180            *outPtr++ = XMLByte(*srcPtr++);
181            continue;
182        }
183
184        //
185        //  Its not representable so use a replacement char. According to
186        //  the options, either throw or use the replacement.
187        //
188        if (options == UnRep_Throw)
189        {
190            XMLCh tmpBuf[17];
191            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
192            ThrowXMLwithMemMgr2
193            (
194                TranscodingException
195                , XMLExcepts::Trans_Unrepresentable
196                , tmpBuf
197                , getEncodingName()
198                , getMemoryManager()
199            );
200        }
201
202        // Use the replacement char
203        *outPtr++ = 0x1A;
204        srcPtr++;
205    }
206
207    // Set the chars we ate
208    charsEaten = countToDo;
209
210    // Return the byte we transcoded
211    return countToDo;
212}
213
214
215bool XMLASCIITranscoder::canTranscodeTo(const unsigned int toCheck)
216{
217    return (toCheck < 0x80);
218}
219
220XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.