source: icXML/icXML-devel/src/icxercesc/util/XML88591Transcoder.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: 5.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
83(
84      const XMLByte* const          srcData
85    , const XMLSize_t               srcCount
86    ,       XMLBuffer &             toFill
87)
88{
89
90    toFill.reset();
91    toFill.ensureCapacity(srcCount);
92
93    //
94    //  Loop through the bytes to do and convert over each byte. Its just
95    //  a cast to the wide char type.
96    //
97
98    const XMLByte*  srcPtr = srcData;
99
100    XMLCh*          destPtr = toFill.getRawBuffer();
101    const XMLByte*  srcEnd = srcPtr + srcCount;
102
103    while (srcPtr < srcEnd)
104    {
105        *destPtr++ = XMLCh(*srcPtr++);
106    }
107
108    toFill.setLen(srcCount);
109
110    // Return the chars we transcoded
111    return srcCount;
112}
113
114XMLSize_t
115XML88591Transcoder::transcodeTo(const   XMLCh* const    srcData
116                                , const XMLSize_t       srcCount
117                                ,       XMLByte* const  toFill
118                                , const XMLSize_t       maxBytes
119                                ,       XMLSize_t&      charsEaten
120                                , const UnRepOpts       options)
121{
122    //
123    //  Calculate the max chars we can do here. Its the lesser of the
124    //  max output bytes and the number of chars in the source.
125    //
126    const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes;
127
128    //
129    //  Loop through the bytes to do and convert over each byte. Its just
130    //  a downcast of the wide char, checking for unrepresentable chars.
131    //
132    const XMLCh*    srcPtr  = srcData;
133    const XMLCh*    srcEnd  = srcPtr + countToDo;
134    XMLByte*        destPtr = toFill;
135    while (srcPtr < srcEnd)
136    {
137        // If its legal, take it and jump back to top
138        if (*srcPtr < 256)
139        {
140            *destPtr++ = XMLByte(*srcPtr++);
141            continue;
142        }
143
144        //
145        //  Its not representable so use a replacement char. According to
146        //  the options, either throw or use the replacement.
147        //
148        if (options == UnRep_Throw)
149        {
150            XMLCh tmpBuf[17];
151            XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
152            ThrowXMLwithMemMgr2
153            (
154                TranscodingException
155                , XMLExcepts::Trans_Unrepresentable
156                , tmpBuf
157                , getEncodingName()
158                , getMemoryManager()
159            );
160        }
161        *destPtr++ = 0x1A;
162        srcPtr++;
163    }
164
165    // Set the chars eaten
166    charsEaten = countToDo;
167
168    // Return the bytes we transcoded
169    return countToDo;
170}
171
172
173bool XML88591Transcoder::canTranscodeTo(const unsigned int toCheck)
174{
175    return (toCheck < 256);
176}
177
178XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.