📄 tabrawbinblock.cpp
字号:
* Return the number of unused bytes in this block. **********************************************************************/int TABRawBinBlock::GetNumUnusedBytes(){ return (m_nBlockSize - m_nSizeUsed);}/********************************************************************** * TABRawBinBlock::GetFirstUnusedByteOffset() * * Return the position of the first unused byte in this block relative * to the beginning of the file, or -1 if the block is full. **********************************************************************/int TABRawBinBlock::GetFirstUnusedByteOffset(){ if (m_nSizeUsed < m_nBlockSize) return m_nFileOffset + m_nSizeUsed; else return -1;}/********************************************************************** * TABRawBinBlock::GetCurAddress() * * Return the current pointer position, relative to beginning of file. **********************************************************************/int TABRawBinBlock::GetCurAddress(){ return (m_nFileOffset + m_nCurPos);}/********************************************************************** * TABRawBinBlock::ReadBytes() * * Copy the number of bytes from the data block's internal buffer to * the user's buffer pointed by pabyDstBuf. * * Passing pabyDstBuf = NULL will only move the read pointer by the * specified number of bytes as if the copy had happened... but it * won't crash. * * Returns 0 if succesful or -1 if an error happened, in which case * UGKError() will have been called. **********************************************************************/int TABRawBinBlock::ReadBytes(int numBytes, UGKByte *pabyDstBuf){ /*---------------------------------------------------------------- * Make sure block is initialized with Read access and that the * operation won't go beyond the buffer's size. *---------------------------------------------------------------*/ if (m_pabyBuf == NULL) { UGKError(ET_Failure, UGKErr_AppDefined, "ReadBytes(): Block has not been initialized."); return -1; } if (m_eAccess != TABRead && m_eAccess != TABReadWrite ) { UGKError(ET_Failure, UGKErr_AppDefined, "ReadBytes(): Block does not support read operations."); return -1; } if (m_nCurPos + numBytes > m_nSizeUsed) { UGKError(ET_Failure, UGKErr_AppDefined, "ReadBytes(): Attempt to read past end of data block."); return -1; } if (pabyDstBuf) { memcpy(pabyDstBuf, m_pabyBuf + m_nCurPos, numBytes); } m_nCurPos += numBytes; return 0;}/********************************************************************** * TABRawBinBlock::Read<datatype>() * * MapInfo files are binary files with LSB first (Intel) byte * ordering. The following functions will read from the input file * and return a value with the bytes ordered properly for the current * platform. **********************************************************************/UGKByte TABRawBinBlock::ReadByte(){ UGKByte byValue; ReadBytes(1, (UGKByte*)(&byValue)); return byValue;}UGKInt16 TABRawBinBlock::ReadInt16(){ UGKInt16 n16Value; ReadBytes(2, (UGKByte*)(&n16Value));#ifdef CPL_MSB return (UGKInt16)CPL_SWAP16(n16Value);#else return n16Value;#endif}UGKInt32 TABRawBinBlock::ReadInt32(){ UGKInt32 n32Value; ReadBytes(4, (UGKByte*)(&n32Value));#ifdef CPL_MSB return (UGKInt32)CPL_SWAP32(n32Value);#else return n32Value;#endif}float TABRawBinBlock::ReadFloat(){ float fValue; ReadBytes(4, (UGKByte*)(&fValue));#ifdef CPL_MSB *(GUInt32*)(&fValue) = CPL_SWAP32(*(GUInt32*)(&fValue));#endif return fValue;}double TABRawBinBlock::ReadDouble(){ double dValue; ReadBytes(8, (UGKByte*)(&dValue)); //===ADD ZGQ 06/04/20 =======#ifdef SITSANGAPP SwapHighLow4Byte(dValue);#endif #ifdef CPL_MSB CPL_SWAPDOUBLE(&dValue);#endif return dValue;}/********************************************************************** * TABRawBinBlock::WriteBytes() * * Copy the number of bytes from the user's buffer pointed by pabySrcBuf * to the data block's internal buffer. * Note that this call only writes to the memory buffer... nothing is * written to the file until WriteToFile() is called. * * Passing pabySrcBuf = NULL will only move the write pointer by the * specified number of bytes as if the copy had happened... but it * won't crash. * * Returns 0 if succesful or -1 if an error happened, in which case * UGKError() will have been called. **********************************************************************/int TABRawBinBlock::WriteBytes(int nBytesToWrite, UGKByte *pabySrcBuf){ /*---------------------------------------------------------------- * Make sure block is initialized with Write access and that the * operation won't go beyond the buffer's size. *---------------------------------------------------------------*/ if (m_pabyBuf == NULL) { UGKError(ET_Failure, UGKErr_AppDefined, "WriteBytes(): Block has not been initialized."); return -1; } if (m_eAccess != TABWrite && m_eAccess != TABReadWrite ) { UGKError(ET_Failure, UGKErr_AppDefined, "WriteBytes(): Block does not support write operations."); return -1; } if (m_nCurPos + nBytesToWrite > m_nBlockSize) { UGKError(ET_Failure, UGKErr_AppDefined, "WriteBytes(): Attempt to write past end of data block."); return -1; } /*---------------------------------------------------------------- * Everything is OK... copy the data *---------------------------------------------------------------*/ if (pabySrcBuf) { memcpy(m_pabyBuf + m_nCurPos, pabySrcBuf, nBytesToWrite); } m_nCurPos += nBytesToWrite; m_nSizeUsed = MAX(m_nSizeUsed, m_nCurPos); m_bModified = TRUE; return 0;}/********************************************************************** * TABRawBinBlock::Write<datatype>() * * Arc/Info files are binary files with MSB first (Motorola) byte * ordering. The following functions will reorder the byte for the * value properly and write that to the output file. * * If a problem happens, then UGKError() will be called and * UGKGetLastErrNo() can be used to test if a write operation was * succesful. **********************************************************************/int TABRawBinBlock::WriteByte(UGKByte byValue){ return WriteBytes(1, (UGKByte*)&byValue);}int TABRawBinBlock::WriteInt16(UGKInt16 n16Value){#ifdef CPL_MSB n16Value = (UGKInt16)CPL_SWAP16(n16Value);#endif return WriteBytes(2, (UGKByte*)&n16Value);}int TABRawBinBlock::WriteInt32(UGKInt32 n32Value){#ifdef CPL_MSB n32Value = (UGKInt32)CPL_SWAP32(n32Value);#endif return WriteBytes(4, (UGKByte*)&n32Value);}int TABRawBinBlock::WriteFloat(float fValue){#ifdef CPL_MSB *(GUInt32*)(&fValue) = CPL_SWAP32(*(GUInt32*)(&fValue));#endif return WriteBytes(4, (UGKByte*)&fValue);}int TABRawBinBlock::WriteDouble(double dValue){#ifdef CPL_MSB CPL_SWAPDOUBLE(&dValue);#endif return WriteBytes(8, (UGKByte*)&dValue);}/********************************************************************** * TABRawBinBlock::WriteZeros() * * Write a number of zeros (sepcified in bytes) at the current position * in the file. * * If a problem happens, then UGKError() will be called and * UGKGetLastErrNo() can be used to test if a write operation was * succesful. **********************************************************************/int TABRawBinBlock::WriteZeros(int nBytesToWrite){ char acZeros[8] = {0, 0, 0, 0, 0, 0, 0, 0}; int i; int nStatus = 0; /* Write by 8 bytes chunks. The last chunk may be less than 8 bytes */ for(i=0; nStatus == 0 && i< nBytesToWrite; i+=8) { nStatus = WriteBytes(MIN(8,(nBytesToWrite-i)), (UGKByte*)acZeros); } return nStatus;}/********************************************************************** * TABRawBinBlock::WritePaddedString() * * Write a string and pad the end of the field (up to nFieldSize) with * spaces number of spaces at the current position in the file. * * If a problem happens, then CPLError() will be called and * CPLGetLastErrNo() can be used to test if a write operation was * succesful. **********************************************************************/int TABRawBinBlock::WritePaddedString(int nFieldSize, const char *pszString){ char acSpaces[8] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; int i, nLen, numSpaces; int nStatus = 0; nLen = strlen(pszString); nLen = MIN(nLen, nFieldSize); numSpaces = nFieldSize - nLen; if (nLen > 0) nStatus = WriteBytes(nLen, (UGKByte*)pszString); /* Write spaces by 8 bytes chunks. The last chunk may be less than 8 bytes */ for(i=0; nStatus == 0 && i< numSpaces; i+=8) { nStatus = WriteBytes(MIN(8,(numSpaces-i)), (UGKByte*)acSpaces); } return nStatus;}/********************************************************************** * TABRawBinBlock::Dump() * * Dump block contents... available only in DEBUG mode. **********************************************************************/void TABRawBinBlock::Dump(FILE *fpOut /*=NULL*/){ if (fpOut == NULL) fpOut = stdout; fprintf(fpOut, "----- TABRawBinBlock::Dump() -----\n"); if (m_pabyBuf == NULL) { fprintf(fpOut, "Block has not been initialized yet."); } else { fprintf(fpOut, "Block (type %d) size=%d bytes at offset %d in file.\n", m_nBlockType, m_nBlockSize, m_nFileOffset); fprintf(fpOut, "Current pointer at byte %d\n", m_nCurPos); } fflush(fpOut);}/********************************************************************** * TABCreateMAPBlockFromFile() * * Load data from the specified file location and create and initialize * a TABMAP*Block of the right type to handle it. * * Returns the new object if succesful or NULL if an error happened, in * which case UGKError() will have been called. **********************************************************************/TABRawBinBlock *TABCreateMAPBlockFromFile(FILE *fpSrc, int nOffset, int nSize /*= 512*/, UGKBool bHardBlockSize /*= TRUE */, TABAccess eAccessMode /*= TABRead*/){ TABRawBinBlock *poBlock = NULL; UGKByte *pabyBuf; if (fpSrc == NULL || nSize == 0) { UGKError(ET_Failure, UGKErr_AssertionFailed, "TABCreateMAPBlockFromFile(): Assertion Failed!"); return NULL; } /*---------------------------------------------------------------- * Alloc a buffer to contain the data *---------------------------------------------------------------*/ pabyBuf = (UGKByte*)UGK_Malloc(nSize*sizeof(UGKByte)); /*---------------------------------------------------------------- * Read from the file *---------------------------------------------------------------*/ if (fseek(fpSrc, nOffset, SEEK_SET) != 0 || fread(pabyBuf, sizeof(UGKByte), nSize, fpSrc)!=(unsigned int)nSize ) { UGKError(ET_Failure, UGKErr_FileIO, "TABCreateMAPBlockFromFile() failed reading %d bytes at offset %d.", nSize, nOffset); UGK_Free(pabyBuf); return NULL; } /*---------------------------------------------------------------- * Create an object of the right type * Header block is different: it does not start with the object * type byte but it is always the first block in a file *---------------------------------------------------------------*/ if (nOffset == 0) { poBlock = new TABMAPHeaderBlock; } else { switch(pabyBuf[0]) { case TABMAP_INDEX_BLOCK: poBlock = new TABMAPIndexBlock(eAccessMode); break; case TABMAP_OBJECT_BLOCK: poBlock = new TABMAPObjectBlock(eAccessMode); break; case TABMAP_COORD_BLOCK: poBlock = new TABMAPCoordBlock(eAccessMode); break; case TABMAP_TOOL_BLOCK: poBlock = new TABMAPToolBlock(eAccessMode); break; case TABMAP_GARB_BLOCK: default: poBlock = new TABRawBinBlock(eAccessMode, bHardBlockSize); break; } } /*---------------------------------------------------------------- * Init new object with the data we just read *---------------------------------------------------------------*/ if (poBlock->InitBlockFromData(pabyBuf, nSize, FALSE, fpSrc, nOffset) != 0) { // Some error happened... and CPLError() has been called delete poBlock; poBlock = NULL; } return poBlock;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -