GME
13
|
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/XMLASCIITranscoder.hpp> 00023 #include <xercesc/util/XMLString.hpp> 00024 #include <xercesc/util/TranscodingException.hpp> 00025 #include <string.h> 00026 00027 XERCES_CPP_NAMESPACE_BEGIN 00028 00029 // --------------------------------------------------------------------------- 00030 // XMLASCIITranscoder: Constructors and Destructor 00031 // --------------------------------------------------------------------------- 00032 XMLASCIITranscoder::XMLASCIITranscoder( const XMLCh* const encodingName 00033 , const XMLSize_t blockSize 00034 , MemoryManager* const manager) : 00035 00036 XMLTranscoder(encodingName, blockSize, manager) 00037 { 00038 } 00039 00040 00041 XMLASCIITranscoder::~XMLASCIITranscoder() 00042 { 00043 } 00044 00045 00046 // --------------------------------------------------------------------------- 00047 // XMLASCIITranscoder: Implementation of the transcoder API 00048 // --------------------------------------------------------------------------- 00049 XMLSize_t 00050 XMLASCIITranscoder::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 source byte count. 00060 // 00061 const XMLSize_t countToDo = srcCount < maxChars ? srcCount : maxChars; 00062 00063 // 00064 // Now loop through that many source chars and just cast each one 00065 // over to the XMLCh format. Check each source that its really a 00066 // valid ASCI char. 00067 // 00068 const XMLByte* srcPtr = srcData; 00069 XMLCh* outPtr = toFill; 00070 XMLSize_t countDone = 0; 00071 for (; countDone < countToDo; countDone++) 00072 { 00073 // Do the optimistic work up front 00074 if (*srcPtr < 0x80) 00075 { 00076 *outPtr++ = XMLCh(*srcPtr++); 00077 continue; 00078 } 00079 00080 // 00081 // We got non source encoding char. If we got more than 32 chars, 00082 // the just break out. We'll come back here later to hit this again 00083 // and give an error much closer to the real source position. 00084 // 00085 if (countDone > 32) 00086 break; 00087 00088 XMLCh tmpBuf[17]; 00089 XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager()); 00090 ThrowXMLwithMemMgr2 00091 ( 00092 TranscodingException 00093 , XMLExcepts::Trans_Unrepresentable 00094 , tmpBuf 00095 , getEncodingName() 00096 , getMemoryManager() 00097 ); 00098 } 00099 00100 // Set the bytes we ate 00101 bytesEaten = countDone; 00102 00103 // Set the char sizes to the fixed size 00104 memset(charSizes, 1, countDone); 00105 00106 // Return the chars we transcoded 00107 return countDone; 00108 } 00109 00110 00111 XMLSize_t 00112 XMLASCIITranscoder::transcodeTo(const XMLCh* const srcData 00113 , const XMLSize_t srcCount 00114 , XMLByte* const toFill 00115 , const XMLSize_t maxBytes 00116 , XMLSize_t& charsEaten 00117 , const UnRepOpts options) 00118 { 00119 // 00120 // Calculate the max chars we can do here. Its the lesser of the 00121 // max output chars and the source byte count. 00122 // 00123 const XMLSize_t countToDo = srcCount < maxBytes ? srcCount : maxBytes; 00124 00125 const XMLCh* srcPtr = srcData; 00126 XMLByte* outPtr = toFill; 00127 for (XMLSize_t index = 0; index < countToDo; index++) 00128 { 00129 // If its legal, do it and jump back to the top 00130 if (*srcPtr < 0x80) 00131 { 00132 *outPtr++ = XMLByte(*srcPtr++); 00133 continue; 00134 } 00135 00136 // 00137 // Its not representable so use a replacement char. According to 00138 // the options, either throw or use the replacement. 00139 // 00140 if (options == UnRep_Throw) 00141 { 00142 XMLCh tmpBuf[17]; 00143 XMLString::binToText((unsigned int)*srcPtr, tmpBuf, 16, 16, getMemoryManager()); 00144 ThrowXMLwithMemMgr2 00145 ( 00146 TranscodingException 00147 , XMLExcepts::Trans_Unrepresentable 00148 , tmpBuf 00149 , getEncodingName() 00150 , getMemoryManager() 00151 ); 00152 } 00153 00154 // Use the replacement char 00155 *outPtr++ = 0x1A; 00156 srcPtr++; 00157 } 00158 00159 // Set the chars we ate 00160 charsEaten = countToDo; 00161 00162 // Return the byte we transcoded 00163 return countToDo; 00164 } 00165 00166 00167 bool XMLASCIITranscoder::canTranscodeTo(const unsigned int toCheck) 00168 { 00169 return (toCheck < 0x80); 00170 } 00171 00172 XERCES_CPP_NAMESPACE_END