home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / asyncio / src / readlineasync.c < prev    next >
C/C++ Source or Header  |  1997-02-18  |  1KB  |  64 lines

  1. #include "async.h"
  2.  
  3.  
  4. _LIBCALL LONG
  5. ReadLineAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buffer, _REG( d0 ) LONG bufSize )
  6. {
  7.     LONG    len;
  8.  
  9.     /* First read any data up to the LF or the buffer is full */
  10.     if( FGetsLenAsync( file, buffer, bufSize, &len ) )
  11.     {
  12.         UBYTE    *end;
  13.  
  14.         end = ( ( UBYTE * ) buffer ) + len - 1;
  15.  
  16.         if( *end != '\n' )
  17.         {
  18.             /* We didn't reach EOF yet */
  19.             while( 1 )
  20.             {
  21.                 UBYTE    *ptr, ch;
  22.                 LONG    i, count;
  23.  
  24.                 ptr = ( UBYTE * ) file->af_Offset;
  25.  
  26.                 if( count = file->af_BytesLeft )
  27.                 {
  28.                     /* Scan for LF char in buffer */
  29.                     for( i = 0; ( i < count ) && ( *ptr != '\n' ); ++i, ++ptr )
  30.                     {
  31.                     }
  32.  
  33.                     /* If i < count, then the loop above aborted
  34.                      * due to LF char.
  35.                      */
  36.                     if( i < count )
  37.                     {
  38.                         ch = '\n';
  39.                         ++i;
  40.                     }
  41.                     else if( ReadAsync( file, &ch, 1 ) <= 0 )
  42.                     {
  43.                         break;
  44.                     }
  45.  
  46.                     file->af_BytesLeft -= i;
  47.                     file->af_Offset    += i;
  48.  
  49.                     if( ch == '\n' )
  50.                     {
  51.                         /* Overwrite last char with LF */
  52.                         ++end;
  53.                         *end++ = '\n';
  54.                         *end = '\0';
  55.                         break;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.     }
  61.  
  62.     return( len );
  63. }
  64.