macosunicodeconverter.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,049 行 · 第 1/3 页
CPP
1,049 行
MacOSLCPTranscoder::~MacOSLCPTranscoder(){ // Dispose the XMLTranscoder we're using delete mTranscoder;}// ---------------------------------------------------------------------------// MacOSLCPTranscoder: Implementation of the virtual transcoder interface// ---------------------------------------------------------------------------// ---------------------------------------------------------------------------// In order to implement calcRequiredSize we have to go ahead and do the// conversion, which seems quite painful. The Mac Unicode converter has// no way of saying "don't actually do the conversion." So we end up// converting twice. It would be nice if the calling code could do some// extra buffering to avoid this result.// ---------------------------------------------------------------------------unsigned intMacOSLCPTranscoder::calcRequiredSize(const char* const srcText , MemoryManager* const manager){ if (!srcText) return 0; // Lock our mutex to gain exclusive access to the transcoder // since the lcp transcoders are used globally. XMLMutexLock lock(&mMutex); std::size_t totalCharsProduced = 0; const char* src = srcText; unsigned int srcCnt = std::strlen(src); // Iterate over the characters, converting into a temporary buffer which we'll discard. // All this to get the size required. while (srcCnt > 0) { TempXMLBuf tmpBuf; unsigned int bytesConsumed = 0; unsigned int charsProduced = mTranscoder->transcodeFrom((XMLByte*)src, srcCnt, tmpBuf, kTempBufCount, bytesConsumed, NULL); src += bytesConsumed; srcCnt -= bytesConsumed; totalCharsProduced += charsProduced; // Bail out if nothing more was produced if (charsProduced == 0) break; } // Return number of XMLCh characters required (not counting terminating NULL!) return totalCharsProduced;}// ---------------------------------------------------------------------------// In order to implement calcRequiredSize we have to go ahead and do the// conversion, which seems quite painful. The Mac Unicode converter has// no way of saying "don't actually do the conversion." So we end up// converting twice. It would be nice if the calling code could do some// extra buffering to avoid this result.// ---------------------------------------------------------------------------unsigned intMacOSLCPTranscoder::calcRequiredSize(const XMLCh* const srcText , MemoryManager* const manager){ if (!srcText) return 0; // Lock our mutex to gain exclusive access to the transcoder // since the lcp transcoders are used globally. XMLMutexLock lock(&mMutex); std::size_t totalBytesProduced = 0; const XMLCh* src = srcText; unsigned int srcCnt = XMLString::stringLen(src); // Iterate over the characters, converting into a temporary buffer which we'll discard. // All this to get the size required. while (srcCnt > 0) { TempCharBuf tmpBuf; unsigned int charsConsumed = 0; unsigned int bytesProduced = mTranscoder->transcodeTo(src, srcCnt, (XMLByte*)tmpBuf, kTempBufCount, charsConsumed, XMLTranscoder::UnRep_RepChar); src += charsConsumed; srcCnt -= charsConsumed; totalBytesProduced += bytesProduced; // Bail out if nothing more was produced if (bytesProduced == 0) break; } // Return number of characters required (not counting terminating NULL!) return totalBytesProduced;}char*MacOSLCPTranscoder::transcode(const XMLCh* const srcText){ // Transcode using a memory manager that allocates // memory using new[]. return transcode(srcText, XMLPlatformUtils::fgArrayMemoryManager);}char*MacOSLCPTranscoder::transcode(const XMLCh* const srcText, MemoryManager* const manager){ if (!srcText) return NULL; // Lock our mutex to gain exclusive access to the transcoder // since the lcp transcoders are used globally. XMLMutexLock lock(&mMutex); ArrayJanitor<char> result(0); const XMLCh* src = srcText; unsigned int srcCnt = XMLString::stringLen(src); std::size_t resultCnt = 0; // Iterate over the characters, buffering into a local temporary // buffer, which we dump into an allocated (and reallocated, as necessary) // string for return. while (srcCnt > 0) { // Transcode some characters TempCharBuf tmpBuf; unsigned int charsConsumed = 0; unsigned int bytesProduced = mTranscoder->transcodeTo(src, srcCnt, (XMLByte*)tmpBuf, kTempBufCount, charsConsumed, XMLTranscoder::UnRep_RepChar); src += charsConsumed; srcCnt -= charsConsumed; // Move the data to result buffer, reallocating as needed if (bytesProduced > 0) { // Allocate space for result std::size_t newCnt = resultCnt + bytesProduced; ArrayJanitor<char> newResult ( (char*) manager->allocate((newCnt + 1) * sizeof(char)) //new char[newCnt + 1] , manager ); if (newResult.get() != NULL) { // Incorporate previous result if (result.get() != NULL) std::memcpy(newResult.get(), result.get(), resultCnt); result.reset(newResult.release()); // Copy in new data std::memcpy(result.get() + resultCnt, tmpBuf, bytesProduced); resultCnt = newCnt; // Terminate the result result[resultCnt] = '\0'; } } else break; } if (!result.get()) { // No error, and no result: we probably processed a zero length // input, in which case we want a valid zero length output. result.reset ( (char*) manager->allocate(sizeof(char))//new char[1] , manager ); result[0] = '\0'; } return result.release();}XMLCh*MacOSLCPTranscoder::transcode(const char* const srcText){ // Transcode using a memory manager that allocates // memory using new[]. return transcode(srcText, XMLPlatformUtils::fgArrayMemoryManager);}XMLCh*MacOSLCPTranscoder::transcode(const char* const srcText, MemoryManager* const manager){ if (!srcText) return NULL; // Lock our mutex to gain exclusive access to the transcoder // since the lcp transcoders are used globally. XMLMutexLock lock(&mMutex); ArrayJanitor<XMLCh> result(0); const char* src = srcText; std::size_t srcCnt = std::strlen(src); std::size_t resultCnt = 0; // Iterate over the characters, buffering into a local temporary // buffer, which we dump into an allocated (and reallocated, as necessary) // string for return. while (srcCnt > 0) { // Transcode some characters TempXMLBuf tmpBuf; unsigned int bytesConsumed = 0; unsigned int charsProduced = mTranscoder->transcodeFrom((XMLByte*)src, srcCnt, tmpBuf, kTempBufCount, bytesConsumed, NULL); src += bytesConsumed; srcCnt -= bytesConsumed; // Move the data to result buffer, reallocating as needed if (charsProduced > 0) { // Allocate space for result std::size_t newCnt = resultCnt + charsProduced; ArrayJanitor<XMLCh> newResult ( (XMLCh*) manager->allocate((newCnt + 1) * sizeof(XMLCh)) //new XMLCh[newCnt + 1] , manager ); if (newResult.get() != NULL) { // Incorporate previous result if (result.get() != NULL) std::memcpy(newResult.get(), result.get(), resultCnt * sizeof(XMLCh)); result.reset(newResult.release()); // Copy in new data std::memcpy(result.get() + resultCnt, tmpBuf, charsProduced * sizeof(XMLCh)); resultCnt = newCnt; result[resultCnt] = 0; } } else break; } if (!result.get()) { // No error, and no result: we probably processed a zero length // input, in which case we want a valid zero length output. result.reset ( (XMLCh*) manager->allocate(sizeof(XMLCh))//new XMLCh[1] , manager ); result[0] = '\0'; } return result.release();}boolMacOSLCPTranscoder::transcode( const char* const toTranscode , XMLCh* const toFill , const unsigned int maxChars , MemoryManager* const manager){ // toFill must contain space for maxChars XMLCh characters + 1 (for terminating NULL). // Check for a couple of psycho corner cases if (!toTranscode || !maxChars || !*toTranscode) { toFill[0] = 0; return true; } // Lock our mutex to gain exclusive access to the transcoder // since the lcp transcoders are used globally. XMLMutexLock lock(&mMutex); // Call the transcoder to do the work unsigned int srcLen = std::strlen(toTranscode); unsigned int bytesConsumed = 0; unsigned int charsProduced = mTranscoder->transcodeFrom((XMLByte*)toTranscode, srcLen, toFill, maxChars, bytesConsumed, NULL); // Zero terminate the output string toFill[charsProduced] = L'\0'; // Return true if we consumed all of the characters return (bytesConsumed == srcLen);}boolMacOSLCPTranscoder::transcode( const XMLCh* const toTranscode , char* const toFill , const unsigned int maxChars , MemoryManager* const manager){ // toFill must contain space for maxChars bytes + 1 (for terminating NULL). // Check for a couple of psycho corner cases if (!toTranscode || !maxChars || !*toTranscode) { toFill[0] = 0; return true; } // Lock our mutex to gain exclusive access to the transcoder // since the lcp transcoders are used globally. XMLMutexLock lock(&mMutex); // Call the transcoder to do the work unsigned int srcLen = XMLString::stringLen(toTranscode); unsigned int charsConsumed = 0; unsigned int bytesProduced = mTranscoder->transcodeTo(toTranscode, srcLen, (XMLByte*)toFill, maxChars, charsConsumed, XMLTranscoder::UnRep_RepChar); // Zero terminate the output string toFill[bytesProduced] = '\0'; // Return true if we consumed all of the characters return (charsConsumed == srcLen);}XERCES_CPP_NAMESPACE_END
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?