GME  13
XML88591Transcoder.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/TranscodingException.hpp>
00023 #include <xercesc/util/XML88591Transcoder.hpp>
00024 #include <xercesc/util/XMLString.hpp>
00025 #include <string.h>
00026 
00027 XERCES_CPP_NAMESPACE_BEGIN
00028 
00029 // ---------------------------------------------------------------------------
00030 //  XML88591Transcoder: Constructors and Destructor
00031 // ---------------------------------------------------------------------------
00032 XML88591Transcoder::XML88591Transcoder( const   XMLCh* const    encodingName
00033                                         , const XMLSize_t       blockSize
00034                                         , MemoryManager* const  manager) :
00035 
00036     XMLTranscoder(encodingName, blockSize, manager)
00037 {
00038 }
00039 
00040 
00041 XML88591Transcoder::~XML88591Transcoder()
00042 {
00043 }
00044 
00045 
00046 // ---------------------------------------------------------------------------
00047 //  XML88591Transcoder: Implementation of the transcoder API
00048 // ---------------------------------------------------------------------------
00049 XMLSize_t
00050 XML88591Transcoder::transcodeFrom(  const   XMLByte* const       srcData
00051                                     , const XMLSize_t            srcCount
00052                                     ,       XMLCh* const         toFill
00053                                     , const XMLSize_t            maxChars
00054                                     ,       XMLSize_t&           bytesEaten
00055                                     ,       unsigned char* const charSizes)
00056 {
00057     //
00058     //  Calculate the max chars we can do here. Its the lesser of the
00059     //  max output chars and the number of bytes in the source.
00060     //
00061     const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars;
00062 
00063     //
00064     //  Loop through the bytes to do and convert over each byte. Its just
00065     //  a cast to the wide char type.
00066     //
00067     const XMLByte*  srcPtr = srcData;
00068     XMLCh*          destPtr = toFill;
00069     const XMLByte*  srcEnd = srcPtr + countToDo;
00070     while (srcPtr < srcEnd)
00071         *destPtr++ = XMLCh(*srcPtr++);
00072 
00073     // Set the bytes eaten, and set the char size array to the fixed size
00074     bytesEaten = countToDo;
00075     memset(charSizes, 1, countToDo);
00076 
00077     // Return the chars we transcoded
00078     return countToDo;
00079 }
00080 
00081 
00082 XMLSize_t
00083 XML88591Transcoder::transcodeTo(const   XMLCh* const    srcData
00084                                 , const XMLSize_t       srcCount
00085                                 ,       XMLByte* const  toFill
00086                                 , const XMLSize_t       maxBytes
00087                                 ,       XMLSize_t&      charsEaten
00088                                 , const UnRepOpts       options)
00089 {
00090     //
00091     //  Calculate the max chars we can do here. Its the lesser of the
00092     //  max output bytes and the number of chars in the source.
00093     //
00094     const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes;
00095 
00096     //
00097     //  Loop through the bytes to do and convert over each byte. Its just
00098     //  a downcast of the wide char, checking for unrepresentable chars.
00099     //
00100     const XMLCh*    srcPtr  = srcData;
00101     const XMLCh*    srcEnd  = srcPtr + countToDo;
00102     XMLByte*        destPtr = toFill;
00103     while (srcPtr < srcEnd)
00104     {
00105         // If its legal, take it and jump back to top
00106         if (*srcPtr < 256)
00107         {
00108             *destPtr++ = XMLByte(*srcPtr++);
00109             continue;
00110         }
00111 
00112         //
00113         //  Its not representable so use a replacement char. According to
00114         //  the options, either throw or use the replacement.
00115         //
00116         if (options == UnRep_Throw)
00117         {
00118             XMLCh tmpBuf[17];
00119             XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager());
00120             ThrowXMLwithMemMgr2
00121             (
00122                 TranscodingException
00123                 , XMLExcepts::Trans_Unrepresentable
00124                 , tmpBuf
00125                 , getEncodingName()
00126                 , getMemoryManager()
00127             );
00128         }
00129         *destPtr++ = 0x1A;
00130         srcPtr++;
00131     }
00132 
00133     // Set the chars eaten
00134     charsEaten = countToDo;
00135 
00136     // Return the bytes we transcoded
00137     return countToDo;
00138 }
00139 
00140 
00141 bool XML88591Transcoder::canTranscodeTo(const unsigned int toCheck)
00142 {
00143     return (toCheck < 256);
00144 }
00145 
00146 XERCES_CPP_NAMESPACE_END