domstring.cpp

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

CPP
1,182
字号
};void DOMString::insertData(unsigned int offset, const DOMString &src){    unsigned int origStrLength = this->length();    if (offset > origStrLength)        throw DOM_DOMException(DOM_DOMException::INDEX_SIZE_ERR, 0);    if (fHandle == 0)    {        *this = src.clone();        return;    }    if (src.fHandle == 0 || src.fHandle->fLength == 0)        return;    XMLCh *srcP = src.fHandle->fDSData->fData;    unsigned int srcLength = src.fHandle->fLength;    unsigned int newLength = fHandle->fLength + srcLength;    if (newLength >= fHandle->fDSData->fBufferLength ||        fHandle->fDSData->fRefCount > 1  || fHandle == src.fHandle )    {        // We can't stick the data to be added into the        //  existing string, either because there is not space in        //  the buffer, or because the buffer is being shared with        //  some other string.        //  So, make a new buffer.        DOMStringData *newBuf = DOMStringData::allocateBuffer(newLength);        XMLCh *newP  = newBuf->fData;        XMLCh *oldP   = fHandle->fDSData->fData;        unsigned int i;        for (i=0; i<offset; ++i)            newP[i] = oldP[i];        for (i=0; i<srcLength; i++)            newP[i+offset] = srcP[i];        for (i=offset; i<origStrLength; i++)            newP[i+srcLength] = oldP[i];        fHandle->fDSData->removeRef();        fHandle->fDSData = newBuf;    }    else    {        // There is room in the already-existing buffer to hold        //  the data to be inserted.  Insert it.        //        XMLCh *destP = fHandle->fDSData->fData;        int i;        for (i=(int)origStrLength-1; i>=(int)offset; i--)            destP[i+srcLength] = destP[i];        unsigned int j;        for (j=0; j<srcLength; j++)            destP[j+offset] = srcP[j];    };    fHandle->fLength += srcLength;}unsigned int DOMString::length() const{    unsigned int len = 0;    if (fHandle != 0)        len = fHandle->fLength;    return len;};void DOMString::print() const{    unsigned int len = this->length();    if (len > 0)    {        // Transcode from Unicode to char * in whatever the system local code page is.        char *pc = transcode(XMLPlatformUtils::fgMemoryManager);        fputs(pc, stdout);        XMLPlatformUtils::fgMemoryManager->deallocate(pc);//delete [] pc;    }};void DOMString::println() const{	print();    putchar('\n');};const XMLCh *DOMString::rawBuffer() const{    XMLCh  *retP = 0;    if (fHandle)    {        retP = fHandle->fDSData->fData;    }    return retP;};char *DOMString::transcode() const{    if (!fHandle || fHandle->fLength == 0)    {        char* retP = new char[1];        *retP = 0;        return retP;    }    // We've got some data    // DOMStrings are not always null terminated, so we may need to    // copy to another buffer first in order to null terminate it for    // use as input to the transcoding routines..    //    XMLCh* DOMStrData = fHandle->fDSData->fData;    const unsigned int localBufLen = 1000;    XMLCh localBuf[localBufLen];    XMLCh *allocatedBuf = 0;    XMLCh *srcP;    if (DOMStrData[fHandle->fLength] == 0)    {        // The data in the DOMString itself happens to be null terminated.        //  Just use it in place.        srcP = DOMStrData;    }    else if (fHandle->fLength < localBufLen-1)    {        // The data is not null terminated, but does fit in the        //  local buffer (fast allocation).  Copy it over, and add        //  the null termination,        memcpy(localBuf, DOMStrData, fHandle->fLength * sizeof(XMLCh));        srcP = localBuf;        srcP[fHandle->fLength] = 0;    }    else    {        // The data is too big for the local buffer.  Heap allocate one.        allocatedBuf = srcP = new XMLCh[fHandle->fLength + 1];        memcpy(allocatedBuf, DOMStrData, fHandle->fLength * sizeof(XMLCh));        srcP[fHandle->fLength] = 0;    }    //    //  Find out how many output chars we need and allocate a buffer big enough    //  for that plus a null.    //    //  The charsNeeded normally is same as fHandle->fLength.  To enhance performance,    //  we start with this estimate, and if overflow, then call calcRequiredSize for actual size    unsigned int charsNeeded = fHandle->fLength;    char* retP = new char[charsNeeded + 1];    if (!getDomConverter()->transcode(srcP, retP, charsNeeded) || (XMLString::stringLen(retP) != charsNeeded))    {        delete [] retP;        charsNeeded = getDomConverter()->calcRequiredSize(srcP);        retP = new char[charsNeeded + 1];        if (!getDomConverter()->transcode(srcP, retP, charsNeeded))        {            // <TBD> We should throw something here?        }    }    delete [] allocatedBuf;   // which will be null if we didn't allocate one.    // Cap it off and return it    retP[charsNeeded] = 0;    return retP;}char *DOMString::transcode(MemoryManager* const manager) const{    if (!fHandle || fHandle->fLength == 0)    {        char* retP = (char*) manager->allocate(sizeof(char));//new char[1];        *retP = 0;        return retP;    }    // We've got some data    // DOMStrings are not always null terminated, so we may need to    // copy to another buffer first in order to null terminate it for    // use as input to the transcoding routines..    //    XMLCh* DOMStrData = fHandle->fDSData->fData;    const unsigned int localBufLen = 1000;    XMLCh localBuf[localBufLen];    XMLCh *allocatedBuf = 0;    XMLCh *srcP;    if (DOMStrData[fHandle->fLength] == 0)    {        // The data in the DOMString itself happens to be null terminated.        //  Just use it in place.        srcP = DOMStrData;    }    else if (fHandle->fLength < localBufLen-1)    {        // The data is not null terminated, but does fit in the        //  local buffer (fast allocation).  Copy it over, and add        //  the null termination,        memcpy(localBuf, DOMStrData, fHandle->fLength * sizeof(XMLCh));        srcP = localBuf;        srcP[fHandle->fLength] = 0;    }    else    {        // The data is too big for the local buffer.  Heap allocate one.        allocatedBuf = srcP = (XMLCh*) manager->allocate        (            (fHandle->fLength + 1) * sizeof(XMLCh)        );//new XMLCh[fHandle->fLength + 1];        memcpy(allocatedBuf, DOMStrData, fHandle->fLength * sizeof(XMLCh));        srcP[fHandle->fLength] = 0;    }    //    //  Find out how many output chars we need and allocate a buffer big enough    //  for that plus a null.    //    //  The charsNeeded normally is same as fHandle->fLength.  To enhance performance,    //  we start with this estimate, and if overflow, then call calcRequiredSize for actual size    unsigned int charsNeeded = fHandle->fLength;    char* retP = (char*) manager->allocate((charsNeeded + 1) * sizeof(char));//new char[charsNeeded + 1];    if (!getDomConverter()->transcode(srcP, retP, charsNeeded) || (XMLString::stringLen(retP) != charsNeeded))    {        manager->deallocate(retP);//delete [] retP;        charsNeeded = getDomConverter()->calcRequiredSize(srcP);        retP = (char*) manager->allocate((charsNeeded + 1) * sizeof(char));//new char[charsNeeded + 1];        if (!getDomConverter()->transcode(srcP, retP, charsNeeded))        {            // <TBD> We should throw something here?        }    }    if (allocatedBuf)	    manager->deallocate(allocatedBuf);//delete [] allocatedBuf;   // which will be null if we didn't allocate one.    // Cap it off and return it    retP[charsNeeded] = 0;    return retP;}DOMString DOMString::transcode(const char* str){    return DOMString(str);}int DOMString::compareString(const DOMString &other) const{    // Note: this strcmp does not match the semantics    //       of the standard C strcmp.  All it needs to do is    //       define some less than - equals - greater than ordering    //       of strings.  How doesn't matter.    //    unsigned int thisLen = length();    unsigned int otherLen = other.length();    if (thisLen < otherLen)        return -1;    if (thisLen > otherLen)        return 1;    if (thisLen == 0)        return 0;    XMLCh *thisP =  this->fHandle->fDSData->fData;    XMLCh *otherP = other.fHandle->fDSData->fData;    unsigned int i;    for (i=0; i<thisLen; i++)    {        if (thisP[i] < otherP[i])            return -1;        else if (thisP[i] > otherP[i])            return 1;    };    return 0;};DOMString DOMString::substringData(unsigned int offset, unsigned int count) const{    unsigned int thisLen = length();    if (offset > thisLen || offset < 0 || count < 0)        throw DOM_DOMException(DOM_DOMException::INDEX_SIZE_ERR, 0);    // Cap count to the string length to eliminate overflow    //  problems when we get passed bogus values, like -1.    if (count > thisLen)        count = thisLen;    // If the count extends past the end of the string, cut it    //   back so that the returned string will stop at the end    //   of the source string.    if (offset + count >= thisLen)        count = thisLen - offset;    if (count == 0)        return DOMString();    // If the substring starts at the beginning of the original string    //   we do not need to copy the data, but can set up a new    //   string handle with the shorter length.    if (offset == 0)    {        DOMString retString = this->clone();        retString.fHandle->fLength = count;        return retString;    };    // The substring starts somewhere in the interior of the orignal string.    // Create a completely new DOMString.  No buffer sharing is possible.    XMLCh *data = fHandle->fDSData->fData;    return DOMString(data+offset, count);};DOMString operator + (const DOMString &lhs, const DOMString &rhs){    DOMString retString = lhs.clone();    retString.appendData(rhs);    return retString;}DOMString operator + (const DOMString &lhs, const XMLCh* rhs){    DOMString retString = lhs.clone();    retString.appendData(rhs);    return retString;}DOMString operator + (const XMLCh* lhs, const DOMString& rhs){    DOMString retString = DOMString(lhs);    retString.appendData(rhs);    return retString;}DOMString operator + (const DOMString &lhs, XMLCh rhs){    DOMString retString = lhs.clone();    retString.appendData(rhs);    return retString;}DOMString operator + (XMLCh lhs, const DOMString& rhs){    DOMString retString;	retString.appendData(lhs);    retString.appendData(rhs);    return retString;}// -----------------------------------------------------------------------//  Notification that lazy data has been deleted// -----------------------------------------------------------------------static void reinitDomConverter(){        delete gDomConverter;           //  Delete the local code page converter.        gDomConverter = 0;};static void reinitDomMutex(){        delete DOMStringHandleMutex;    //  Delete the synchronization mutex,        DOMStringHandleMutex = 0;};XERCES_CPP_NAMESPACE_END

⌨️ 快捷键说明

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