📄 tabmapobjectblock.cpp
字号:
// tabmapobjectblock.cpp: implementation of the TABMAPObjectBlock class.////////////////////////////////////////////////////////////////////////#include "tabmapobjectblock.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"#include "tabmapobjhdr.h" #define MAP_OBJECT_HEADER_SIZE 20/********************************************************************** * TABMAPObjectBlock::TABMAPObjectBlock() * * Constructor. **********************************************************************/TABMAPObjectBlock::TABMAPObjectBlock(TABAccess eAccessMode /*= TABRead*/): TABRawBinBlock(eAccessMode, TRUE){ m_papoObjHdr = NULL; m_numObjects = 0;}/********************************************************************** * TABMAPObjectBlock::~TABMAPObjectBlock() * * Destructor. **********************************************************************/TABMAPObjectBlock::~TABMAPObjectBlock(){ m_nMinX = 1000000000; m_nMinY = 1000000000; m_nMaxX = -1000000000; m_nMaxY = -1000000000; FreeObjectArray();}/********************************************************************** * TABMAPObjectBlock::FreeObjectArray() **********************************************************************/void TABMAPObjectBlock::FreeObjectArray(){ if (m_numObjects > 0 && m_papoObjHdr) { for(int i=0; i<m_numObjects; i++) delete m_papoObjHdr[i]; UGK_Free(m_papoObjHdr); } m_numObjects = 0;}/********************************************************************** * TABMAPObjectBlock::InitBlockFromData() * * Perform some initialization on the block after its binary data has * been set or changed (or loaded from a file). * * Returns 0 if succesful or -1 if an error happened, in which case * UGKError() will have been called. **********************************************************************/int TABMAPObjectBlock::InitBlockFromData(UGKByte *pabyBuf, int nSize, UGKBool bMakeCopy /* = TRUE */, FILE *fpSrc /* = NULL */, int nOffset /* = 0 */){ int nStatus; /*----------------------------------------------------------------- * First of all, we must call the base class' InitBlockFromData() *----------------------------------------------------------------*/ nStatus = TABRawBinBlock::InitBlockFromData(pabyBuf, nSize, bMakeCopy, fpSrc, nOffset); if (nStatus != 0) return nStatus; /*----------------------------------------------------------------- * Validate block type *----------------------------------------------------------------*/ if (m_nBlockType != TABMAP_OBJECT_BLOCK) { UGKError(ET_Failure, UGKErr_FileIO, "InitBlockFromData(): Invalid Block Type: got %d expected %d", m_nBlockType, TABMAP_OBJECT_BLOCK); UGK_Free(m_pabyBuf); return -1; } /*----------------------------------------------------------------- * Init member variables *----------------------------------------------------------------*/ FreeObjectArray(); GotoByteInBlock(0x002); m_numDataBytes = ReadInt16(); /* Excluding 4 bytes header */ m_nCenterX = ReadInt32(); m_nCenterY = ReadInt32(); m_nFirstCoordBlock = ReadInt32(); m_nLastCoordBlock = ReadInt32(); m_nCurObjectOffset = -1; m_nCurObjectId = -1; return 0;}/************************************************************************//* AdvanceToNextObject() *//************************************************************************/int TABMAPObjectBlock::AdvanceToNextObject( TABMAPHeaderBlock *poHeader ){ if( m_nCurObjectId == -1 ) { m_nCurObjectOffset = 20; } else { m_nCurObjectOffset += poHeader->GetMapObjectSize( m_nCurObjectType ); } if( m_nCurObjectOffset + 5 < m_numDataBytes + 20 ) { GotoByteInBlock( m_nCurObjectOffset ); m_nCurObjectType = ReadByte(); //读第一个字节从而知道此对象长度 } else { m_nCurObjectType = -1; } if( m_nCurObjectType <= 0 || m_nCurObjectType >= 0x80 ) { m_nCurObjectType = -1; m_nCurObjectId = -1; m_nCurObjectOffset = -1; } else { m_nCurObjectId = ReadInt32(); //读取1-4字节 // Is this object marked as deleted? If so, skip it. // I check both the top bits but I have only seen this occur // with the second highest bit set (ie. in usa/states.tab). NFW. if( (((UGKUInt32)m_nCurObjectId) & (UGKUInt32) 0xC0000000) != 0 ) { m_nCurObjectId = AdvanceToNextObject( poHeader ); //表示已经标志删除 } } return m_nCurObjectId;}/********************************************************************** * TABMAPObjectBlock::CommitToFile() * * Commit the current state of the binary block to the file to which * it has been previously attached. * * This method makes sure all values are properly set in the map object * block header and then calls TABRawBinBlock::CommitToFile() to do * the actual writing to disk. * * Returns 0 if succesful or -1 if an error happened, in which case * UGKError() will have been called. **********************************************************************/int TABMAPObjectBlock::CommitToFile(){ int nStatus = 0; if ( m_pabyBuf == NULL ) { UGKError(ET_Failure, UGKErr_AssertionFailed, "TABMAPObjectBlock::CommitToFile(): Block has not been initialized yet!"); return -1; } /*----------------------------------------------------------------- * Make sure 20 bytes block header is up to date. *----------------------------------------------------------------*/ GotoByteInBlock(0x000); WriteInt16(TABMAP_OBJECT_BLOCK); // Block type code WriteInt16(m_nSizeUsed - MAP_OBJECT_HEADER_SIZE); // num. bytes used WriteInt32(m_nCenterX); WriteInt32(m_nCenterY); WriteInt32(m_nFirstCoordBlock); WriteInt32(m_nLastCoordBlock); nStatus = UGKGetLastErrorNo(); /*----------------------------------------------------------------- * Write all the individual objects *----------------------------------------------------------------*/ for(int i=0; i<m_numObjects; i++) { m_papoObjHdr[i]->WriteObj(this); } /*----------------------------------------------------------------- * OK, call the base class to write the block to disk. *----------------------------------------------------------------*/ if (nStatus == 0) nStatus = TABRawBinBlock::CommitToFile(); return nStatus;}/********************************************************************** * TABMAPObjectBlock::InitNewBlock() * * Initialize a newly created block so that it knows to which file it * is attached, its block size, etc . and then perform any specific * initialization for this block type, including writing a default * block header, etc. and leave the block ready to receive data. * * 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 TABMAPObjectBlock::InitNewBlock(FILE *fpSrc, int nBlockSize, int nFileOffset /* = 0*/){ /*----------------------------------------------------------------- * Start with the default initialisation *----------------------------------------------------------------*/ if ( TABRawBinBlock::InitNewBlock(fpSrc, nBlockSize, nFileOffset) != 0) return -1; /*----------------------------------------------------------------- * And then set default values for the block header. *----------------------------------------------------------------*/ // Set block MBR to extreme values to force an update on the first // UpdateMBR() call. m_nMinX = 1000000000; m_nMaxX = -1000000000; m_nMinY = 1000000000; m_nMaxY = -1000000000; // Make sure there is no object header left from last time. FreeObjectArray(); m_numDataBytes = 0; /* Data size excluding header */ m_nCenterX = m_nCenterY = 0; m_nFirstCoordBlock = 0; m_nLastCoordBlock = 0; if (m_eAccess != TABRead) { GotoByteInBlock(0x000); WriteInt16(TABMAP_OBJECT_BLOCK);// Block type code WriteInt16(0); // num. bytes used, excluding header // MBR center here... will be written in CommitToFile()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -