GME  13
XMLChTranscoder.cpp
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one or more
00003  * contributor license agreements.  See the NOTICE file distributed with
00004  * this work for additional information regarding copyright ownership.
00005  * The ASF licenses this file to You under the Apache License, Version 2.0
00006  * (the "License"); you may not use this file except in compliance with
00007  * the License.  You may obtain a copy of the License at
00008  * 
00009  *      http://www.apache.org/licenses/LICENSE-2.0
00010  * 
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 
00019 // ---------------------------------------------------------------------------
00020 //  Includes
00021 // ---------------------------------------------------------------------------
00022 #include <xercesc/util/BitOps.hpp>
00023 #include <xercesc/util/XMLChTranscoder.hpp>
00024 #include <string.h>
00025 
00026 XERCES_CPP_NAMESPACE_BEGIN
00027 
00028 // ---------------------------------------------------------------------------
00029 //  XMLChTranscoder: Constructors and Destructor
00030 // ---------------------------------------------------------------------------
00031 XMLChTranscoder::XMLChTranscoder(const  XMLCh* const   encodingName
00032                                 , const XMLSize_t      blockSize
00033                                 , MemoryManager* const manager) :
00034 
00035     XMLTranscoder(encodingName, blockSize, manager)
00036 {
00037 }
00038 
00039 
00040 XMLChTranscoder::~XMLChTranscoder()
00041 {
00042 }
00043 
00044 
00045 // ---------------------------------------------------------------------------
00046 //  XMLChTranscoder: Implementation of the transcoder API
00047 // ---------------------------------------------------------------------------
00048 XMLSize_t
00049 XMLChTranscoder::transcodeFrom( const   XMLByte* const          srcData
00050                                 , const XMLSize_t               srcCount
00051                                 ,       XMLCh* const            toFill
00052                                 , const XMLSize_t               maxChars
00053                                 ,       XMLSize_t&              bytesEaten
00054                                 ,       unsigned char* const    charSizes)
00055 {
00056     //
00057     //  Calculate the max chars we can do here. Its the lesser of the
00058     //  max output chars and the number of chars in the source.
00059     //
00060     const XMLSize_t srcChars = srcCount / sizeof(XMLCh);
00061     const XMLSize_t countToDo = srcChars < maxChars ? srcChars : maxChars;
00062 
00063     //
00064     //  Copy over the count of chars that we precalculated. Notice we
00065     //  convert char count to byte count here!!!
00066     //
00067     memcpy(toFill, srcData, countToDo * sizeof(XMLCh));
00068 
00069     // Set the bytes eaten
00070     bytesEaten = countToDo * sizeof(XMLCh);
00071 
00072     // Set the character sizes to the fixed size
00073     memset(charSizes, sizeof(XMLCh), countToDo);
00074 
00075     // Return the chars we transcoded
00076     return countToDo;
00077 }
00078 
00079 
00080 XMLSize_t
00081 XMLChTranscoder::transcodeTo(const  XMLCh* const    srcData
00082                             , const XMLSize_t       srcCount
00083                             ,       XMLByte* const  toFill
00084                             , const XMLSize_t       maxBytes
00085                             ,       XMLSize_t&      charsEaten
00086                                 , const UnRepOpts)
00087 {
00088     //
00089     //  Calculate the max chars we can do here. Its the lesser of the
00090     //  max chars we can store in the output byte buffer, and the number
00091     //  of chars in the source.
00092     //
00093     const XMLSize_t maxOutChars  = maxBytes / sizeof(XMLCh);
00094     const XMLSize_t countToDo    = maxOutChars < srcCount
00095                                     ? maxOutChars : srcCount;
00096 
00097     //
00098     //  Copy over the number of chars we calculated. Note that we have
00099     //  to convert the char count to a byte count!!
00100     //
00101     memcpy(toFill, srcData, countToDo * sizeof(XMLCh));
00102 
00103     // Set the chars eaten
00104     charsEaten = countToDo;
00105 
00106     // Return the bytes we transcoded
00107     return countToDo * sizeof(XMLCh);
00108 }
00109 
00110 
00111 bool XMLChTranscoder::canTranscodeTo(const unsigned int)
00112 {
00113     // We can handle anything
00114     return true;
00115 }
00116 
00117 XERCES_CPP_NAMESPACE_END