iconvtransservice.cpp

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

CPP
585
字号
        }        wideCharBuf[wLent] = 0x00;        // Calc the needed size.        const size_t neededLen = ::wcstombs(NULL, wideCharBuf, 0);        if (neededLen == -1)        {            delete [] allocatedArray;            retVal = new char[1];            retVal[0] = 0;            return retVal;        }        retVal = new char[neededLen + 1];        ::wcstombs(retVal, wideCharBuf, neededLen);        retVal[neededLen] = 0;        delete [] allocatedArray;    }    else    {        retVal = new char[1];        retVal[0] = 0;    }    return retVal;}char* IconvLCPTranscoder::transcode(const XMLCh* const toTranscode,                                    MemoryManager* const manager){    if (!toTranscode)        return 0;    char* retVal = 0;    if (*toTranscode)    {        unsigned int  wLent = getWideCharLength(toTranscode);        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 = ::wcstombs(NULL, wideCharBuf, 0);        if (neededLen == -1)        {            manager->deallocate(allocatedArray);//delete [] allocatedArray;            retVal = (char*) manager->allocate(sizeof(char)); //new char[1];            retVal[0] = 0;            return retVal;        }        retVal = (char*) manager->allocate((neededLen + 1) * sizeof(char));//new char[neededLen + 1];        ::wcstombs(retVal, wideCharBuf, neededLen);        retVal[neededLen] = 0;        manager->deallocate(allocatedArray);//delete [] allocatedArray;    }    else    {        retVal = (char*) manager->allocate(sizeof(char));//new char[1];        retVal[0] = 0;    }    return retVal;}bool IconvLCPTranscoder::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);    wchar_t       tmpWideCharArr[gTempBuffArraySize];    wchar_t*      allocatedArray = 0;    wchar_t*      wideCharBuf = 0;    if (wLent > maxBytes) {        wLent = maxBytes;    }    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 = ::wcstombs(toFill, wideCharBuf, maxBytes);    if (mblen == -1)    {        manager->deallocate(allocatedArray);//delete [] allocatedArray;        return false;    }    // Cap it off just in case    toFill[mblen] = 0;    manager->deallocate(allocatedArray);//delete [] allocatedArray;    return true;}XMLCh* IconvLCPTranscoder::transcode(const char* const toTranscode){    if (!toTranscode)        return 0;    XMLCh* retVal = 0;    if (*toTranscode)    {        const unsigned int len = calcRequiredSize(toTranscode);        if (len == 0)        {            retVal = new XMLCh[1];            retVal[0] = 0;            return retVal;        }        wchar_t       tmpWideCharArr[gTempBuffArraySize];        wchar_t*      allocatedArray = 0;        wchar_t*      wideCharBuf = 0;        if (len >= gTempBuffArraySize)            wideCharBuf = allocatedArray = new wchar_t[len + 1];        else            wideCharBuf = tmpWideCharArr;        ::mbstowcs(wideCharBuf, toTranscode, len);        retVal = new XMLCh[len + 1];        for (unsigned int i = 0; i < len; i++)        {            retVal[i] = (XMLCh) wideCharBuf[i];        }        retVal[len] = 0x00;        delete [] allocatedArray;    }    else    {        retVal = new XMLCh[1];        retVal[0] = 0;    }    return retVal;}XMLCh* IconvLCPTranscoder::transcode(const char* const toTranscode,                                     MemoryManager* const manager){    if (!toTranscode)        return 0;    XMLCh* retVal = 0;    if (*toTranscode)    {        const unsigned int len = calcRequiredSize(toTranscode, manager);        if (len == 0)        {            retVal = (XMLCh*) manager->allocate(sizeof(XMLCh)); //new XMLCh[1];            retVal[0] = 0;            return retVal;        }        wchar_t       tmpWideCharArr[gTempBuffArraySize];        wchar_t*      allocatedArray = 0;        wchar_t*      wideCharBuf = 0;        if (len >= gTempBuffArraySize)            wideCharBuf = allocatedArray = (wchar_t*) manager->allocate            (                (len + 1) * sizeof(wchar_t)            );//new wchar_t[len + 1];        else            wideCharBuf = tmpWideCharArr;        ::mbstowcs(wideCharBuf, toTranscode, len);        retVal = (XMLCh*) manager->allocate((len + 1) *sizeof(XMLCh));//new XMLCh[len + 1];        for (unsigned int i = 0; i < len; i++)        {            retVal[i] = (XMLCh) wideCharBuf[i];        }        retVal[len] = 0x00;        manager->deallocate(allocatedArray);//delete [] allocatedArray;    }    else    {        retVal = (XMLCh*) manager->allocate(sizeof(XMLCh));//new XMLCh[1];        retVal[0] = 0;    }    return retVal;}bool IconvLCPTranscoder::transcode( const   char* const     toTranscode                                    ,       XMLCh* const    toFill                                    , const unsigned int    maxChars                                    , MemoryManager* const  manager){    // Check for a couple of psycho corner cases    if (!toTranscode || !maxChars)    {        toFill[0] = 0;        return true;    }    if (!*toTranscode)    {        toFill[0] = 0;        return true;    }    unsigned int len = calcRequiredSize(toTranscode);    wchar_t       tmpWideCharArr[gTempBuffArraySize];    wchar_t*      allocatedArray = 0;    wchar_t*      wideCharBuf = 0;    if (len > maxChars) {        len = maxChars;    }    if (maxChars >= gTempBuffArraySize)        wideCharBuf = allocatedArray = (wchar_t*) manager->allocate        (            (maxChars + 1) * sizeof(wchar_t)        );//new wchar_t[maxChars + 1];    else        wideCharBuf = tmpWideCharArr;    if (::mbstowcs(wideCharBuf, toTranscode, maxChars) == -1)    {        manager->deallocate(allocatedArray);//delete [] allocatedArray;        return false;    }    for (unsigned int i = 0; i < len; i++)    {        toFill[i] = (XMLCh) wideCharBuf[i];    }    toFill[len] = 0x00;    manager->deallocate(allocatedArray);//delete [] allocatedArray;    return true;}// ---------------------------------------------------------------------------//  IconvLCPTranscoder: Constructors and Destructor// ---------------------------------------------------------------------------IconvLCPTranscoder::IconvLCPTranscoder(){}IconvLCPTranscoder::~IconvLCPTranscoder(){}XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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