iconvfbsdtransservice.cpp

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

CPP
1,778
字号
void IconvFBSDTransService::lowerCase(XMLCh* const toLowerCase) const{    XMLCh* outPtr = toLowerCase;    while (*outPtr)    {#ifndef XML_USE_LIBICONV        *outPtr = fbsd_towlower(*outPtr);#else /* XML_USE_LIBICONV */        *outPtr = toLower(*outPtr);#endif /* !XML_USE_LIBICONV */        outPtr++;    }}// ---------------------------------------------------------------------------//  IconvFBSDLCPTranscoder: The virtual transcoder API// ---------------------------------------------------------------------------unsigned intIconvFBSDLCPTranscoder::calcRequiredSize (const char* const srcText                                          , MemoryManager* const manager){    if (!srcText)        return 0;#ifndef XML_USE_LIBICONV    unsigned int retVal = fbsd_mbstowcs(NULL, srcText, 0);    if (retVal == ~0)        return 0;    return retVal;#else /* XML_USE_LIBICONV */    size_t      len, srcLen;    len = srcLen = strlen(srcText);    if (len == 0)        return 0;    char    tmpWideArr[gTempBuffArraySize];    size_t    totalLen = 0;    for (;;) {        char        *pTmpArr = tmpWideArr;        const char    *ptr = srcText + srcLen - len;        size_t    rc = iconvFrom(ptr, &len, &pTmpArr, gTempBuffArraySize);        if (rc == (size_t) -1 && errno != E2BIG) {            ThrowXMLwithMemMgr(TranscodingException, XMLExcepts::Trans_BadSrcSeq, manager);            /* return 0; */        }        rc = pTmpArr - (char *) tmpWideArr;        totalLen += rc;        if (rc == 0 || len == 0)            break;    }    return totalLen / uChSize();#endif /* XML_USE_LIBICONV */}unsigned intIconvFBSDLCPTranscoder::calcRequiredSize(const XMLCh* const srcText                                         , MemoryManager* const manager){    if (!srcText)        return 0;    unsigned int  wLent = getWideCharLength(srcText);    if (wLent == 0)        return 0;#ifndef XML_USE_LIBICONV    wchar_t       tmpWideCharArr[gTempBuffArraySize];    wchar_t*      allocatedArray = 0;    wchar_t*      wideCharBuf = 0;    if (wLent >= gTempBuffArraySize)        wideCharBuf = allocatedArray = (wchar_t*) manager->allocate        (            (wLent + 1) * sizeof(wchar_t)        );//new wchar_t[wLent + 1];    else        wideCharBuf = tmpWideCharArr;    for (unsigned int i = 0; i < wLent; i++)        wideCharBuf[i] = srcText[i];    wideCharBuf[wLent] = 0x00;    const unsigned int retVal = fbsd_wcstombs(NULL, wideCharBuf, 0);    if (allocatedArray)        manager->deallocate(allocatedArray);//delete [] allocatedArray;    if (retVal == ~0)        return 0;    return retVal;#else /* XML_USE_LIBICONV */    char    tmpWBuff[gTempBuffArraySize];    char    *wBuf = 0;    char    *wBufPtr = 0;    size_t      len = wLent * uChSize();    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {        if (len > gTempBuffArraySize) {            wBufPtr = (char*) manager->allocate            (                len * sizeof(char)            );//new char[len];            if (wBufPtr == NULL)            return 0;            wBuf = wBufPtr;        } else            wBuf = tmpWBuff;        xmlToMbs (srcText, wLent, wBuf, wLent);    } else    wBuf = (char *) srcText;    char    tmpBuff[gTempBuffArraySize];    size_t    totalLen = 0;    char    *srcEnd = wBuf + wLent * uChSize();    for (;;) {        char        *pTmpArr = tmpBuff;        const char    *ptr = srcEnd - len;        size_t    rc = iconvTo(ptr, &len, &pTmpArr, gTempBuffArraySize);        if (rc == (size_t) -1 && errno != E2BIG) {            if (wBufPtr)            manager->deallocate(wBufPtr);//delete [] wBufPtr;            ThrowXMLwithMemMgr(TranscodingException, XMLExcepts::Trans_BadSrcSeq, manager);            /* return 0; */        }        rc = pTmpArr - tmpBuff;        totalLen += rc;        if (rc == 0 || len == 0)            break;    }    if (wBufPtr)        manager->deallocate(wBufPtr);//delete [] wBufPtr;    return totalLen;#endif /* !XML_USE_LIBICONV */}char* IconvFBSDLCPTranscoder::transcode(const XMLCh* const toTranscode){    if (!toTranscode)        return 0;    char* retVal = 0;    if (*toTranscode) {        unsigned int  wLent = getWideCharLength(toTranscode);#ifndef XML_USE_LIBICONV        wchar_t       tmpWideCharArr[gTempBuffArraySize];        wchar_t*      allocatedArray = 0;        wchar_t*      wideCharBuf = 0;        if (wLent >= gTempBuffArraySize)            wideCharBuf = allocatedArray = new wchar_t[wLent + 1];        else            wideCharBuf = tmpWideCharArr;        for (unsigned int i = 0; i < wLent; i++)            wideCharBuf[i] = toTranscode[i];        wideCharBuf[wLent] = 0x00;        // Calc the needed size.        const size_t neededLen = fbsd_wcstombs(NULL, wideCharBuf, 0);        if (neededLen == -1) {            if (allocatedArray)                delete [] allocatedArray;            return 0;        }        retVal = new char[neededLen + 1];        fbsd_wcstombs(retVal, wideCharBuf, neededLen);        if (allocatedArray)            delete [] allocatedArray;        retVal[neededLen] = 0;#else /* XML_USE_LIBICONV */        // Calc needed size.        const size_t neededLen = calcRequiredSize (toTranscode);        if (neededLen == 0)            return 0;        // allocate output buffer        retVal = new char[neededLen + 1];        if (retVal == NULL)            return 0;        // prepare the original        char    tmpWBuff[gTempBuffArraySize];        char    *wideCharBuf = 0;        char    *wBufPtr = 0;        size_t  len = wLent * uChSize();        if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {            if (len > gTempBuffArraySize) {            wBufPtr = new char[len];            if (wBufPtr == NULL)                return 0;            wideCharBuf = wBufPtr;            } else            wideCharBuf = tmpWBuff;            xmlToMbs (toTranscode, wLent, wideCharBuf, wLent);        } else            wideCharBuf = (char *) toTranscode;        // perform conversion        wLent *= uChSize();        char    *ptr = retVal;        size_t  tmpwLent = wLent;        size_t  rc = iconvTo(wideCharBuf, &tmpwLent, &ptr, neededLen);        if (rc == (size_t)-1) {            if (wBufPtr)            delete [] wBufPtr;            return 0;        }        if (wBufPtr)            delete [] wBufPtr;        retVal[neededLen] = 0;#endif /* !XML_USE_LIBICONV */    } else {        retVal = new char[1];        if (retVal == NULL)            return 0;        retVal[0] = 0;    }    return retVal;}char* IconvFBSDLCPTranscoder::transcode(const XMLCh* const toTranscode,                                        MemoryManager* const manager){    if (!toTranscode)        return 0;    char* retVal = 0;    if (*toTranscode) {        unsigned int  wLent = getWideCharLength(toTranscode);#ifndef XML_USE_LIBICONV        wchar_t       tmpWideCharArr[gTempBuffArraySize];        wchar_t*      allocatedArray = 0;        wchar_t*      wideCharBuf = 0;        if (wLent >= gTempBuffArraySize)            wideCharBuf = allocatedArray = (wchar_t*) manager->allocate            (                (wLent + 1) * sizeof(wchar_t)            );//new wchar_t[wLent + 1];        else            wideCharBuf = tmpWideCharArr;        for (unsigned int i = 0; i < wLent; i++)            wideCharBuf[i] = toTranscode[i];        wideCharBuf[wLent] = 0x00;        // Calc the needed size.        const size_t neededLen = fbsd_wcstombs(NULL, wideCharBuf, 0);        if (neededLen == -1) {            if (allocatedArray)                manager->deallocate(allocatedArray);//delete [] allocatedArray;            return 0;        }        retVal = (char*) manager->allocate((neededLen + 1) * sizeof(char));//new char[neededLen + 1];        fbsd_wcstombs(retVal, wideCharBuf, neededLen);        if (allocatedArray)            manager->deallocate(allocatedArray);//delete [] allocatedArray;        retVal[neededLen] = 0;#else /* XML_USE_LIBICONV */        // Calc needed size.        const size_t neededLen = calcRequiredSize (toTranscode, manager);        if (neededLen == 0)            return 0;        // allocate output buffer        retVal = (char*) manager->allocate((neededLen + 1) * sizeof(char));//new char[neededLen + 1];        if (retVal == NULL)            return 0;        // prepare the original        char    tmpWBuff[gTempBuffArraySize];        char    *wideCharBuf = 0;        char    *wBufPtr = 0;        size_t  len = wLent * uChSize();        if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {            if (len > gTempBuffArraySize) {            wBufPtr = (char*) manager->allocate(len * sizeof(char));//new char[len];            if (wBufPtr == NULL) {                manager->deallocate(retVal);                return 0;            }            wideCharBuf = wBufPtr;            } else            wideCharBuf = tmpWBuff;            xmlToMbs (toTranscode, wLent, wideCharBuf, wLent);        } else            wideCharBuf = (char *) toTranscode;        // perform conversion        wLent *= uChSize();        char    *ptr = retVal;        size_t  tmpwLent = wLent;        size_t  rc = iconvTo(wideCharBuf, &tmpwLent, &ptr, neededLen);        if (rc == (size_t)-1) {            if (wBufPtr)                manager->deallocate(wBufPtr);//delete [] wBufPtr;            return 0;        }        if (wBufPtr)            manager->deallocate(wBufPtr);//delete [] wBufPtr;        retVal[neededLen] = 0;#endif /* !XML_USE_LIBICONV */    } else {        retVal = (char*) manager->allocate(sizeof(char));//new char[1];        if (retVal == NULL)            return 0;        retVal[0] = 0;    }    return retVal;}bool IconvFBSDLCPTranscoder::transcode( const   XMLCh* const    toTranscode                    , char* const        toFill                    , const unsigned int    maxBytes                    , MemoryManager* const  manager){    // Watch for a couple of pyscho corner cases    if (!toTranscode || !maxBytes) {        toFill[0] = 0;        return true;    }    if (!*toTranscode) {        toFill[0] = 0;        return true;    }    unsigned int  wLent = getWideCharLength(toTranscode);    if (wLent > maxBytes)        wLent = maxBytes;#ifndef XML_USE_LIBICONV    wchar_t       tmpWideCharArr[gTempBuffArraySize];    wchar_t*      allocatedArray = 0;    wchar_t*      wideCharBuf = 0;    if (maxBytes >= gTempBuffArraySize)        wideCharBuf = allocatedArray = (wchar_t*) manager->allocate        (            (maxBytes + 1) * sizeof(wchar_t)        );//new wchar_t[maxBytes + 1];    else        wideCharBuf = tmpWideCharArr;    for (unsigned int i = 0; i < wLent; i++)        wideCharBuf[i] = toTranscode[i];    wideCharBuf[wLent] = 0x00;    // Ok, go ahead and try the transcoding. If it fails, then ...    size_t mblen = fbsd_wcstombs(toFill, wideCharBuf, maxBytes);    if (mblen == -1) {        if (allocatedArray)            manager->deallocate(allocatedArray);//delete [] allocatedArray;        return false;    }    if (allocatedArray)        manager->deallocate(allocatedArray);//delete [] allocatedArray;    // Cap it off just in case    toFill[mblen] = 0;#else /* XML_USE_LIBICONV */    // Fill the "unicode" string    char    tmpWBuff[gTempBuffArraySize];    char    *wideCharBuf = 0;    char    *wBufPtr = 0;    size_t  len = wLent * uChSize();    if (uChSize() != sizeof(XMLCh) || UBO() != BYTE_ORDER) {        if (len > gTempBuffArraySize) {            wBufPtr = (char*) manager->allocate            (                len * sizeof(char)            );//new char[len];            if (wBufPtr == NULL)                return 0;            wideCharBuf = wBufPtr;         }         else             wideCharBuf = tmpWBuff;             xmlToMbs (toTranscode, wLent, wideCharBuf, wLent);     }     else         wideCharBuf = (char *) toTranscode;    // Ok, go ahead and try the transcoding. If it fails, then ...    char    *ptr = toFill;    size_t rc = iconvTo(wideCharBuf, &len, &ptr, maxBytes);    if (rc == (size_t)-1) {        if (wBufPtr)           manager->deallocate(wBufPtr);//delete [] wBufPtr;        return false;    }    if (wBufPtr)        manager->deallocate(wBufPtr);//delete [] wBufPtr;    *ptr = 0;#endif /* !XML_USE_LIBICONV */    return true;}XMLCh* IconvFBSDLCPTranscoder::transcode(const char* const toTranscode){    if (!toTranscode)        return 0;    XMLCh* retVal = 0;    if (*toTranscode) {        const unsigned int wLent = calcRequiredSize(toTranscode);        if (wLent == 0) {            retVal = new XMLCh[1];            retVal[0] = 0;            return retVal;        }#ifndef XML_USE_LIBICONV        wchar_t       tmpWideCharArr[gTempBuffArraySize];        wchar_t*      allocatedArray = 0;        wchar_t*      wideCharBuf = 0;

⌨️ 快捷键说明

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