?? avc_rawbin.c
字號:
memcpy(pBuf, pszConvBuf, nBytesToRead); }}/********************************************************************** * AVCRawBinFSeek() * * Move the read pointer to the specified location. * * As with fseek(), the specified position can be relative to the * beginning of the file (SEEK_SET), or the current position (SEEK_CUR). * SEEK_END is not supported. **********************************************************************/void AVCRawBinFSeek(AVCRawBinFile *psFile, int nOffset, int nFrom){ int nTarget = 0; CPLAssert(nFrom == SEEK_SET || nFrom == SEEK_CUR); /* Supported only with read access for now */ CPLAssert(psFile && psFile->eAccess != AVCWrite); if (psFile == NULL || psFile->eAccess == AVCWrite) return; /* Compute destination relative to current memory buffer */ if (nFrom == SEEK_SET) nTarget = nOffset - psFile->nOffset; else if (nFrom == SEEK_CUR) nTarget = nOffset + psFile->nCurPos; /* Is the destination located inside the current buffer? */ if (nTarget > 0 && nTarget <= psFile->nCurSize) { /* Requested location is already in memory... just move the * read pointer */ psFile->nCurPos = nTarget; } else { /* Requested location is not part of the memory buffer... * move the FILE * to the right location and be ready to * read from there. */ VSIFSeek(psFile->fp, psFile->nOffset+nTarget, SEEK_SET); psFile->nCurPos = 0; psFile->nCurSize = 0; psFile->nOffset = psFile->nOffset+nTarget; }}/********************************************************************** * AVCRawBinEOF() * * Return TRUE if there is no more data to read from the file or * FALSE otherwise. **********************************************************************/GBool AVCRawBinEOF(AVCRawBinFile *psFile){ if (psFile == NULL || psFile->fp == NULL) return TRUE; /* In write access mode, always return TRUE, since we always write * at EOF for now. */ if (psFile->eAccess != AVCRead && psFile->eAccess != AVCReadWrite) return TRUE; /* If file data size was specified, then check that we have not * passed that point yet... */ if (psFile->nFileDataSize > 0 && (psFile->nOffset+psFile->nCurPos) >= psFile->nFileDataSize) return TRUE; /* If the file pointer has been moved by AVCRawBinFSeek(), then * we may be at a position past EOF, but VSIFeof() would still * return FALSE. * To prevent this situation, if the memory buffer is empty, * we will try to read 1 byte from the file to force the next * chunk of data to be loaded (and we'll move the the read pointer * back by 1 char after of course!). * If we are at the end of the file, this will trigger the EOF flag. */ if (psFile->nCurPos == 0 && psFile->nCurSize == 0) { char c; /* Set bDisableReadBytesEOFError=TRUE to temporarily disable * the EOF error message from AVCRawBinReadBytes(). */ bDisableReadBytesEOFError = TRUE; AVCRawBinReadBytes(psFile, 1, &c); bDisableReadBytesEOFError = FALSE; if (psFile->nCurPos > 0) AVCRawBinFSeek(psFile, -1, SEEK_CUR); } return (psFile->nCurPos == psFile->nCurSize && VSIFEof(psFile->fp));}/********************************************************************** * AVCRawBinRead<datatype>() * * Arc/Info files are binary files with MSB first (Motorola) byte * ordering. The following functions will read from the input file * and return a value with the bytes ordered properly for the current * platform. **********************************************************************/GInt16 AVCRawBinReadInt16(AVCRawBinFile *psFile){ GInt16 n16Value; AVCRawBinReadBytes(psFile, 2, (GByte*)(&n16Value)); if (psFile->eByteOrder != geSystemByteOrder) { return (GInt16)CPL_SWAP16(n16Value); } return n16Value;}GInt32 AVCRawBinReadInt32(AVCRawBinFile *psFile){ GInt32 n32Value; AVCRawBinReadBytes(psFile, 4, (GByte*)(&n32Value)); if (psFile->eByteOrder != geSystemByteOrder) { return (GInt32)CPL_SWAP32(n32Value); } return n32Value;}float AVCRawBinReadFloat(AVCRawBinFile *psFile){ float fValue; AVCRawBinReadBytes(psFile, 4, (GByte*)(&fValue)); if (psFile->eByteOrder != geSystemByteOrder) { CPL_SWAP32PTR( &fValue ); } return fValue;}double AVCRawBinReadDouble(AVCRawBinFile *psFile){ double dValue; AVCRawBinReadBytes(psFile, 8, (GByte*)(&dValue)); if (psFile->eByteOrder != geSystemByteOrder) { CPL_SWAPDOUBLE(&dValue); } return dValue;}/********************************************************************** * AVCRawBinWriteBytes() * * Write the number of bytes from the buffer to the file. * * If a problem happens, then CPLError() will be called and * CPLGetLastErrNo() can be used to test if a write operation was * succesful. **********************************************************************/void AVCRawBinWriteBytes(AVCRawBinFile *psFile, int nBytesToWrite, GByte *pBuf){ /*---------------------------------------------------------------- * Make sure file is opened with Write access *---------------------------------------------------------------*/ if (psFile == NULL || (psFile->eAccess != AVCWrite && psFile->eAccess != AVCReadWrite)) { CPLError(CE_Failure, CPLE_FileIO, "AVCRawBinWriteBytes(): call not compatible with access mode."); return; } if (VSIFWrite(pBuf, nBytesToWrite, 1, psFile->fp) != 1) CPLError(CE_Failure, CPLE_FileIO, "Writing to %s failed.", psFile->pszFname); /*---------------------------------------------------------------- * In write mode, we keep track of current file position ( =nbr of * bytes written) through psFile->nCurPos *---------------------------------------------------------------*/ psFile->nCurPos += nBytesToWrite;}/********************************************************************** * AVCRawBinWrite<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 CPLError() will be called and * CPLGetLastErrNo() can be used to test if a write operation was * succesful. **********************************************************************/void AVCRawBinWriteInt16(AVCRawBinFile *psFile, GInt16 n16Value){ if (psFile->eByteOrder != geSystemByteOrder) { n16Value = (GInt16)CPL_SWAP16(n16Value); } AVCRawBinWriteBytes(psFile, 2, (GByte*)&n16Value);}void AVCRawBinWriteInt32(AVCRawBinFile *psFile, GInt32 n32Value){ if (psFile->eByteOrder != geSystemByteOrder) { n32Value = (GInt32)CPL_SWAP32(n32Value); } AVCRawBinWriteBytes(psFile, 4, (GByte*)&n32Value);}void AVCRawBinWriteFloat(AVCRawBinFile *psFile, float fValue){ if (psFile->eByteOrder != geSystemByteOrder) { CPL_SWAP32PTR( &fValue ); } AVCRawBinWriteBytes(psFile, 4, (GByte*)&fValue);}void AVCRawBinWriteDouble(AVCRawBinFile *psFile, double dValue){ if (psFile->eByteOrder != geSystemByteOrder) { CPL_SWAPDOUBLE(&dValue); } AVCRawBinWriteBytes(psFile, 8, (GByte*)&dValue);}/********************************************************************** * AVCRawBinWriteZeros() * * Write a number of zeros (sepcified in bytes) 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. **********************************************************************/void AVCRawBinWriteZeros(AVCRawBinFile *psFile, int nBytesToWrite){ char acZeros[8] = {0, 0, 0, 0, 0, 0, 0, 0}; int i; /* Write by 8 bytes chunks. The last chunk may be less than 8 bytes */ for(i=0; i< nBytesToWrite; i+=8) { AVCRawBinWriteBytes(psFile, MIN(8,(nBytesToWrite-i)), (GByte*)acZeros); }}/********************************************************************** * AVCRawBinWritePaddedString() * * 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. **********************************************************************/void AVCRawBinWritePaddedString(AVCRawBinFile *psFile, int nFieldSize, const char *pszString){ char acSpaces[8] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '}; int i, nLen, numSpaces; /* If we're on a system with a multibyte codepage then we have to * convert strings to the proper multibyte encoding. */ pszString = AVCE00Convert2ArcDBCS(psFile->psDBCSInfo, pszString, nFieldSize); nLen = strlen(pszString); nLen = MIN(nLen, nFieldSize); numSpaces = nFieldSize - nLen; if (nLen > 0) AVCRawBinWriteBytes(psFile, nLen, (GByte*)pszString); /* Write spaces by 8 bytes chunks. The last chunk may be less than 8 bytes */ for(i=0; i< numSpaces; i+=8) { AVCRawBinWriteBytes(psFile, MIN(8,(numSpaces-i)), (GByte*)acSpaces); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -