📄 tabindfile.cpp
字号:
// tabindfile.cpp: implementation of the TABINDFile class.////////////////////////////////////////////////////////////////////////#include "tabindfile.h"#include "ugk_errhandle.h"#include "ugk_memopr.h"#include "ugk_string.h"#define IND_MAGIC_COOKIE 24242424/********************************************************************** * TABINDFile::TABINDFile() * * Constructor. **********************************************************************/TABINDFile::TABINDFile(){ m_fp = NULL; m_pszFname = NULL; m_eAccessMode = TABRead; m_numIndexes = 0; m_papoIndexRootNodes = NULL; m_papbyKeyBuffers = NULL;}/********************************************************************** * TABINDFile::~TABINDFile() * * Destructor. **********************************************************************/TABINDFile::~TABINDFile(){ Close();}/********************************************************************** * TABINDFile::Open() * * Open a .IND file, read the header and the root nodes for all the * field indexes, and be ready to search the indexes. * * If the filename that is passed in contains a .DAT extension then * the extension will be changed to .IND before trying to open the file. * * Note that we pass a pszAccess flag, but only read access is supported * for now (and there are no plans to support write.) * * Set bTestOpenNoError=TRUE to silently return -1 with no error message * if the file cannot be opened because it does not exist. * * Returns 0 on success, -1 on error. **********************************************************************/int TABINDFile::Open(const char *pszFname, const char *pszAccess, UGKBool bTestOpenNoError /*=FALSE*/){ int nLen; if (m_fp) { UGKError(ET_Failure, UGKErr_FileIO, "Open() failed: object already contains an open file"); return -1; } /*----------------------------------------------------------------- * Validate access mode and make sure we use binary access. * Note that for write access, we actually need read/write access to * the file. *----------------------------------------------------------------*/ if (EQUALN(pszAccess, "r", 1) && strchr(pszAccess, '+') != NULL) { m_eAccessMode = TABReadWrite; pszAccess = "rb+"; } else 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; } /*----------------------------------------------------------------- * Change .DAT (or .TAB) extension to .IND if necessary *----------------------------------------------------------------*/ m_pszFname = UGKStrdup(pszFname); nLen = strlen(m_pszFname); if (nLen > 4 && !EQUAL(m_pszFname+nLen-4, ".IND") ) strcpy(m_pszFname+nLen-4, ".ind");#ifndef WIN32APP TABAdjustFilenameExtension(m_pszFname);#endif /*----------------------------------------------------------------- * Open file *----------------------------------------------------------------*/ m_fp = fopen(m_pszFname, pszAccess); if (m_fp == NULL) { if (!bTestOpenNoError) UGKError(ET_Failure, UGKErr_FileIO, "Open() failed for %s", m_pszFname); UGK_Free(m_pszFname); m_pszFname = NULL; return -1; } /*----------------------------------------------------------------- * Reset block manager to allocate first block at byte 512, after header. *----------------------------------------------------------------*/ m_oBlockManager.Reset(); m_oBlockManager.AllocNewBlock(); /*----------------------------------------------------------------- * Read access: Read the header block * This will also alloc and init the array of index root nodes. *----------------------------------------------------------------*/ if ((m_eAccessMode == TABRead || m_eAccessMode == TABReadWrite) && ReadHeader() != 0) { // Failed reading header... CPLError() has already been called Close(); return -1; } /*----------------------------------------------------------------- * Write access: Init class members and write a dummy header block *----------------------------------------------------------------*/ if (m_eAccessMode == TABWrite) { m_numIndexes = 0; if (WriteHeader() != 0) { // Failed writing header... CPLError() has already been called Close(); return -1; } } return 0;}/********************************************************************** * TABINDFile::Close() * * Close current file, and release all memory used. * * Returns 0 on success, -1 on error. **********************************************************************/int TABINDFile::Close(){ if (m_fp == NULL) return 0; /*----------------------------------------------------------------- * In Write Mode, commit all indexes to the file *----------------------------------------------------------------*/ if (m_eAccessMode == TABWrite || m_eAccessMode == TABReadWrite) { WriteHeader(); for(int iIndex=0; iIndex<m_numIndexes; iIndex++) { if (m_papoIndexRootNodes && m_papoIndexRootNodes[iIndex]) { m_papoIndexRootNodes[iIndex]->CommitToFile(); } } } /*----------------------------------------------------------------- * Free index nodes in memory *----------------------------------------------------------------*/ for (int iIndex=0; iIndex<m_numIndexes; iIndex++) { if (m_papoIndexRootNodes && m_papoIndexRootNodes[iIndex]) delete m_papoIndexRootNodes[iIndex]; if (m_papbyKeyBuffers && m_papbyKeyBuffers[iIndex]) UGK_Free(m_papbyKeyBuffers[iIndex]); } UGK_Free(m_papoIndexRootNodes); m_papoIndexRootNodes = NULL; UGK_Free(m_papbyKeyBuffers); m_papbyKeyBuffers = NULL; m_numIndexes = 0; /*----------------------------------------------------------------- * Close file *----------------------------------------------------------------*/ fclose(m_fp); m_fp = NULL; UGK_Free(m_pszFname); m_pszFname = NULL; return 0;}/********************************************************************** * TABINDFile::ReadHeader() * * (private method) * Read the header block and init all class members for read access. * * Returns 0 on success, -1 on error. **********************************************************************/int TABINDFile::ReadHeader(){ assert(m_fp); assert(m_eAccessMode == TABRead || m_eAccessMode == TABReadWrite); /*----------------------------------------------------------------- * In ReadWrite mode, we need to init BlockManager with file size *----------------------------------------------------------------*/ struct stat sStatBuf; if (m_eAccessMode == TABReadWrite && stat(m_pszFname, &sStatBuf) != -1) { m_oBlockManager.SetLastPtr(((sStatBuf.st_size-1)/512)*512); } /*----------------------------------------------------------------- * Read the header block *----------------------------------------------------------------*/ TABRawBinBlock *poHeaderBlock; poHeaderBlock = new TABRawBinBlock(m_eAccessMode, TRUE); if (poHeaderBlock->ReadFromFile(m_fp, 0, 512) != 0) { // CPLError() has already been called. delete poHeaderBlock; return -1; } poHeaderBlock->GotoByteInBlock(0); UGKUInt32 nMagicCookie = poHeaderBlock->ReadInt32(); if (nMagicCookie != IND_MAGIC_COOKIE) { UGKError(ET_Failure, UGKErr_FileIO, "%s: Invalid Magic Cookie: got %d, expected %d", m_pszFname, nMagicCookie, IND_MAGIC_COOKIE); delete poHeaderBlock; return -1; } poHeaderBlock->GotoByteInBlock(12); m_numIndexes = poHeaderBlock->ReadInt16(); if (m_numIndexes < 1 || m_numIndexes > 29) { UGKError(ET_Failure, UGKErr_FileIO, "Invalid number of indexes (%d) in file %s", m_numIndexes, m_pszFname); delete poHeaderBlock; return -1; } /*----------------------------------------------------------------- * Alloc and init the array of index root nodes. *----------------------------------------------------------------*/ m_papoIndexRootNodes = (TABINDNode**)UGK_Calloc(m_numIndexes, sizeof(TABINDNode*)); m_papbyKeyBuffers = (UGKByte **)UGK_Calloc(m_numIndexes, sizeof(UGKByte*)); /* First index def. starts at byte 48 */ poHeaderBlock->GotoByteInBlock(48); for(int iIndex=0; iIndex<m_numIndexes; iIndex++) { /*------------------------------------------------------------- * Read next index definition *------------------------------------------------------------*/ UGKInt32 nRootNodePtr = poHeaderBlock->ReadInt32(); poHeaderBlock->ReadInt16(); // skip... max. num of entries per node int nTreeDepth = poHeaderBlock->ReadByte(); int nKeyLength = poHeaderBlock->ReadByte(); poHeaderBlock->GotoByteRel(8); // skip next 8 bytes; /*------------------------------------------------------------- * And init root node for this index. * Note that if nRootNodePtr==0 then this means that the * corresponding index does not exist (i.e. has been deleted?) * so we simply do not allocate the root node in this case. * An error will be produced if the user tries to access this index * later during execution. *------------------------------------------------------------*/ if (nRootNodePtr > 0) { m_papoIndexRootNodes[iIndex] = new TABINDNode(m_eAccessMode); if (m_papoIndexRootNodes[iIndex]->InitNode(m_fp, nRootNodePtr, nKeyLength, nTreeDepth, FALSE, &m_oBlockManager)!= 0) { // CPLError has already been called delete poHeaderBlock; return -1; } // Alloc a temporary key buffer for this index. // This buffer will be used by the BuildKey() method m_papbyKeyBuffers[iIndex] = (UGKByte *)UGK_Calloc(nKeyLength+1, sizeof(UGKByte)); } else { m_papoIndexRootNodes[iIndex] = NULL; m_papbyKeyBuffers[iIndex] = NULL; } } /*----------------------------------------------------------------- * OK, we won't need the header block any more... free it. *----------------------------------------------------------------*/ delete poHeaderBlock; return 0;}/********************************************************************** * TABINDFile::WriteHeader() * * (private method) * Write the header block based on current index information. * * Returns 0 on success, -1 on error. **********************************************************************/int TABINDFile::WriteHeader(){ assert(m_fp); assert(m_eAccessMode == TABWrite || m_eAccessMode == TABReadWrite); /*----------------------------------------------------------------- * Write the 48 bytes of file header *----------------------------------------------------------------*/ TABRawBinBlock *poHeaderBlock; poHeaderBlock = new TABRawBinBlock(m_eAccessMode, TRUE); poHeaderBlock->InitNewBlock(m_fp, 512, 0); poHeaderBlock->WriteInt32( IND_MAGIC_COOKIE ); poHeaderBlock->WriteInt16( 100 ); // ??? poHeaderBlock->WriteInt16( 512 ); // ??? poHeaderBlock->WriteInt32( 0 ); // ??? poHeaderBlock->WriteInt16( m_numIndexes ); poHeaderBlock->WriteInt16( 0x15e7); // ??? poHeaderBlock->WriteInt16( 10 ); // ??? poHeaderBlock->WriteInt16( 0x611d); // ??? poHeaderBlock->WriteZeros( 28 ); /*----------------------------------------------------------------- * The first index definition starts at byte 48 *----------------------------------------------------------------*/ for(int iIndex=0; iIndex<m_numIndexes; iIndex++) { TABINDNode *poRootNode = m_papoIndexRootNodes[iIndex]; if (poRootNode) { /*--------------------------------------------------------- * Write next index definition *--------------------------------------------------------*/ poHeaderBlock->WriteInt32(poRootNode->GetNodeBlockPtr()); poHeaderBlock->WriteInt16(poRootNode->GetMaxNumEntries()); poHeaderBlock->WriteByte( poRootNode->GetSubTreeDepth()); poHeaderBlock->WriteByte( poRootNode->GetKeyLength()); poHeaderBlock->WriteZeros( 8 ); } else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -