/* deletes xxx bytes from the current file, beginning at the current record */ static int _del_buff( PFT_TEXT ft_text, HB_ISIZ iLen ) { char * WriteBuff = ( char * ) hb_xgrab( BUFFSIZE ); HB_FOFFSET fpRead, fpWrite; HB_ISIZ WriteLen; /* initialize file pointers */ fpWrite = ft_text->offset[ ft_text->area ]; fpRead = ft_text->offset[ ft_text->area ] + iLen; /* do initial load of buffer */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; ft_text->error[ ft_text->area ] = 0; while( WriteLen > 0 ) { HB_ISIZ SaveLen; /* position to beginning of write area */ SaveLen = hb_fileResult( hb_fileWriteAt( ft_text->handles[ ft_text->area ], WriteBuff, WriteLen, fpWrite ) ); /* move write pointer */ fpWrite += SaveLen; if( SaveLen != WriteLen ) { /* error, fetch errcode and quit */ ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* return to read area and read another buffer */ WriteLen = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], WriteBuff, BUFFSIZE, fpRead ) ); fpRead += WriteLen; } /* 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( WriteBuff ); return ft_text->error[ ft_text->area ]; }
static HB_ERRCODE hb_sdfReadRecord( SDFAREAP pArea ) { HB_USHORT uiRead, uiToRead, uiEolPos; HB_TRACE( HB_TR_DEBUG, ( "hb_sdfReadRecord(%p)", pArea ) ); uiToRead = pArea->uiRecordLen + pArea->uiEolLen + 2; uiRead = ( HB_USHORT ) hb_fileReadAt( pArea->pFile, pArea->pRecord, uiToRead, pArea->nRecordOffset ); if( uiRead > 0 && uiRead < uiToRead && pArea->pRecord[ uiRead - 1 ] == '\032' ) --uiRead; if( uiRead == 0 ) { pArea->area.fEof = HB_TRUE; pArea->fPositioned = HB_FALSE; hb_sdfClearRecordBuffer( pArea ); } else { pArea->area.fEof = HB_FALSE; pArea->fPositioned = HB_TRUE; uiEolPos = ( HB_USHORT ) hb_strAt( pArea->szEol, pArea->uiEolLen, ( const char * ) pArea->pRecord, uiRead ); if( uiEolPos ) { --uiEolPos; if( uiRead < uiToRead && uiRead == uiEolPos + pArea->uiEolLen ) pArea->nNextOffset = ( HB_FOFFSET ) -1; else pArea->nNextOffset = pArea->nRecordOffset + uiEolPos + pArea->uiEolLen; if( uiEolPos < pArea->uiRecordLen ) memset( pArea->pRecord + uiEolPos, ' ', pArea->uiRecordLen - uiEolPos ); } else { if( uiRead < uiToRead ) pArea->nNextOffset = ( HB_FOFFSET ) -1; else pArea->nNextOffset = 0; if( uiRead < pArea->uiRecordLen ) memset( pArea->pRecord + uiRead, ' ', pArea->uiRecordLen - uiRead ); } if( uiEolPos != pArea->uiRecordLen ) memcpy( pArea->pRecord + pArea->uiRecordLen, pArea->szEol, pArea->uiEolLen ); } return HB_SUCCESS; }
static PHRB_BODY hb_hrbLoadFromFile( const char * szHrb, HB_USHORT usMode ) { PHRB_BODY pHrbBody = NULL; PHB_ITEM pError = NULL; PHB_FILE pFile; /* Open as binary */ do { pFile = hb_fileExtOpen( szHrb, hb_stackSetStruct()->HB_SET_DEFEXTENSIONS ? ".hrb" : NULL, FO_READ | FXO_SHARELOCK, NULL, pError ); if( pFile == NULL ) { pError = hb_errRT_FileError( pError, NULL, EG_OPEN, 6102, szHrb ); if( hb_errLaunch( pError ) != E_RETRY ) break; } } while( pFile == NULL ); if( pError ) hb_itemRelease( pError ); if( pFile != NULL ) { HB_SIZE nBodySize = ( HB_SIZE ) hb_fileSize( pFile ); if( nBodySize ) { char * pbyBuffer; pbyBuffer = ( char * ) hb_xgrab( nBodySize + sizeof( char ) + 1 ); hb_fileReadAt( pFile, pbyBuffer, nBodySize, 0 ); pbyBuffer[ nBodySize ] = '\0'; pHrbBody = hb_hrbLoad( ( const char * ) pbyBuffer, nBodySize, usMode, szHrb ); hb_xfree( pbyBuffer ); } hb_fileClose( pFile ); } return pHrbBody; }
static void hb_memoread( HB_BOOL bHandleEOF ) { const char * pszFileName = hb_parc( 1 ); if( pszFileName ) { PHB_FILE pFile = hb_fileExtOpen( pszFileName, NULL, FO_READ | FO_SHARED | FO_PRIVATE | FXO_SHARELOCK | FXO_NOSEEKPOS, NULL, NULL ); if( pFile != NULL ) { HB_SIZE nSize = ( HB_SIZE ) hb_fileSize( pFile ); if( nSize != 0 ) { char * pbyBuffer = ( char * ) hb_xgrab( nSize + 1 ); nSize = hb_fileReadAt( pFile, pbyBuffer, nSize, 0 ); /* Don't read the file terminating EOF character */ if( bHandleEOF && nSize > 0 ) { if( pbyBuffer[ nSize - 1 ] == HB_CHAR_EOF ) --nSize; } hb_retclen_buffer( pbyBuffer, nSize ); } else hb_retc_null(); hb_fileClose( pFile ); } else hb_retc_null(); } else hb_retc_null(); }
static int hb_delimNextChar( DELIMAREAP pArea ) { if( pArea->ulBufferIndex + pArea->uiEolLen >= pArea->ulBufferRead && ( pArea->ulBufferRead == 0 || pArea->ulBufferRead >= pArea->ulBufferSize - 1 ) ) { ULONG ulLeft = pArea->ulBufferRead - pArea->ulBufferIndex; if( ulLeft ) memcpy( pArea->pBuffer, pArea->pBuffer + pArea->ulBufferIndex, ulLeft ); pArea->ulBufferStart += pArea->ulBufferIndex; pArea->ulBufferIndex = 0; pArea->ulBufferRead = hb_fileReadAt( pArea->pFile, pArea->pBuffer + ulLeft, pArea->ulBufferSize - ulLeft, pArea->ulBufferStart + ulLeft ); if( pArea->ulBufferRead > 0 && pArea->pBuffer[ pArea->ulBufferRead + ulLeft - 1 ] == '\032' ) pArea->ulBufferRead--; pArea->ulBufferRead += ulLeft; } if( pArea->ulBufferIndex + pArea->uiEolLen <= pArea->ulBufferRead && memcmp( pArea->pBuffer + pArea->ulBufferIndex, pArea->szEol, pArea->uiEolLen ) == 0 ) { pArea->ulBufferIndex += pArea->uiEolLen; pArea->ulNextOffset = pArea->ulBufferStart + pArea->ulBufferIndex; return -1; } else if( pArea->ulBufferIndex < pArea->ulBufferRead ) { return pArea->pBuffer[ pArea->ulBufferIndex++ ]; } pArea->ulNextOffset = ( HB_FOFFSET ) -1; return -2; }
/* internal routine to do buffer skips. Passing a positive value performs a downward skip, a negative number does an upward skip. Passing 0 skips to the end of file. Returns a long indicating the number of records skipped */ static long _ft_skip( long iRecs ) { PFT_TEXT ft_text = ( PFT_TEXT ) hb_stackGetTSD( &s_ft_text ); HB_ISIZ iByteCount; HB_ISIZ iBytesRead, iBytesRemaining; char * cPtr; long iSkipped = 0; char * cBuff = ( char * ) hb_xgrab( BUFFSIZE ); HB_FOFFSET fpOffset = ft_text->offset[ ft_text->area ]; ft_text->isBof[ ft_text->area ] = HB_FALSE; ft_text->isEof[ ft_text->area ] = HB_FALSE; ft_text->error[ ft_text->area ] = 0; /* iRecs is zero if they want to find the EOF, start a top of file */ if( iRecs == 0 ) { fpOffset = 0; ft_text->recno[ ft_text->area ] = 1; } if( iRecs >= 0 ) { do { cPtr = cBuff; /* read a chunk */ if( ( iBytesRead = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], cBuff, BUFFSIZE, fpOffset ) ) ) == 0 ) { /* buffer is empty thus EOF, set vars and quit */ ft_text->isEof[ ft_text->area ] = HB_TRUE; ft_text->last_rec[ ft_text->area ] = ft_text->recno[ ft_text->area ]; ft_text->last_off[ ft_text->area ] = ft_text->offset[ ft_text->area ]; ft_text->error[ ft_text->area ] = hb_fsError(); break; } iBytesRemaining = iBytesRead; /* parse the buffer while there's still stuff in it */ do { /* get count of chars in this line */ iByteCount = _findeol( cPtr, iBytesRemaining, NULL ); if( iByteCount > 0 && iByteCount != iBytesRemaining ) { /* found an EOL, iByteCount points to first char of next record */ iBytesRemaining -= iByteCount; fpOffset += iByteCount; cPtr += iByteCount; ft_text->offset[ ft_text->area ] = fpOffset; ft_text->recno[ ft_text->area ]++; iSkipped++; if( iRecs && ( iSkipped == iRecs ) ) iBytesRemaining = iBytesRead = 0; } else { /* no more EOLs in this buffer, or EOL is last chars in the buffer */ /* check for EOF */ if( iBytesRead != BUFFSIZE ) { /* buffer was not full, thus EOF, set vars and quit */ iBytesRemaining = 0; ft_text->last_rec[ ft_text->area ] = ft_text->recno[ ft_text->area ]; ft_text->last_off[ ft_text->area ] = ft_text->offset[ ft_text->area ]; if( iRecs ) ft_text->isEof[ ft_text->area ] = HB_TRUE; } else { /* buffer was full, so probably not EOF, but maybe EOL straddled end of buffer, so back up pointer a bit before doing the next read */ fpOffset = hb_fileSeek( ft_text->handles[ ft_text->area ], 0, FS_RELATIVE ) - 1; iBytesRemaining = 0; } } } while( iBytesRemaining > 0 ); } while( iBytesRead == BUFFSIZE ); } else { /* skip backwards */ iRecs = -iRecs; if( ft_text->recno[ ft_text->area ] > iRecs ) { do { /* calc offset to read area of file ahead of current pointer */ fpOffset = HB_MAX( ft_text->offset[ ft_text->area ] - BUFFSIZE, 0 ); /* read a chunk */ if( ( iBytesRead = hb_fileResult( hb_fileReadAt( ft_text->handles[ ft_text->area ], cBuff, BUFFSIZE, fpOffset ) ) ) == 0 ) { /* buffer is empty thus file is zero len, set vars and quit */ ft_text->isBof[ ft_text->area ] = HB_TRUE; ft_text->isEof[ ft_text->area ] = HB_TRUE; ft_text->recno[ ft_text->area ] = 0; ft_text->offset[ ft_text->area ] = 0; ft_text->last_rec[ ft_text->area ] = 0; ft_text->error[ ft_text->area ] = hb_fsError(); break; } /* set pointer within buffer */ iBytesRemaining = ( int ) ( ft_text->offset[ ft_text->area ] - fpOffset ); cPtr = cBuff + iBytesRemaining; /* parse the buffer while there's still stuff in it */ do { /* get count of chars in this line */ iByteCount = _findbol( cPtr, iBytesRemaining ); if( iByteCount > 0 ) { /* found an EOL, iByteCount points to first char of next record */ iBytesRemaining -= iByteCount; ft_text->offset[ ft_text->area ] -= iByteCount; cPtr -= iByteCount; fpOffset = ft_text->offset[ ft_text->area ]; ft_text->recno[ ft_text->area ]--; iSkipped++; if( iSkipped == iRecs ) iBytesRemaining = iBytesRead = 0; } else { /* no more EOLs in this buffer so we're either at BOF or record crosses buffer boundary */ /* check for BOF */ if( iBytesRead != BUFFSIZE ) { /* buffer was not full, thus BOF, set vars and quit */ iBytesRemaining = 0; ft_text->offset[ ft_text->area ] = 0; ft_text->recno[ ft_text->area ] = 1; ft_text->isBof[ ft_text->area ] = HB_TRUE; } else { /* buffer was full, so not BOF */ iBytesRemaining = 0; } } } while( iBytesRemaining > 0 ); } while( fpOffset > 0 && iBytesRead == BUFFSIZE ); } else { ft_text->offset[ ft_text->area ] = 0; ft_text->recno[ ft_text->area ] = 1; ft_text->isBof[ ft_text->area ] = HB_TRUE; } } hb_xfree( cBuff ); return iSkipped; }
/* 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 HB_ERRCODE hb_sdfNextRecord( SDFAREAP pArea ) { HB_TRACE( HB_TR_DEBUG, ( "hb_sdfNextRecord(%p)", pArea ) ); if( ! pArea->fPositioned ) pArea->nNextOffset = ( HB_FOFFSET ) -1; else { if( pArea->nNextOffset == 0 ) { HB_USHORT uiRead, uiToRead, uiEolPos, uiRest = 0; HB_FOFFSET ulOffset = pArea->nRecordOffset; uiToRead = pArea->uiRecordLen + pArea->uiEolLen + 2; do { uiRead = ( HB_USHORT ) hb_fileReadAt( pArea->pFile, pArea->pRecord + uiRest, uiToRead - uiRest, ulOffset + uiRest ) + uiRest; if( uiRead > 0 && uiRead < uiToRead && pArea->pRecord[ uiRead - 1 ] == '\032' ) --uiRead; uiEolPos = ( HB_USHORT ) hb_strAt( pArea->szEol, pArea->uiEolLen, ( const char * ) pArea->pRecord, uiRead ); if( uiEolPos ) { --uiEolPos; if( uiRead < uiToRead && uiRead == uiEolPos + pArea->uiEolLen ) pArea->nNextOffset = ( HB_FOFFSET ) -1; else pArea->nNextOffset = ulOffset + uiEolPos + pArea->uiEolLen; } else if( uiRead < uiToRead ) { pArea->nNextOffset = ( HB_FOFFSET ) -1; } else { if( pArea->uiEolLen > 1 ) { uiRest = pArea->uiEolLen - 1; memcpy( pArea->pRecord, pArea->pRecord + uiRead - uiRest, uiRest ); } ulOffset += uiRead - uiRest; } } while( pArea->nNextOffset == 0 ); } pArea->ulRecNo++; } if( pArea->nNextOffset == ( HB_FOFFSET ) -1 ) { pArea->area.fEof = HB_TRUE; pArea->fPositioned = HB_FALSE; hb_sdfClearRecordBuffer( pArea ); return HB_SUCCESS; } pArea->nRecordOffset = pArea->nNextOffset; return hb_sdfReadRecord( pArea ); }