📄 tabmapfile.cpp
字号:
// tabmapfile.cpp: implementation of the TABMAPFile class.////////////////////////////////////////////////////////////////////////#include "tabmapfile.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"#include "ugk_string.h"/********************************************************************** * TABMAPFile::TABMAPFile() * * Constructor. **********************************************************************/TABMAPFile::TABMAPFile(){ m_nMinTABVersion = 300; m_fp = NULL; m_pszFname = NULL; m_poHeader = NULL; m_poSpIndex = NULL; m_poSpIndexLeaf = NULL; m_poCurObjBlock = NULL; m_nCurObjPtr = -1; m_nCurObjType = -1; m_nCurObjId = -1; m_poCurCoordBlock = NULL; m_poToolDefTable = NULL;}/********************************************************************** * TABMAPFile::~TABMAPFile() * * Destructor. **********************************************************************/TABMAPFile::~TABMAPFile(){ Close();}/********************************************************************** * TABMAPFile::Open() * * Open a .MAP file, and initialize the structures to be ready to read * objects from it. * * Since .MAP and .ID files are optional, you can set bNoErrorMsg=TRUE to * disable the error message and receive an return value of 1 if file * cannot be opened. * In this case, only the methods MoveToObjId() and GetCurObjType() can * be used. They will behave as if the .ID file contained only null * references, so all object will look like they have NONE geometries. * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPFile::Open(const char *pszFname, const char *pszAccess, UGKBool bNoErrorMsg /* = FALSE */){ FILE *fp=NULL; TABRawBinBlock *poBlock=NULL; if (m_fp) { UGKError(ET_Failure, UGKErr_FileIO, "Open() failed: object already contains an open file"); return -1; } m_nMinTABVersion = 300; m_fp = NULL; m_poHeader = NULL; m_poIdIndex = NULL; m_poSpIndex = NULL; m_poToolDefTable = NULL; /*----------------------------------------------------------------- * Validate access mode and make sure we use binary access. *----------------------------------------------------------------*/ if (EQUALN(pszAccess, "r", 1)) { m_eAccessMode = TABRead; pszAccess = "rb"; } else if (EQUALN(pszAccess, "w", 1)) { m_eAccessMode = TABWrite; pszAccess = "wb+"; } else { UGKError(ET_Failure, UGKErr_FileIO, "Open() failed: access mode \"%s\" not supported", pszAccess); return -1; } /*----------------------------------------------------------------- * Open file *----------------------------------------------------------------*/ fp = fopen(pszFname, pszAccess); m_oBlockManager.Reset(); if (fp != NULL && m_eAccessMode == TABRead) { /*----------------------------------------------------------------- * Read access: try to read header block * First try with a 512 bytes block to check the .map version. * If it's version 500 or more then read again a 1024 bytes block * 读头块,先读512字节,然后根据它来判断头块是512还是1024字节 *----------------------------------------------------------------*/ poBlock = TABCreateMAPBlockFromFile(fp, 0, 512); if (poBlock && poBlock->GetBlockClass() == TABMAP_HEADER_BLOCK && ((TABMAPHeaderBlock*)poBlock)->m_nMAPVersionNumber >= 500) { // Version 500 or higher. Read 1024 bytes block instead of 512 delete poBlock; poBlock = TABCreateMAPBlockFromFile(fp, 0, 1024); } if (poBlock==NULL || poBlock->GetBlockClass() != TABMAP_HEADER_BLOCK) { if (poBlock) delete poBlock; poBlock = NULL; fclose(fp); UGKError(ET_Failure, UGKErr_FileIO, "Open() failed: %s does not appear to be a valid .MAP file", pszFname); return -1; } } else if (fp != NULL && m_eAccessMode == TABWrite) { /*----------------------------------------------------------------- * Write access: create a new header block * .MAP files of Version 500 and up appear to have a 1024 bytes * header. The last 512 bytes are usually all zeros. *----------------------------------------------------------------*/ poBlock = new TABMAPHeaderBlock(m_eAccessMode); poBlock->InitNewBlock(fp, 1024, m_oBlockManager.AllocNewBlock() ); // Alloc a second 512 bytes of space since oBlockManager deals // with 512 bytes blocks. m_oBlockManager.AllocNewBlock(); } else if (bNoErrorMsg) { /*----------------------------------------------------------------- * .MAP does not exist... produce no error message, but set * the class members so that MoveToObjId() and GetCurObjType() * can be used to return only NONE geometries. *----------------------------------------------------------------*/ m_fp = NULL; m_nCurObjType = TAB_GEOM_NONE; /* Create a false header block that will return default * values for projection and coordsys conversion stuff... */ m_poHeader = new TABMAPHeaderBlock(m_eAccessMode); m_poHeader->InitNewBlock(NULL, 512, 0 ); return 1; } else { UGKError(ET_Failure, UGKErr_FileIO, "Open() failed for %s", pszFname); return -1; } /*----------------------------------------------------------------- * File appears to be valid... set the various class members *----------------------------------------------------------------*/ m_fp = fp; m_poHeader = (TABMAPHeaderBlock*)poBlock; //头块 m_pszFname = UGKStrdup(pszFname); /*----------------------------------------------------------------- * Create a TABMAPObjectBlock, in READ mode only. * * In WRITE mode, the object block will be created only when needed. * We do not create the object block in the open() call because * files that contained only "NONE" geometries ended up with empty * object and spatial index blocks. *----------------------------------------------------------------*/ //$zgq if (m_eAccessMode == TABRead) { m_poCurObjBlock = new TABMAPObjectBlock(m_eAccessMode); m_poCurObjBlock->InitNewBlock(m_fp, 512); } else { m_poCurObjBlock = NULL; } /*----------------------------------------------------------------- * Open associated .ID (object id index) file * 打开与之关联的.ID文件 *----------------------------------------------------------------*/ m_poIdIndex = new TABIDFile; if (m_poIdIndex->Open(pszFname, pszAccess) != 0) { // Failed... an error has already been reported Close(); return -1; } /*----------------------------------------------------------------- * Default Coord filter is the MBR of the whole file * This is currently unused but could eventually be used to handle * spatial filters more efficiently. *----------------------------------------------------------------*/ if (m_eAccessMode == TABRead) { ResetCoordFilter(); } /*----------------------------------------------------------------- * We could scan a file through its quad tree index... but we don't! * * In read mode, we just ignore the spatial index. * 为什么读模式就没用空间索引呢????? * In write mode the index is created and maintained as new object * blocks are added inside CommitObjBlock(). *----------------------------------------------------------------*/ m_poSpIndex = NULL; /*----------------------------------------------------------------- * Initialization of the Drawing Tools table will be done automatically * as Read/Write calls are done later. *----------------------------------------------------------------*/ m_poToolDefTable = NULL; /*----------------------------------------------------------------- * Make sure all previous calls succeded. *----------------------------------------------------------------*/ if (UGKGetLastErrorNo() != 0) { // Open Failed... an error has already been reported Close(); return -1; } return 0;}/********************************************************************** * TABMAPFile::Close() * * Close current file, and release all memory used. * * Returns 0 on success, -1 on error. **********************************************************************/int TABMAPFile::Close(){ // Check if file is opened... it is possible to have a fake header // without an actual file attached to it. if (m_fp == NULL && m_poHeader == NULL) return 0; /*---------------------------------------------------------------- * Write access: commit latest changes to the file. *---------------------------------------------------------------*/ if (m_eAccessMode == TABWrite) { // Start by committing current object and coord blocks // Nothing happens if none has been created yet. CommitObjBlock(FALSE); // Write the drawing tools definitions now. CommitDrawingTools(); // Commit spatial index blocks CommitSpatialIndex(); // __TODO__ We probably need to update some header fields first. if (m_poHeader) { // OK, with V450 files, objects are not limited to 32k nodes // any more, and this means that m_nMaxCoordBufSize can become // huge, and actually more huge than can be held in memory. // MapInfo counts m_nMaxCoordBufSize=0 for V450 objects, but // until this is cleanly implented, we will just prevent // m_nMaxCoordBufSizefrom going beyond 512k in V450 files. if (m_nMinTABVersion >= 450) { m_poHeader->m_nMaxCoordBufSize = MIN(m_poHeader->m_nMaxCoordBufSize, 512*1024); } m_poHeader->CommitToFile(); } } // Check for overflow of internal coordinates and produce a warning // if that happened... if (m_poHeader && m_poHeader->m_bIntBoundsOverflow) { double dBoundsMinX, dBoundsMinY, dBoundsMaxX, dBoundsMaxY; Int2Coordsys(-1000000000, -1000000000, dBoundsMinX, dBoundsMinY); Int2Coordsys(1000000000, 1000000000, dBoundsMaxX, dBoundsMaxY); UGKError(ET_Warning, TAB_WarningBoundsOverflow, "Some objects were written outside of the file's " "predefined bounds.\n" "These objects may have invalid coordinates when the file " "is reopened.\n" "Predefined bounds: (%.15g,%.15g)-(%.15g,%.15g)\n", dBoundsMinX, dBoundsMinY, dBoundsMaxX, dBoundsMaxY ); } // Delete all structures if (m_poHeader) delete m_poHeader; m_poHeader = NULL; if (m_poIdIndex) { m_poIdIndex->Close(); delete m_poIdIndex; m_poIdIndex = NULL; } if (m_poCurObjBlock) { delete m_poCurObjBlock; m_poCurObjBlock = NULL; m_nCurObjPtr = -1; m_nCurObjType = -1; m_nCurObjId = -1; } if (m_poCurCoordBlock) { delete m_poCurCoordBlock; m_poCurCoordBlock = NULL; } if (m_poSpIndex) { delete m_poSpIndex; m_poSpIndex = NULL; m_poSpIndexLeaf = NULL; } if (m_poToolDefTable) { delete m_poToolDefTable; m_poToolDefTable = NULL; } // Close file if (m_fp) fclose(m_fp); m_fp = NULL; UGK_Free(m_pszFname); m_pszFname = NULL; return 0;}/************************************************************************//* PushBlock() */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -