📄 tabmapindexblock.cpp
字号:
// tabmapindexblock.cpp: implementation of the TABMAPIndexBlock class.////////////////////////////////////////////////////////////////////////#include "tabmapindexblock.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"/********************************************************************** * TABMAPIndexBlock::TABMAPIndexBlock() * * Constructor. **********************************************************************/TABMAPIndexBlock::TABMAPIndexBlock(TABAccess eAccessMode /*= TABRead*/): TABRawBinBlock(eAccessMode, TRUE){ m_numEntries = 0; m_nMinX = 1000000000; m_nMinY = 1000000000; m_nMaxX = -1000000000; m_nMaxY = -1000000000; m_poCurChild = NULL; m_nCurChildIndex = -1; m_poParentRef = NULL; m_poBlockManagerRef = NULL;}/********************************************************************** * TABMAPIndexBlock::~TABMAPIndexBlock() * * Destructor. **********************************************************************/TABMAPIndexBlock::~TABMAPIndexBlock(){ if (m_poCurChild) { if (m_eAccess == TABWrite || m_eAccess == TABReadWrite) m_poCurChild->CommitToFile(); delete m_poCurChild; }}/********************************************************************** * TABMAPIndexBlock::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 TABMAPIndexBlock::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_INDEX_BLOCK) { UGKError(ET_Failure, UGKErr_FileIO, "InitBlockFromData(): Invalid Block Type: got %d expected %d", m_nBlockType, TABMAP_INDEX_BLOCK); UGK_Free(m_pabyBuf); m_pabyBuf = NULL; return -1; } /*----------------------------------------------------------------- * Init member variables *----------------------------------------------------------------*/ GotoByteInBlock(0x002); m_numEntries = ReadInt16(); if (m_numEntries > 0) ReadAllEntries(); //读取所有的TABMAPIndexEntry return 0;}/********************************************************************** * TABMAPIndexBlock::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 TABMAPIndexBlock::CommitToFile(){ int nStatus = 0; if ( m_pabyBuf == NULL ) { UGKError(ET_Failure, UGKErr_AssertionFailed, "CommitToFile(): Block has not been initialized yet!"); return -1; } /*----------------------------------------------------------------- * Commit child first *----------------------------------------------------------------*/ if (m_poCurChild) { if (m_poCurChild->CommitToFile() != 0) return -1; } /*----------------------------------------------------------------- * Make sure 4 bytes block header is up to date. *----------------------------------------------------------------*/ GotoByteInBlock(0x000); WriteInt16(TABMAP_INDEX_BLOCK); // Block type code WriteInt16(m_numEntries); nStatus = UGKGetLastErrorNo(); /*----------------------------------------------------------------- * Loop through all entries, writing each of them, and calling * CommitToFile() (recursively) on any child index entries we may * encounter. *----------------------------------------------------------------*/ for(int i=0; nStatus == 0 && i<m_numEntries; i++) { if (nStatus == 0) nStatus = WriteNextEntry(&(m_asEntries[i])); } /*----------------------------------------------------------------- * OK, call the base class to write the block to disk. *----------------------------------------------------------------*/ if (nStatus == 0) nStatus = TABRawBinBlock::CommitToFile(); return nStatus;}/********************************************************************** * TABMAPIndexBlock::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 TABMAPIndexBlock::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. *----------------------------------------------------------------*/ m_numEntries = 0; m_nMinX = 1000000000; m_nMinY = 1000000000; m_nMaxX = -1000000000; m_nMaxY = -1000000000; if (m_eAccess != TABRead) { GotoByteInBlock(0x000); WriteInt16(TABMAP_INDEX_BLOCK); // Block type code WriteInt16(0); // num. index entries } if (UGKGetLastErrorNo() != 0) return -1; return 0;}/********************************************************************** * TABMAPIndexBlock::ReadNextEntry() * * Read the next index entry from the block and fill the sEntry * structure. * * Returns 0 if succesful or -1 if we reached the end of the block. **********************************************************************/int TABMAPIndexBlock::ReadNextEntry(TABMAPIndexEntry *psEntry){ if (m_nCurPos < 4) GotoByteInBlock( 0x004 ); if (m_nCurPos > 4+(20*m_numEntries) ) { // End of BLock return -1; } psEntry->XMin = ReadInt32(); psEntry->YMin = ReadInt32(); psEntry->XMax = ReadInt32(); psEntry->YMax = ReadInt32(); psEntry->nBlockPtr = ReadInt32(); if (UGKGetLastErrorNo() != 0) return -1; return 0;}/********************************************************************** * TABMAPIndexBlock::ReadAllEntries() * * Init the block by reading all entries from the data block. * * Returns 0 if succesful or -1 on error. **********************************************************************/int TABMAPIndexBlock::ReadAllEntries(){ assert(m_numEntries <= TAB_MAX_ENTRIES_INDEX_BLOCK); if (m_numEntries == 0) return 0; if (GotoByteInBlock( 0x004 ) != 0) return -1; for(int i=0; i<m_numEntries; i++) { if ( ReadNextEntry(&(m_asEntries[i])) != 0) return -1; } return 0;}/********************************************************************** * TABMAPIndexBlock::WriteNextEntry() * * Write the sEntry index entry at current position in the block. * * Returns 0 if succesful or -1 if we reached the end of the block. **********************************************************************/int TABMAPIndexBlock::WriteNextEntry(TABMAPIndexEntry *psEntry){ if (m_nCurPos < 4) GotoByteInBlock( 0x004 ); WriteInt32(psEntry->XMin); WriteInt32(psEntry->YMin); WriteInt32(psEntry->XMax); WriteInt32(psEntry->YMax); WriteInt32(psEntry->nBlockPtr); if (UGKGetLastErrorNo() != 0) return -1; return 0;}/********************************************************************** * TABMAPIndexBlock::GetNumFreeEntries() * * Return the number of available entries in this block. * * __TODO__ This function could eventually be improved to search * children leaves as well. **********************************************************************/int TABMAPIndexBlock::GetNumFreeEntries(){ /* nMaxEntries = (m_nBlockSize-4)/20;*/ return (TAB_MAX_ENTRIES_INDEX_BLOCK - m_numEntries);}/********************************************************************** * TABMAPIndexBlock::GetEntry() * * Fetch a reference to the requested entry. * * @param iIndex index of entry, must be from 0 to GetNumEntries()-1. * * @return a reference to the internal copy of the entry, or NULL if out * of range. **********************************************************************/TABMAPIndexEntry *TABMAPIndexBlock::GetEntry( int iIndex ){ if( iIndex < 0 || iIndex >= m_numEntries ) return NULL; return m_asEntries + iIndex;}/********************************************************************** * TABMAPIndexBlock::GetCurMaxDepth() * * Return maximum depth in the currently loaded part of the index tree **********************************************************************/int TABMAPIndexBlock::GetCurMaxDepth(){ if (m_poCurChild)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -