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

📄 tabrawbinblock.cpp

📁 linux下一款GIS程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// TABRawBinBlock.cpp: implementation of the TABRawBinBlock class.////////////////////////////////////////////////////////////////////////#include "tabrawbinblock.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"#include "ugk_environment.h"#include "tabmapheaderblock.h"#include "tabmapindexblock.h"#include "tabmaptoolblock.h"#include "tabmapobjectblock.h"#include "tabmapcoordblock.h"/********************************************************************** *                   TABRawBinBlock::TABRawBinBlock() * * Constructor. **********************************************************************/TABRawBinBlock::TABRawBinBlock(TABAccess eAccessMode /*= TABRead*/,                               UGKBool bHardBlockSize /*= TRUE*/){    m_fp = NULL;    m_pabyBuf = NULL;    m_nFirstBlockPtr = 0;    m_nBlockSize = m_nSizeUsed = m_nFileOffset = m_nCurPos = 0;    m_bHardBlockSize = bHardBlockSize;    m_bModified = FALSE;    m_eAccess = eAccessMode; }/********************************************************************** *                   TABRawBinBlock::~TABRawBinBlock() * * Destructor. **********************************************************************/TABRawBinBlock::~TABRawBinBlock(){    if (m_pabyBuf)        UGK_Free(m_pabyBuf);}/********************************************************************** *                   TABRawBinBlock::ReadFromFile() * * Load data from the specified file location and initialize the block. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::ReadFromFile(FILE *fpSrc, int nOffset,                                      int nSize /*= 512*/){    UGKByte *pabyBuf;    if (fpSrc == NULL || nSize == 0)    {        UGKError(ET_Failure, UGKErr_AssertionFailed,                   "TABRawBinBlock::ReadFromFile(): Assertion Failed!");        return -1;    }    m_fp = fpSrc;  //取得文件句柄    m_nFileOffset = nOffset;  //偏移量    m_nCurPos = 0;            //当前位置    m_bModified = FALSE;      //未修改        /*----------------------------------------------------------------     * Alloc a buffer to contain the data	 * 分配一个缓冲区来存放数据     *---------------------------------------------------------------*/    pabyBuf = (UGKByte*)UGK_Malloc(nSize*sizeof(UGKByte));    //----ZGQ		// 下面InitBlockFromData()函数的bMakeCopy参数为FALSE	// 则由TABRawBinBlock对象来保存此块内存,负责释放它    /*----------------------------------------------------------------     * Read from the file     *---------------------------------------------------------------*/    if (fseek(fpSrc, nOffset, SEEK_SET) != 0 ||        (m_nSizeUsed = fread(pabyBuf, sizeof(UGKByte), nSize, fpSrc) ) == 0 ||        (m_bHardBlockSize && m_nSizeUsed != nSize ) )    {        UGKError(ET_Failure, UGKErr_FileIO,                  "ReadFromFile() failed reading %d bytes at offset %d.",                  nSize, nOffset);        UGK_Free(pabyBuf);        return -1;    }    /*----------------------------------------------------------------     * Init block with the data we just read     *---------------------------------------------------------------*/     //根据读取的数据初始化二进制块    return InitBlockFromData(pabyBuf, nSize, FALSE, fpSrc, nOffset);}/********************************************************************** *                   TABRawBinBlock::CommitToFile() * * Commit the current state of the binary block to the file to which  * it has been previously attached. * * Derived classes may want to (optionally) reimplement this method if * they need to do special processing before committing the block to disk. * * For files created with bHardBlockSize=TRUE, a complete block of * the specified size is always written, otherwise only the number of * used bytes in the block will be written to disk. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::CommitToFile(){    int nStatus = 0;    if (m_fp == NULL || m_nBlockSize <= 0 || m_pabyBuf == NULL ||        m_nFileOffset < 0)    {        UGKError(ET_Failure, UGKErr_AssertionFailed,          "TABRawBinBlock::CommitToFile(): Block has not been initialized yet!");        return -1;    }    /*----------------------------------------------------------------     * If block has not been modified, then just return... nothing to do.     *---------------------------------------------------------------*/    if (!m_bModified)        return 0;    /*----------------------------------------------------------------     * Move the output file pointer to the right position...      *---------------------------------------------------------------*/    if (fseek(m_fp, m_nFileOffset, SEEK_SET) != 0)    {        /*------------------------------------------------------------         * Moving pointer failed... we may need to pad with zeros if          * block destination is beyond current end of file.         *-----------------------------------------------------------*/        int nCurPos;        nCurPos = ftell(m_fp);//Gets the current position of a file pointer        if (nCurPos < m_nFileOffset &&            fseek(m_fp, 0L, SEEK_END) == 0 &&            (nCurPos = ftell(m_fp)) < m_nFileOffset)        {            UGKByte cZero = 0;            while(nCurPos < m_nFileOffset && nStatus == 0)            {                if (fwrite(&cZero, 1, 1, m_fp) != 1)                {                    UGKError(ET_Failure, UGKErr_FileIO,                              "Failed writing 1 byte at offset %d.", nCurPos);                    nStatus = -1;                    break;                }                nCurPos++;            }        }                    if (nCurPos != m_nFileOffset)            nStatus = -1; // Error message will follow below    }    /*----------------------------------------------------------------     * At this point we are ready to write to the file.     *     * If m_bHardBlockSize==FALSE, then we do not write a complete block;     * we write only the part of the block that was used.     *---------------------------------------------------------------*/    int numBytesToWrite = m_bHardBlockSize?m_nBlockSize:m_nSizeUsed;    if (nStatus != 0 ||        fwrite(m_pabyBuf,sizeof(UGKByte),                    numBytesToWrite, m_fp) != (size_t)numBytesToWrite )    {        UGKError(ET_Failure, UGKErr_FileIO,                  "Failed writing %d bytes at offset %d.",                  numBytesToWrite, m_nFileOffset);        return -1;    }    fflush(m_fp);    m_bModified = FALSE;    return 0;}/********************************************************************** *                   TABRawBinBlock::InitBlockFromData() * * Set the binary data buffer and initialize the block. * * Calling ReadFromFile() will automatically call InitBlockFromData() to * complete the initialization of the block after the data is read from the * file.  Derived classes should implement their own version of  * InitBlockFromData() if they need specific initialization... in this * case the derived InitBlockFromData() should call TABRawBinBlock::InitBlockFromData() * before doing anything else. * * By default, the buffer will be copied, but if bMakeCopy = FALSE then * it won't be copied, and the object will keep a reference to the * user's buffer... and this object will eventually free the user's buffer. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::InitBlockFromData(UGKByte *pabyBuf, int nSize,                                       UGKBool bMakeCopy /* = TRUE */,                                      FILE *fpSrc /* = NULL */,                                       int nOffset /* = 0 */){    m_fp = fpSrc;    m_nFileOffset = nOffset;    m_nCurPos = 0;    m_bModified = FALSE;        /*----------------------------------------------------------------     * Alloc or realloc the buffer to contain the data if necessary     *---------------------------------------------------------------*/    if (!bMakeCopy) //这里没有拷贝,而是对象的保存缓冲区的引用    {        if (m_pabyBuf != NULL)            UGK_Free(m_pabyBuf);        m_pabyBuf = pabyBuf;        m_nSizeUsed = m_nBlockSize = nSize;    }    else if (m_pabyBuf == NULL || nSize != m_nBlockSize)    {        m_pabyBuf = (UGKByte*)UGK_Realloc(m_pabyBuf, nSize*sizeof(UGKByte));        m_nSizeUsed = m_nBlockSize = nSize;        memcpy(m_pabyBuf, pabyBuf, m_nBlockSize);    }    /*----------------------------------------------------------------     * Extract block type... header block (first block in a file) has     * no block type, so we assign one by default.     *---------------------------------------------------------------*/    if (m_nFileOffset == 0)        m_nBlockType = TABMAP_HEADER_BLOCK;    else    {        // Block type will be validated only if GetBlockType() is called        m_nBlockType = (int)m_pabyBuf[0];    }    return 0;}/********************************************************************** *                   TABRawBinBlock::InitNewBlock() * * Initialize the block so that it knows to which file is is attached, * its block size, etc. * * This is an alternative to calling ReadFromFile() or InitBlockFromData() * that puts the block in a stable state without loading any initial * data in it. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::InitNewBlock(FILE *fpSrc, int nBlockSize,                                   int nFileOffset /* = 0*/){    m_fp = fpSrc;    m_nBlockSize = nBlockSize;    m_nSizeUsed = 0;    m_nCurPos = 0;    m_bModified = FALSE;    if (nFileOffset > 0)        m_nFileOffset = nFileOffset;    else        m_nFileOffset = 0;    m_nBlockType = -1;    m_pabyBuf = (UGKByte*)UGK_Realloc(m_pabyBuf, m_nBlockSize*sizeof(UGKByte));    memset(m_pabyBuf, 0, m_nBlockSize);    return 0;}/********************************************************************** *                   TABRawBinBlock::GetBlockType() * * Return the block type for the current object. * * Returns a block type >= 0 if succesful or -1 if an error happened, in  * which case  UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::GetBlockType(){    if (m_pabyBuf == NULL)    {        UGKError(ET_Failure, UGKErr_AppDefined,                  "GetBlockType(): Block has not been initialized.");        return -1;    }    if (m_nBlockType > TABMAP_LAST_VALID_BLOCK_TYPE)    {        UGKError(ET_Failure, UGKErr_NotSupported,                  "GetBlockType(): Unsupported block type %d.",                   m_nBlockType);        return -1;    }    return m_nBlockType;}/********************************************************************** *                   TABRawBinBlock::GotoByteInBlock() * * Move the block pointer to the specified position relative to the  * beginning of the block. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::GotoByteInBlock(int nOffset){    if ( (m_eAccess == TABRead && nOffset > m_nSizeUsed) ||         (m_eAccess != TABRead && nOffset > m_nBlockSize) )    {        UGKError(ET_Failure, UGKErr_AppDefined,                  "GotoByteInBlock(): Attempt to go past end of data block.");        return -1;    }    if (nOffset < 0)    {        UGKError(ET_Failure, UGKErr_AppDefined,                "GotoByteInBlock(): Attempt to go before start of data block.");        return -1;    }    m_nCurPos = nOffset;        m_nSizeUsed = MAX(m_nSizeUsed, m_nCurPos);    return 0;}/********************************************************************** *                   TABRawBinBlock::GotoByteRel() * * Move the block pointer by the specified number of bytes relative * to its current position. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::GotoByteRel(int nOffset){    return GotoByteInBlock(m_nCurPos + nOffset);}/********************************************************************** *                   TABRawBinBlock::GotoByteInFile() * * Move the block pointer to the specified position relative to the  * beginning of the file.   *  * In read access, the current block may be reloaded to contain a right * block of binary data if necessary. * * In write mode, the current block may automagically be committed to  * disk and a new block initialized if necessary. * * Returns 0 if succesful or -1 if an error happened, in which case  * UGKError() will have been called. **********************************************************************/int     TABRawBinBlock::GotoByteInFile(int nOffset){    int nNewBlockPtr;    if (nOffset < 0)    {        UGKError(ET_Failure, UGKErr_AppDefined,                "GotoByteInFile(): Attempt to go before start of file.");        return -1;    }    nNewBlockPtr = ( (nOffset-m_nFirstBlockPtr)/m_nBlockSize)*m_nBlockSize +                     m_nFirstBlockPtr;    if (m_eAccess == TABRead)    {        if ( (nOffset<m_nFileOffset || nOffset>=m_nFileOffset+m_nSizeUsed) &&             ReadFromFile(m_fp, nNewBlockPtr, m_nBlockSize) != 0)        {            // Failed reading new block... error has already been reported.            return -1;        }    }    else if (m_eAccess == TABWrite)    {        if ( (nOffset<m_nFileOffset || nOffset>=m_nFileOffset+m_nBlockSize) &&             (CommitToFile() != 0 ||              InitNewBlock(m_fp, m_nBlockSize, nNewBlockPtr) != 0)  )        {            // Failed reading new block... error has already been reported.            return -1;        }    }    else    {        UGKError(ET_Failure, UGKErr_NotSupported,                  "Access mode not supported yet!");        return -1;    }    m_nCurPos = nOffset-m_nFileOffset;    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() *

⌨️ 快捷键说明

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