/* function getblock() reads the text file and returns the a block. the variables offset and buffsize tell it where to start reading and how many bytes to try to read. if the block read in would not fill the buffer then the offset is adjusted so that the start or end of of the file is positioned at the head or tail of the buffer. It returns the offset into the file of the first byte of the buffer. */ static HB_FOFFSET getblock( PFT_DISPC dispc, HB_FOFFSET offset ) { /* set the file pointer to the proper offset and if an error occured then check to see if a positive offset was requested, if so then set the pointer to the offset from the end of the file, otherwise set it from the beginning of the file. */ hb_fileSeek( dispc->infile, offset, FS_SET ); /* read in the file and set the buffer bottom variable equal to the number of bytes actually read in. */ dispc->buffbot = hb_fileRead( dispc->infile, dispc->buffer, dispc->buffsize, -1 ); /* if a full buffer's worth was not read in, make it full. */ if( dispc->buffbot != dispc->buffsize && dispc->fsize > dispc->buffsize ) { if( offset > 0 ) hb_fileSeek( dispc->infile, -dispc->buffsize, FS_END ); else hb_fileSeek( dispc->infile, dispc->buffsize, FS_SET ); dispc->buffbot = hb_fileRead( dispc->infile, dispc->buffer, dispc->buffsize, -1 ); } /* return the actual file position */ return hb_fileSeek( dispc->infile, 0, FS_RELATIVE ) - dispc->buffbot; }
HB_BYTE * hb_fileLoadData( PHB_FILE pFile, HB_SIZE nMaxSize, HB_SIZE * pnSize ) { HB_BYTE * pFileBuf = NULL; HB_SIZE nSize = 0, nRead, nBufSize; HB_FOFFSET nFileSize = hb_fileSize( pFile ); if( nFileSize == FS_ERROR || ( nFileSize == 0 && hb_fsError() == HB_FILE_ERR_UNSUPPORTED ) ) { for( nBufSize = 0;; ) { if( nBufSize == nSize ) { nBufSize += nBufSize == 0 ? HB_FILELOAD_BUFFERSIZE : nBufSize >> 1; if( nMaxSize > 0 && nBufSize > nMaxSize ) { nBufSize = nMaxSize; if( nBufSize == nSize ) break; } pFileBuf = ( HB_BYTE * ) hb_xrealloc( pFileBuf, nBufSize ); } nRead = hb_fileRead( pFile, pFileBuf + nSize, nBufSize - nSize, -1 ); if( nRead == 0 || nRead == ( HB_SIZE ) FS_ERROR ) break; nSize += nRead; } }
HB_BOOL hb_fsCopy( const char * pszSource, const char * pszDest ) { HB_BOOL fResult = HB_FALSE; PHB_FILE pSrcFile; if( ( pSrcFile = hb_fileExtOpen( pszSource, NULL, FO_READ | FO_SHARED | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { PHB_FILE pDstFile; HB_ERRCODE errCode; if( ( pDstFile = hb_fileExtOpen( pszDest, NULL, FXO_TRUNCATE | FO_READWRITE | FO_EXCLUSIVE | FXO_SHARELOCK, NULL, NULL ) ) != NULL ) { void * pbyBuffer = hb_xgrab( HB_FSCOPY_BUFFERSIZE ); for( ;; ) { HB_SIZE nBytesRead; if( ( nBytesRead = hb_fileRead( pSrcFile, pbyBuffer, HB_FSCOPY_BUFFERSIZE, -1 ) ) > 0 && nBytesRead != ( HB_SIZE ) FS_ERROR ) { if( nBytesRead != hb_fileWrite( pDstFile, pbyBuffer, nBytesRead, -1 ) ) { errCode = hb_fsError(); break; } } else { errCode = hb_fsError(); fResult = errCode == 0; break; } } hb_xfree( pbyBuffer ); hb_fileClose( pDstFile ); } else errCode = hb_fsError(); hb_fileClose( pSrcFile ); if( fResult ) { HB_FATTR ulAttr; if( hb_fileAttrGet( pszSource, &ulAttr ) ) hb_fileAttrSet( pszDest, ulAttr ); } hb_fsSetError( errCode ); } return fResult; }
static int hb_LZSSxRead( PHB_LZSSX_COMPR pCompr ) { if( pCompr->inBuffPos < pCompr->inBuffRead ) return ( HB_UCHAR ) pCompr->inBuffer[ pCompr->inBuffPos++ ]; if( pCompr->pInput != NULL ) { pCompr->inBuffRead = hb_fileResult( hb_fileRead( pCompr->pInput, pCompr->inBuffer, pCompr->inBuffSize, -1 ) ); pCompr->inBuffPos = 0; if( pCompr->inBuffPos < pCompr->inBuffRead ) return ( HB_UCHAR ) pCompr->inBuffer[ pCompr->inBuffPos++ ]; } return -1; }
static HB_BOOL hb_copyfile( const char * pszSource, const char * pszDest ) { HB_BOOL bRetVal = HB_FALSE; PHB_FILE pSource; PHB_ITEM pError = NULL; HB_TRACE( HB_TR_DEBUG, ( "hb_copyfile(%s, %s)", pszSource, pszDest ) ); do { pSource = hb_fileExtOpen( pszSource, NULL, FO_READ | FO_SHARED | FO_PRIVATE | FXO_DEFAULTS | FXO_SHARELOCK, NULL, pError ); if( pSource == NULL ) { pError = hb_errRT_FileError( pError, NULL, EG_OPEN, 2012, pszSource ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } while( pSource == NULL ); if( pError ) { hb_itemRelease( pError ); pError = NULL; } if( pSource != NULL ) { PHB_FILE pDest; do { pDest = hb_fileExtOpen( pszDest, NULL, FO_READWRITE | FO_EXCLUSIVE | FO_PRIVATE | FXO_TRUNCATE | FXO_DEFAULTS | FXO_SHARELOCK, NULL, pError ); if( pDest == NULL ) { pError = hb_errRT_FileError( pError, NULL, EG_CREATE, 2012, pszDest ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } while( pDest == NULL ); if( pError ) { hb_itemRelease( pError ); pError = NULL; } if( pDest != NULL ) { HB_UCHAR * buffer; HB_SIZE nRead; buffer = ( HB_UCHAR * ) hb_xgrab( BUFFER_SIZE ); bRetVal = HB_TRUE; while( ( nRead = hb_fileRead( pSource, buffer, BUFFER_SIZE, -1 ) ) != 0 ) { HB_SIZE nWritten = 0; while( nWritten < nRead ) { nWritten += hb_fileWrite( pDest, buffer + nWritten, nRead - nWritten, -1 ); if( nWritten < nRead ) { pError = hb_errRT_FileError( pError, NULL, EG_WRITE, 2016, pszDest ); if( hb_errLaunch( pError ) != E_RETRY ) { bRetVal = HB_FALSE; break; } } } } if( pError ) hb_itemRelease( pError ); hb_xfree( buffer ); hb_fileClose( pDest ); } hb_fileClose( pSource ); #if defined( HB_OS_UNIX ) if( bRetVal ) { HB_FATTR ulAttr; if( hb_fileAttrGet( pszSource, &ulAttr ) ) hb_fileAttrSet( pszDest, ulAttr ); } #endif } return bRetVal; }
/* the contents of the inserted bytes are indeterminate, i.e. you'll have to write to them before they mean anything */ static int _ins_buff( PFT_TEXT ft_text, HB_ISIZ iLen ) { char * ReadBuff = ( char * ) hb_xgrab( BUFFSIZE ); char * WriteBuff = ( char * ) hb_xgrab( BUFFSIZE ); char * SaveBuff; HB_FOFFSET fpRead, fpWrite; HB_ISIZ WriteLen, ReadLen; HB_ISIZ SaveLen; HB_ISIZ iLenRemaining = iLen; /* set target move distance, this allows iLen to be greater than BUFFSIZE */ iLen = HB_MIN( iLenRemaining, BUFFSIZE ); iLenRemaining -= iLen; /* initialize file pointers */ fpRead = ft_text->offset[ ft_text->area ]; fpWrite = ft_text->offset[ ft_text->area ] + iLen; /* do initial load of both buffers */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; ReadLen = hb_fileResult( hb_fileRead( ft_text->handles[ ft_text->area ], ReadBuff, BUFFSIZE, -1 ) ); fpRead += ReadLen; ft_text->error[ ft_text->area ] = 0; while( ! ft_text->error[ ft_text->area ] && iLen > 0 ) { while( WriteLen > 0 ) { /* position to beginning of write area */ if( hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ) != fpWrite ) { ft_text->error[ ft_text->area ] = hb_fsError(); break; } if( ( SaveLen = hb_fileResult( hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, WriteLen, -1 ) ) ) == 0 ) { ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* move write pointer */ fpWrite += SaveLen; if( SaveLen != WriteLen ) { /* error, fetch errcode and quit */ ft_text->error[ ft_text->area ] = hb_fsError(); break; } #if 0 WriteLen = SaveLen; #endif /* swap buffers */ SaveBuff = WriteBuff; WriteBuff = ReadBuff; ReadBuff = SaveBuff; WriteLen = ReadLen; /* return to read area and read another buffer */ ReadLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], ReadBuff, BUFFSIZE, fpRead ) ); fpRead += ReadLen; } iLen = HB_MIN( iLenRemaining, BUFFSIZE ); iLenRemaining -= iLen; } /* store length in bytes, set legacy EOF marker */ ft_text->lastbyte[ ft_text->area ] = hb_fileSeek( ft_text->handles[ ft_text->area ], fpWrite, FS_SET ); hb_fileWrite( ft_text->handles[ ft_text->area ], WriteBuff, 0, -1 ); /* clear last_rec so next gobot will recount the records */ ft_text->last_rec[ ft_text->area ] = 0; hb_fileSeek( ft_text->handles[ ft_text->area ], ft_text->offset[ ft_text->area ], FS_SET ); hb_xfree( ReadBuff ); hb_xfree( WriteBuff ); return ft_text->error[ ft_text->area ]; }
static char * s_ReadLine( PHB_FILE hFileHandle, HB_ISIZ * plBuffLen, const char ** pTerm, HB_ISIZ * pnTermSizes, HB_ISIZ nTerms, HB_BOOL * pbFound, HB_BOOL * pbEOF ) { HB_ISIZ nPosTerm = 0, nPos, nPosition; int nTries; HB_ISIZ nRead = 0, nOffset, nSize; char * pBuff; HB_TRACE( HB_TR_DEBUG, ( "s_ReadLine(%p, %" HB_PFS "d, %p, %p, %" HB_PFS "d, %p, %p)", hFileHandle, *plBuffLen, pTerm, pnTermSizes, nTerms, pbFound, pbEOF ) ); *pbFound = HB_FALSE; *pbEOF = HB_FALSE; nTries = 0; nOffset = 0; nSize = *plBuffLen; if( *plBuffLen < 10 ) *plBuffLen = READING_BLOCK; pBuff = ( char * ) hb_xgrab( *plBuffLen + 1 ); do { if( nTries > 0 ) { /* pBuff can be enlarged to hold the line as needed.. */ nSize = ( *plBuffLen * ( nTries + 1 ) ) + 1; pBuff = ( char * ) hb_xrealloc( pBuff, nSize ); nOffset += nRead; } /* read from file */ nRead = hb_fileRead( hFileHandle, pBuff + nOffset, nSize - nOffset, -1 ); /* scan the read buffer */ if( nRead > 0 ) { for( nPos = 0; nPos < nRead; nPos++ ) { for( nPosTerm = 0; nPosTerm < nTerms; nPosTerm++ ) { /* Compare with the LAST terminator byte */ if( pBuff[ nOffset + nPos ] == pTerm[ nPosTerm ][ pnTermSizes[ nPosTerm ] - 1 ] && ( pnTermSizes[ nPosTerm ] - 1 ) <= ( nPos + nOffset ) ) { *pbFound = HB_TRUE; for( nPosition = 0; nPosition < ( pnTermSizes[ nPosTerm ] - 1 ); nPosition++ ) { if( pTerm[ nPosTerm ][ nPosition ] != pBuff[ nOffset + ( nPos - pnTermSizes[ nPosTerm ] ) + nPosition + 1 ] ) { *pbFound = HB_FALSE; break; } } if( *pbFound ) break; } } if( *pbFound ) break; } if( *pbFound ) { *plBuffLen = nOffset + nPos - pnTermSizes[ nPosTerm ] + 1; pBuff[ *plBuffLen ] = '\0'; /* Set handle pointer in the end of the line */ hb_fileSeek( hFileHandle, ( ( nRead - nPos ) * -1 ) + 1, FS_RELATIVE ); return pBuff; } } else { if( ! *pbFound ) { if( nTries == 0 ) { pBuff[ 0 ] = '\0'; *plBuffLen = 0; } else { pBuff[ nOffset + nRead ] = '\0'; *plBuffLen = nOffset + nRead; } *pbEOF = HB_TRUE; } } nTries++; } while( ! *pbFound && nRead > 0 ); return pBuff; }
static HB_ERRCODE hb_sdfReadRecord( SDFAREAP pArea ) { HB_SIZE nRead; HB_TRACE( HB_TR_DEBUG, ( "hb_sdfReadRecord(%p)", pArea ) ); pArea->area.fEof = HB_TRUE; nRead = 0; for( ;; ) { char ch; if( pArea->nBufferRead - pArea->nBufferIndex < ( HB_SIZE ) pArea->uiEolLen + 1 && pArea->nBufferRead == pArea->nBufferSize ) { HB_SIZE nLeft = pArea->nBufferRead - pArea->nBufferIndex; if( nLeft ) memmove( pArea->pBuffer, pArea->pBuffer + pArea->nBufferIndex, nLeft ); pArea->nBufferIndex = 0; pArea->nBufferRead = hb_fileRead( pArea->pFile, pArea->pBuffer + nLeft, pArea->nBufferSize - nLeft, -1 ); if( pArea->nBufferRead == ( HB_SIZE ) FS_ERROR ) pArea->nBufferRead = 0; pArea->nBufferRead += nLeft; } if( pArea->nBufferIndex >= pArea->nBufferRead ) break; ch = pArea->pBuffer[ pArea->nBufferIndex++ ]; if( pArea->fAnyEol ) { if( ch == '\r' || ch == '\n' ) { if( pArea->nBufferIndex < pArea->nBufferRead && pArea->pBuffer[ pArea->nBufferIndex ] != ch && ( pArea->pBuffer[ pArea->nBufferIndex ] == '\r' || pArea->pBuffer[ pArea->nBufferIndex ] == '\n' ) ) pArea->nBufferIndex++; pArea->area.fEof = HB_FALSE; break; } } else if( ch == pArea->szEol[ 0 ] ) { if( pArea->uiEolLen == 1 || ( pArea->nBufferRead - pArea->nBufferIndex >= ( HB_SIZE ) pArea->uiEolLen - 1 && memcmp( pArea->pBuffer + pArea->nBufferIndex, pArea->szEol + 1, pArea->uiEolLen - 1 ) == 0 ) ) { pArea->nBufferIndex += pArea->uiEolLen - 1; pArea->area.fEof = HB_FALSE; break; } } if( nRead < ( HB_SIZE ) pArea->uiRecordLen && ch != '\032' ) pArea->pRecord[ nRead++ ] = ch; } if( nRead < ( HB_SIZE ) pArea->uiRecordLen ) memset( pArea->pRecord + nRead, ' ', pArea->uiRecordLen - nRead ); if( nRead > 0 ) pArea->area.fEof = HB_FALSE; pArea->fPositioned = ! pArea->area.fEof; return HB_SUCCESS; }