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

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

Initial imports for icXML v0.9

File size: 5.6 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/BitOps.hpp>
23#include <icxercesc/util/XMLChTranscoder.hpp>
24#include <string.h>
25
26XERCES_CPP_NAMESPACE_BEGIN
27
28// ---------------------------------------------------------------------------
29//  XMLChTranscoder: Constructors and Destructor
30// ---------------------------------------------------------------------------
31XMLChTranscoder::XMLChTranscoder(const  XMLCh* const   encodingName
32                                , const XMLSize_t      blockSize
33                                , MemoryManager* const manager) :
34
35    XMLTranscoder(encodingName, blockSize, manager)
36{
37}
38
39
40XMLChTranscoder::~XMLChTranscoder()
41{
42}
43
44
45// ---------------------------------------------------------------------------
46//  XMLChTranscoder: Implementation of the transcoder API
47// ---------------------------------------------------------------------------
48XMLSize_t
49XMLChTranscoder::transcodeFrom( const   XMLByte* const          srcData
50                                , const XMLSize_t               srcCount
51                                ,       XMLCh* const            toFill
52                                , const XMLSize_t               maxChars
53                                ,       XMLSize_t&              bytesEaten
54                                ,       unsigned char* const    charSizes)
55{
56    //
57    //  Calculate the max chars we can do here. Its the lesser of the
58    //  max output chars and the number of chars in the source.
59    //
60    const XMLSize_t srcChars = srcCount / sizeof(XMLCh);
61    const XMLSize_t countToDo = srcChars < maxChars ? srcChars : maxChars;
62
63    //
64    //  Copy over the count of chars that we precalculated. Notice we
65    //  convert char count to byte count here!!!
66    //
67    memcpy(toFill, srcData, countToDo * sizeof(XMLCh));
68
69    // Set the bytes eaten
70    bytesEaten = countToDo * sizeof(XMLCh);
71
72    // Set the character sizes to the fixed size
73    memset(charSizes, sizeof(XMLCh), countToDo);
74
75    // Return the chars we transcoded
76    return countToDo;
77}
78
79XMLSize_t
80XMLChTranscoder::transcodeFrom( const   XMLByte* const          srcData
81                                , const XMLSize_t               srcCount
82                                ,       XMLCh* const            toFill
83                                , const XMLSize_t               maxChars
84                                ,       XMLSize_t&              bytesEaten)
85{
86    //
87    //  Calculate the max chars we can do here. Its the lesser of the
88    //  max output chars and the number of chars in the source.
89    //
90    const XMLSize_t srcChars = srcCount / sizeof(XMLCh);
91    const XMLSize_t countToDo = srcChars < maxChars ? srcChars : maxChars;
92
93    //
94    //  Copy over the count of chars that we precalculated. Notice we
95    //  convert char count to byte count here!!!
96    //
97    memcpy(toFill, srcData, countToDo * sizeof(XMLCh));
98
99    // Set the bytes eaten
100    bytesEaten = countToDo * sizeof(XMLCh);
101
102    // Return the chars we transcoded
103    return countToDo;
104}
105
106XMLSize_t XMLChTranscoder::transcodeFrom
107(
108      const XMLByte* const          srcData
109    , const XMLSize_t               srcCount
110    ,       XMLBuffer &             toFill
111)
112{
113    toFill.reset();
114
115    const XMLSize_t srcChars = srcCount / sizeof(XMLCh);
116
117    toFill.ensureCapacity(srcChars);
118
119    //
120    //  Copy over the count of chars that we precalculated. Notice we
121    //  convert char count to byte count here!!!
122    //
123    memcpy(toFill.getRawBuffer(), srcData, srcCount);
124
125    toFill.setLen(srcCount);
126
127    // Return the chars we transcoded
128    return srcChars;
129}
130
131
132XMLSize_t
133XMLChTranscoder::transcodeTo(const  XMLCh* const    srcData
134                            , const XMLSize_t       srcCount
135                            ,       XMLByte* const  toFill
136                            , const XMLSize_t       maxBytes
137                            ,       XMLSize_t&      charsEaten
138                                , const UnRepOpts)
139{
140    //
141    //  Calculate the max chars we can do here. Its the lesser of the
142    //  max chars we can store in the output byte buffer, and the number
143    //  of chars in the source.
144    //
145    const XMLSize_t maxOutChars  = maxBytes / sizeof(XMLCh);
146    const XMLSize_t countToDo    = maxOutChars < srcCount
147                                    ? maxOutChars : srcCount;
148
149    //
150    //  Copy over the number of chars we calculated. Note that we have
151    //  to convert the char count to a byte count!!
152    //
153    memcpy(toFill, srcData, countToDo * sizeof(XMLCh));
154
155    // Set the chars eaten
156    charsEaten = countToDo;
157
158    // Return the bytes we transcoded
159    return countToDo * sizeof(XMLCh);
160}
161
162
163bool XMLChTranscoder::canTranscodeTo(const unsigned int)
164{
165    // We can handle anything
166    return true;
167}
168
169XERCES_CPP_NAMESPACE_END
Note: See TracBrowser for help on using the repository browser.