⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mitab_rawbinblock.cpp

📁 GIS系统支持库Geospatial Data Abstraction Library代码.GDAL is a translator library for raster geospatial dat
💻 CPP
📖 第 1 页 / 共 2 页
字号:
    m_nSizeUsed = MAX(m_nSizeUsed, m_nCurPos);    return 0;}/********************************************************************** *                   TABRawBinBlock::SetFirstBlockPtr() * * Set the position in the file at which the first block starts. * This value will usually be the header size and needs to be specified  * only if the header size is different from the other blocks size.  * * This value will be used by GotoByteInFile() to properly align the data * blocks that it loads automatically when a requested position is outside * of the block currently in memory. **********************************************************************/void  TABRawBinBlock::SetFirstBlockPtr(int nOffset){    m_nFirstBlockPtr = nOffset;}/********************************************************************** *                   TABRawBinBlock::GetNumUnusedBytes() * * Return the number of unused bytes in this block. **********************************************************************/int     TABRawBinBlock::GetNumUnusedBytes(){    return (m_nBlockSize - m_nSizeUsed);}/********************************************************************** *                   TABRawBinBlock::GetFirstUnusedByteOffset() * * Return the position of the first unused byte in this block relative * to the beginning of the file, or -1 if the block is full. **********************************************************************/int     TABRawBinBlock::GetFirstUnusedByteOffset(){    if (m_nSizeUsed < m_nBlockSize)        return m_nFileOffset + m_nSizeUsed;    else        return -1;}/********************************************************************** *                   TABRawBinBlock::GetCurAddress() * * Return the current pointer position, relative to beginning of file. **********************************************************************/int     TABRawBinBlock::GetCurAddress(){    return (m_nFileOffset + m_nCurPos);}/********************************************************************** *                   TABRawBinBlock::ReadBytes() * * Copy the number of bytes from the data block's internal buffer to * the user's buffer pointed by pabyDstBuf. * * Passing pabyDstBuf = NULL will only move the read pointer by the * specified number of bytes as if the copy had happened... but it  * won't crash. * * Returns 0 if succesful or -1 if an error happened, in which case  * CPLError() will have been called. **********************************************************************/int     TABRawBinBlock::ReadBytes(int numBytes, GByte *pabyDstBuf){    /*----------------------------------------------------------------     * Make sure block is initialized with Read access and that the     * operation won't go beyond the buffer's size.     *---------------------------------------------------------------*/    if (m_pabyBuf == NULL)    {        CPLError(CE_Failure, CPLE_AppDefined,                 "ReadBytes(): Block has not been initialized.");        return -1;    }    if (m_eAccess != TABRead && m_eAccess != TABReadWrite )    {        CPLError(CE_Failure, CPLE_AppDefined,                 "ReadBytes(): Block does not support read operations.");        return -1;    }    if (m_nCurPos + numBytes > m_nSizeUsed)    {        CPLError(CE_Failure, CPLE_AppDefined,                 "ReadBytes(): Attempt to read past end of data block.");        return -1;    }    if (pabyDstBuf)    {        memcpy(pabyDstBuf, m_pabyBuf + m_nCurPos, numBytes);    }    m_nCurPos += numBytes;    return 0;}/********************************************************************** *                   TABRawBinBlock::Read<datatype>() * * MapInfo files are binary files with LSB first (Intel) byte  * ordering.  The following functions will read from the input file * and return a value with the bytes ordered properly for the current  * platform. **********************************************************************/GByte  TABRawBinBlock::ReadByte(){    GByte byValue;    ReadBytes(1, (GByte*)(&byValue));    return byValue;}GInt16  TABRawBinBlock::ReadInt16(){    GInt16 n16Value;    ReadBytes(2, (GByte*)(&n16Value));#ifdef CPL_MSB    return (GInt16)CPL_SWAP16(n16Value);#else    return n16Value;#endif}GInt32  TABRawBinBlock::ReadInt32(){    GInt32 n32Value;    ReadBytes(4, (GByte*)(&n32Value));#ifdef CPL_MSB    return (GInt32)CPL_SWAP32(n32Value);#else    return n32Value;#endif}float   TABRawBinBlock::ReadFloat(){    float fValue;    ReadBytes(4, (GByte*)(&fValue));#ifdef CPL_MSB    *(GUInt32*)(&fValue) = CPL_SWAP32(*(GUInt32*)(&fValue));#endif    return fValue;}double  TABRawBinBlock::ReadDouble(){    double dValue;    ReadBytes(8, (GByte*)(&dValue));#ifdef CPL_MSB    CPL_SWAPDOUBLE(&dValue);#endif    return dValue;}/********************************************************************** *                   TABRawBinBlock::WriteBytes() * * Copy the number of bytes from the user's buffer pointed by pabySrcBuf * to the data block's internal buffer. * Note that this call only writes to the memory buffer... nothing is * written to the file until WriteToFile() is called. * * Passing pabySrcBuf = NULL will only move the write pointer by the * specified number of bytes as if the copy had happened... but it  * won't crash. * * Returns 0 if succesful or -1 if an error happened, in which case  * CPLError() will have been called. **********************************************************************/int  TABRawBinBlock::WriteBytes(int nBytesToWrite, GByte *pabySrcBuf){    /*----------------------------------------------------------------     * Make sure block is initialized with Write access and that the     * operation won't go beyond the buffer's size.     *---------------------------------------------------------------*/    if (m_pabyBuf == NULL)    {        CPLError(CE_Failure, CPLE_AppDefined,                 "WriteBytes(): Block has not been initialized.");        return -1;    }    if (m_eAccess != TABWrite && m_eAccess != TABReadWrite )    {        CPLError(CE_Failure, CPLE_AppDefined,                 "WriteBytes(): Block does not support write operations.");        return -1;    }    if (m_nCurPos + nBytesToWrite > m_nBlockSize)    {        CPLError(CE_Failure, CPLE_AppDefined,                 "WriteBytes(): Attempt to write past end of data block.");        return -1;    }    /*----------------------------------------------------------------     * Everything is OK... copy the data     *---------------------------------------------------------------*/    if (pabySrcBuf)    {        memcpy(m_pabyBuf + m_nCurPos, pabySrcBuf, nBytesToWrite);    }    m_nCurPos += nBytesToWrite;    m_nSizeUsed = MAX(m_nSizeUsed, m_nCurPos);    m_bModified = TRUE;    return 0;}/********************************************************************** *                    TABRawBinBlock::Write<datatype>() * * Arc/Info files are binary files with MSB first (Motorola) byte  * ordering.  The following functions will reorder the byte for the * value properly and write that to the output file. * * If a problem happens, then CPLError() will be called and  * CPLGetLastErrNo() can be used to test if a write operation was  * succesful. **********************************************************************/int  TABRawBinBlock::WriteByte(GByte byValue){    return WriteBytes(1, (GByte*)&byValue);}int  TABRawBinBlock::WriteInt16(GInt16 n16Value){#ifdef CPL_MSB    n16Value = (GInt16)CPL_SWAP16(n16Value);#endif    return WriteBytes(2, (GByte*)&n16Value);}int  TABRawBinBlock::WriteInt32(GInt32 n32Value){#ifdef CPL_MSB    n32Value = (GInt32)CPL_SWAP32(n32Value);#endif    return WriteBytes(4, (GByte*)&n32Value);}int  TABRawBinBlock::WriteFloat(float fValue){#ifdef CPL_MSB    *(GUInt32*)(&fValue) = CPL_SWAP32(*(GUInt32*)(&fValue));#endif    return WriteBytes(4, (GByte*)&fValue);}int  TABRawBinBlock::WriteDouble(double dValue){#ifdef CPL_MSB    CPL_SWAPDOUBLE(&dValue);#endif    return WriteBytes(8, (GByte*)&dValue);}/********************************************************************** *                    TABRawBinBlock::WriteZeros() * * Write a number of zeros (sepcified in bytes) at the current position  * in the file. * * If a problem happens, then CPLError() will be called and  * CPLGetLastErrNo() can be used to test if a write operation was  * succesful. **********************************************************************/int  TABRawBinBlock::WriteZeros(int nBytesToWrite){    char acZeros[8] = {0, 0, 0, 0, 0, 0, 0, 0};    int i;    int nStatus = 0;    /* Write by 8 bytes chunks.  The last chunk may be less than 8 bytes      */    for(i=0; nStatus == 0 && i< nBytesToWrite; i+=8)    {        nStatus = WriteBytes(MIN(8,(nBytesToWrite-i)), (GByte*)acZeros);    }    return nStatus;}/********************************************************************** *                   TABRawBinBlock::WritePaddedString() * * Write a string and pad the end of the field (up to nFieldSize) with * spaces number of spaces at the current position in the file. * * If a problem happens, then CPLError() will be called and  * CPLGetLastErrNo() can be used to test if a write operation was  * succesful. **********************************************************************/int  TABRawBinBlock::WritePaddedString(int nFieldSize, const char *pszString){    char acSpaces[8] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};    int i, nLen, numSpaces;    int nStatus = 0;    nLen = strlen(pszString);    nLen = MIN(nLen, nFieldSize);    numSpaces = nFieldSize - nLen;    if (nLen > 0)        nStatus = WriteBytes(nLen, (GByte*)pszString);    /* Write spaces by 8 bytes chunks.  The last chunk may be less than 8 bytes     */    for(i=0; nStatus == 0 && i< numSpaces; i+=8)    {        nStatus = WriteBytes(MIN(8,(numSpaces-i)), (GByte*)acSpaces);    }    return nStatus;}/********************************************************************** *                   TABRawBinBlock::Dump() * * Dump block contents... available only in DEBUG mode. **********************************************************************/#ifdef DEBUGvoid TABRawBinBlock::Dump(FILE *fpOut /*=NULL*/){    if (fpOut == NULL)        fpOut = stdout;    fprintf(fpOut, "----- TABRawBinBlock::Dump() -----\n");    if (m_pabyBuf == NULL)    {        fprintf(fpOut, "Block has not been initialized yet.");    }    else    {        fprintf(fpOut, "Block (type %d) size=%d bytes at offset %d in file.\n",                m_nBlockType, m_nBlockSize, m_nFileOffset);        fprintf(fpOut, "Current pointer at byte %d\n", m_nCurPos);    }    fflush(fpOut);}#endif // DEBUG/********************************************************************** *                   TABCreateMAPBlockFromFile() * * Load data from the specified file location and create and initialize  * a TABMAP*Block of the right type to handle it. * * Returns the new object if succesful or NULL if an error happened, in  * which case CPLError() will have been called. **********************************************************************/TABRawBinBlock *TABCreateMAPBlockFromFile(FILE *fpSrc, int nOffset,                                           int nSize /*= 512*/,                                           GBool bHardBlockSize /*= TRUE */,                                          TABAccess eAccessMode /*= TABRead*/){    TABRawBinBlock *poBlock = NULL;    GByte *pabyBuf;    if (fpSrc == NULL || nSize == 0)    {        CPLError(CE_Failure, CPLE_AssertionFailed,                  "TABCreateMAPBlockFromFile(): Assertion Failed!");        return NULL;    }    /*----------------------------------------------------------------     * Alloc a buffer to contain the data     *---------------------------------------------------------------*/    pabyBuf = (GByte*)CPLMalloc(nSize*sizeof(GByte));    /*----------------------------------------------------------------     * Read from the file     *---------------------------------------------------------------*/    if (VSIFSeek(fpSrc, nOffset, SEEK_SET) != 0 ||        VSIFRead(pabyBuf, sizeof(GByte), nSize, fpSrc)!=(unsigned int)nSize )    {        CPLError(CE_Failure, CPLE_FileIO,         "TABCreateMAPBlockFromFile() failed reading %d bytes at offset %d.",                 nSize, nOffset);        CPLFree(pabyBuf);        return NULL;    }    /*----------------------------------------------------------------     * Create an object of the right type     * Header block is different: it does not start with the object      * type byte but it is always the first block in a file     *---------------------------------------------------------------*/    if (nOffset == 0)    {        poBlock = new TABMAPHeaderBlock;    }    else    {        switch(pabyBuf[0])        {          case TABMAP_INDEX_BLOCK:            poBlock = new TABMAPIndexBlock(eAccessMode);            break;          case TABMAP_OBJECT_BLOCK:            poBlock = new TABMAPObjectBlock(eAccessMode);            break;          case TABMAP_COORD_BLOCK:            poBlock = new TABMAPCoordBlock(eAccessMode);            break;          case TABMAP_TOOL_BLOCK:            poBlock = new TABMAPToolBlock(eAccessMode);            break;          case TABMAP_GARB_BLOCK:          default:            poBlock = new TABRawBinBlock(eAccessMode, bHardBlockSize);            break;        }    }    /*----------------------------------------------------------------     * Init new object with the data we just read     *---------------------------------------------------------------*/    if (poBlock->InitBlockFromData(pabyBuf, nSize, FALSE, fpSrc, nOffset) != 0)    {        // Some error happened... and CPLError() has been called        delete poBlock;        poBlock = NULL;    }    return poBlock;}

⌨️ 快捷键说明

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