Beispiel #1
0
/* 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;
}
Beispiel #2
0
static HB_SIZE ct_StrFile( const char * pFileName, const char * pcStr, HB_SIZE nLen,
                           HB_BOOL bAppend, HB_FOFFSET nOffset, HB_BOOL bTrunc )
{
   HB_SIZE nWrite = 0;
   HB_BOOL bFile = hb_fileExists( pFileName, NULL );

   PHB_FILE hFile = hb_fileExtOpen( pFileName, NULL,
                                    FO_WRITE | FO_PRIVATE |
                                    FXO_SHARELOCK |
                                    ( bAppend ? FXO_APPEND : FXO_TRUNCATE ) |
                                    ( ct_getsafety() ? FXO_UNIQUE : 0 ),
                                    NULL, NULL );

   if( hFile )
   {
      if( ! bFile )
         hb_fileAttrSet( pFileName, ct_getfcreate() );

      if( nOffset )
         hb_fileSeek( hFile, nOffset, FS_SET );
      else
         hb_fileSeek( hFile, 0, FS_END );

      nWrite = hb_fileResult( hb_fileWrite( hFile, pcStr, nLen, -1 ) );
      if( nWrite == nLen && bTrunc )
         hb_fileWrite( hFile, NULL, 0, -1 );

      hb_fileClose( hFile );
   }
   return nWrite;
}
Beispiel #3
0
/* 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 ];
}
Beispiel #4
0
/* 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;
}
Beispiel #5
0
/* 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 ];
}
Beispiel #6
0
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;
}