uniconv390transservice.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 1,475 行 · 第 1/4 页

CPP
1,475
字号
      srcCount++;   } else {      srcBuf[0] = XMLCh(toCheck);   }   // Set up a temp buffer to format into. Make it more than big enough   char  tmpBuf[16];   char *tmpInPtr = (char *) srcBuf;   char *tmpOutPtr = (char *) tmpBuf;   unsigned int inByteLeft = srcCount*sizeof(XMLCh);   unsigned int outByteLeft = 16;   { // locking scope      XMLMutexLock lockConverter(&fConverter->fMutex);      retCode = uniconv(fConverter->fIconv390DescriptorTo, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);   }   if ( (retCode < 0) && (errno != E2BIG) ) {      return false;   }   return true;}// ***************************************************************************// ***************************************************************************// ***************************************************************************// ***************************************************************************// *************** Uniconv390LCPTranscoder Class *****************************// ***************************************************************************// ***************************************************************************// ***************************************************************************// ***************************************************************************// ---------------------------------------------------------------------------//  Uniconv390LCPTranscoder: Constructor and Destructor// ---------------------------------------------------------------------------Uniconv390LCPTranscoder::Uniconv390LCPTranscoder( uniconvconverter_t* const toAdopt) :    fConverter(toAdopt){}Uniconv390LCPTranscoder::~Uniconv390LCPTranscoder(){    // If there is a converter, clean it up   if (fConverter) {      removeConverter(fConverter);      fConverter=0;   }}// ---------------------------------------------------------------------------//  Uniconv390LCPTranscoder: calcRequiredSize//// The only way I can find to reliably determine the exact required size is to actually// transcode the string and see how long it is. Fortunately, this is only done as a last// ditch effort so it should only be used very rarely (if at all).// ---------------------------------------------------------------------------unsigned int Uniconv390LCPTranscoder::calcRequiredSize(const XMLCh* const srcText                                                       , MemoryManager* const manager){DBGPRINTF1("Uniconv390LCPTranscoder::calcRequiredSize(const XMLCh* const srcText)  \n");//printf("!!!***Uniconv390LCPTranscoder::calcRequiredSize(const XMLCh* const srcText)  \n");   int thesize=0;   if (!srcText)      return 0;   if (!*srcText)      return 0;   char * result = transcode(srcText, manager);   if (result) {      thesize = strlen(result);      manager->deallocate(result);//delete [] result;   }   return thesize;}unsigned int Uniconv390LCPTranscoder::calcRequiredSize(const char* const srcText                                                       , MemoryManager* const manager){DBGPRINTF1("Uniconv390LCPTranscoder::calcRequiredSize(const char* const srcText)  \n");//printf("!!!***Uniconv390LCPTranscoder::calcRequiredSize(const char* const srcText)  \n");   int thesize=0;   if (!srcText)      return 0;   if (!*srcText)      return 0;   XMLCh * result = transcode(srcText, manager);   if (result) {      thesize = getWideCharLength(result);      manager->deallocate(result);//delete [] result;   }DBGPRINTF2("Uniconv390LCPTranscoder::calcRequiredSize(const char* const srcText) %d  \n",thesize);   return thesize;}// ---------------------------------------------------------------------------//  Uniconv390LCPTranscoder: transcode//// Now what follows are various methods to transcode to/from unicode.// ---------------------------------------------------------------------------char* Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode){//printf("Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode) ");//printf("transcode handle=%x\n",fConverter->fIconv390DescriptorTo);   if (!toTranscode)      return 0;   char* retVal = 0;    // find out the length of the source and use this as an estimate for the needed buffer length.   unsigned int  wLent = getWideCharLength(toTranscode);   if (wLent == 0) {      retVal = new char[1];      retVal[0] = 0;      return retVal;   }   retVal = new char[wLent * 2 + 1]; // get double just to be sure.   while (true) {      int retCode;      char *tmpInPtr = (char*) toTranscode;      char *tmpOutPtr = (char*) retVal;      unsigned int inByteLeft = wLent*sizeof(XMLCh);      unsigned int outByteLeft = wLent*sizeof(XMLCh);//printf("!!!transcode len=%d\n",wLent);      { // Locking scope         XMLMutexLock lockConverter(&fConverter->fMutex);         retCode = uniconv(fConverter->fIconv390DescriptorTo, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);      }//printf("!!!transcode uniconv finished rc=%d errno=%d\n",retCode,errno);      // If the data does not fit into our estimation of the buffer size, then delete the buffer,      // double the estimated length and try again.      if ( ((retCode < 0) && (errno == E2BIG)) || (outByteLeft == 0) ) {//printf("!!!Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode):Retrying with a bigger buffer.......\n");         delete [] retVal;         wLent*=2;         retVal = new char[wLent*sizeof(XMLCh) + 1];      }      // If uniconv doesn't complete for any other reason, then return failure.      else if (retCode < 0) {         return 0;      }      // it was successful so break out of the loop      else {         *tmpOutPtr = 0x00;         break;      }   }//printf("Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode):%s\n",retVal);   return retVal;}char* Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode,                                         MemoryManager* const manager){//printf("Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode) ");//printf("transcode handle=%x\n",fConverter->fIconv390DescriptorTo);   if (!toTranscode)      return 0;   char* retVal = 0;    // find out the length of the source and use this as an estimate for the needed buffer length.   unsigned int  wLent = getWideCharLength(toTranscode);   if (wLent == 0) {      retVal = (char*) manager->allocate(sizeof(char));//new char[1];      retVal[0] = 0;      return retVal;   }   retVal = (char*) manager->allocate((wLent * 2 + 1) * sizeof(char));//new char[wLent * 2 + 1]; // get double just to be sure.   while (true) {      int retCode;      char *tmpInPtr = (char*) toTranscode;      char *tmpOutPtr = (char*) retVal;      unsigned int inByteLeft = wLent*sizeof(XMLCh);      unsigned int outByteLeft = wLent*sizeof(XMLCh);//printf("!!!transcode len=%d\n",wLent);      { // Locking scope         XMLMutexLock lockConverter(&fConverter->fMutex);         retCode = uniconv(fConverter->fIconv390DescriptorTo, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);      }//printf("!!!transcode uniconv finished rc=%d errno=%d\n",retCode,errno);      // If the data does not fit into our estimation of the buffer size, then delete the buffer,      // double the estimated length and try again.      if ( ((retCode < 0) && (errno == E2BIG)) || (outByteLeft == 0) ) {//printf("!!!Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode):Retrying with a bigger buffer.......\n");         manager->deallocate(retVal);//delete [] retVal;         wLent*=2;         retVal = (char*) manager->allocate         (             (wLent*sizeof(XMLCh) + 1) * sizeof(char)         );//new char[wLent*sizeof(XMLCh) + 1];      }      // If uniconv doesn't complete for any other reason, then return failure.      else if (retCode < 0) {         return 0;      }      // it was successful so break out of the loop      else {         *tmpOutPtr = 0x00;         break;      }   }//printf("Uniconv390LCPTranscoder::transcode(const XMLCh* const toTranscode):%s\n",retVal);   return retVal;}XMLCh* Uniconv390LCPTranscoder::transcode(const char* const toTranscode){DBGPRINTF2("Uniconv390LCPTranscoder::transcode(const char* const toTranscode):%s \n",toTranscode);//printf("transcode handle=%x\n",fConverter->fIconv390DescriptorFrom);   if (!toTranscode)      return 0;   XMLCh* retVal = 0;   const unsigned int len = strlen(toTranscode);   retVal = new XMLCh[len + 1]; // +1 is for the null terminator!   if (len == 0) {      retVal[0] = 0;      return retVal;   }   int retCode;   char *tmpInPtr = (char*) toTranscode;   char *tmpOutPtr = (char*) retVal;   unsigned int inByteLeft = len;   unsigned int outByteLeft = len*sizeof(XMLCh);   { // locking scope      XMLMutexLock lockConverter(&fConverter->fMutex);      retCode = uniconv(fConverter->fIconv390DescriptorFrom, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);   }   // Because we check the length in the beginning, and we make sure the output buffer   // is big enough, uniconv should complete the transcoding. If it doesn't for any reason, then   // return failure.   if (retCode < 0) {      delete [] retVal;      return 0;   }   *tmpOutPtr = 0x00;   *(tmpOutPtr+1) = 0x00;   return retVal;}XMLCh* Uniconv390LCPTranscoder::transcode(const char* const toTranscode,                                          MemoryManager* const manager){DBGPRINTF2("Uniconv390LCPTranscoder::transcode(const char* const toTranscode):%s \n",toTranscode);//printf("transcode handle=%x\n",fConverter->fIconv390DescriptorFrom);   if (!toTranscode)      return 0;   XMLCh* retVal = 0;   const unsigned int len = strlen(toTranscode);   retVal = (XMLCh*) manager->allocate((len + 1) * sizeof(XMLCh));//new XMLCh[len + 1]; // +1 is for the null terminator!   if (len == 0) {      retVal[0] = 0;      return retVal;   }   int retCode;   char *tmpInPtr = (char*) toTranscode;   char *tmpOutPtr = (char*) retVal;   unsigned int inByteLeft = len;   unsigned int outByteLeft = len*sizeof(XMLCh);   { // locking scope      XMLMutexLock lockConverter(&fConverter->fMutex);      retCode = uniconv(fConverter->fIconv390DescriptorFrom, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);   }   // Because we check the length in the beginning, and we make sure the output buffer   // is big enough, uniconv should complete the transcoding. If it doesn't for any reason, then   // return failure.   if (retCode < 0) {      manager->deallocate(retVal);//delete [] retVal;      return 0;   }   *tmpOutPtr = 0x00;   *(tmpOutPtr+1) = 0x00;   return retVal;}bool Uniconv390LCPTranscoder::transcode(const  char* const     toTranscode                                ,       XMLCh* const    toFill                                , const unsigned int    maxChars                                , MemoryManager* const  manager){DBGPRINTF1("Uniconv390LCPTranscoder::transcode(const  char* const     toTranscode, etc.... \n");//printf("transcode handle=%x\n",fConverter->fIconv390DescriptorFrom);    // Check for a couple of psycho corner cases   if (!toTranscode || !maxChars) {      toFill[0] = 0;      return true;   }   unsigned int  Lent = strlen(toTranscode);   if (Lent == 0) {      toFill[0] = 0;      return true;   }   int retCode;   char *tmpInPtr = (char*) toTranscode;   char *tmpOutPtr = (char*) toFill;   unsigned int inByteLeft = Lent;   unsigned int outByteLeft = maxChars*2;   { // locking scope      XMLMutexLock lockConverter(&fConverter->fMutex);      retCode = uniconv(fConverter->fIconv390DescriptorFrom, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);   }   // Because we check the length in the beginning, and the caller makes sure that the output buffer   // is big enough, uniconv should complete the transcoding. If it doesn't for any reason, then   // return failure.   if (retCode < 0) {      return false;   }   *tmpOutPtr = 0x00;   *(tmpOutPtr+1) = 0x00;   return true;}bool Uniconv390LCPTranscoder::transcode(   const   XMLCh* const    toTranscode                                    ,       char* const     toFill                                    , const unsigned int    maxBytes                                    , MemoryManager* const  manager){DBGPRINTF1("Uniconv390LCPTranscoder::transcode(const  XMLCh* const     toTranscode, etc.... \n");//printf("transcode handle=%x\n",fConverter->fIconv390DescriptorTo);   // Watch for a couple of pyscho corner cases   if (!toTranscode || !maxBytes) {      toFill[0] = 0;      return true;   }   //-------------------   unsigned int  wLent = getWideCharLength(toTranscode);   if (wLent == 0) {      toFill[0] = 0;      return true;   }   int retCode;   char *tmpInPtr = (char*) toTranscode;   char *tmpOutPtr = (char*) toFill;   unsigned int inByteLeft = wLent*sizeof(XMLCh);   unsigned int outByteLeft = maxBytes;   { // locking scope      XMLMutexLock lockConverter(&fConverter->fMutex);      retCode = uniconv(fConverter->fIconv390DescriptorTo, &tmpInPtr, &inByteLeft, &tmpOutPtr, &outByteLeft);   }   // Because we check the length in the beginning, and the caller makes sure that the output buffer   // is big enough, uniconv should complete the transcoding. If it doesn't for any reason, then   // return failure.   if (retCode < 0) {      return false;   }   *tmpOutPtr = 0x00;   return true;}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?