home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / biblioteki / asyncio / src / fgetslenasync.c < prev    next >
C/C++ Source or Header  |  1997-06-28  |  2KB  |  99 lines

  1. #include "async.h"
  2.  
  3.  
  4. _LIBCALL APTR
  5. FGetsLenAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buf,
  6.     _REG( d0 ) LONG numBytes, _REG( a2 ) LONG *len )
  7. {
  8.     UBYTE    *p = ( UBYTE * ) buf;
  9.     LONG    length = 0;
  10.  
  11.     --numBytes;    /*  room for \n and \0  */
  12.  
  13.     for( ;; )
  14.     {
  15.         UBYTE    *ptr;
  16.         LONG    i, count;
  17.  
  18.         ptr = ( UBYTE * ) file->af_Offset;
  19.  
  20.         if( count = file->af_BytesLeft )
  21.         {
  22.             count = MIN( count, numBytes );
  23.  
  24.             for( i = 0; ( i < count ) && ( *ptr != '\n' ); ++i )
  25.             {
  26.                 *p++ = *ptr++;
  27.             }
  28.  
  29.             length += i;
  30.  
  31.             /* Check for valid EOL char */
  32.             if( i < count )
  33.             {
  34.                 /* MH: Since i < count, and count <= numBytes,
  35.                  * there _is_ room for \n\0.
  36.                  */
  37.                 *p++ = '\n';
  38.                 ++i;
  39.                 length += 1;
  40.             }
  41.  
  42.             file->af_BytesLeft -= i;
  43.             file->af_Offset    += i;
  44.  
  45.             if( ( i >= numBytes ) || ( *( p - 1 ) == '\n' ) )
  46.             {
  47.                 /* MH: It is enough to break out of the loop.
  48.                  * no need to "waste" code by making a special
  49.                  * exit here. ;)
  50.                  */
  51.                 break;
  52.             }
  53.  
  54.             numBytes -= i;
  55.         }
  56.  
  57.         /* MH: numBytes must be at least 1 here, so there is still room
  58.          * for \n\0, in case we read \n.
  59.          */
  60.  
  61.         if( ReadAsync( file, p, 1 ) < 1 )
  62.         {
  63.             break;
  64.         }
  65.  
  66.         --numBytes;
  67.         ++length;
  68.  
  69.         if( *p++ == '\n' )
  70.         {
  71.             break;
  72.         }
  73.     }
  74.  
  75.     *p = '\0';
  76.     *len = length;
  77.  
  78.     if( p == ( UBYTE * ) buf )
  79.     {
  80. #ifdef ASIO_NOEXTERNALS
  81.         struct DosLibrary    *DOSBase = file->af_DOSBase;
  82. #endif
  83.  
  84.         SetIoErr( file->af_Packet.sp_Pkt.dp_Res2 );
  85.         return( NULL );
  86.     }
  87.  
  88.     return( buf );
  89. }
  90.  
  91.  
  92. _CALL APTR
  93. FGetsAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buf, _REG( d0 ) LONG numBytes )
  94. {
  95.     LONG    len;
  96.  
  97.     return( FGetsLenAsync( file, buf, numBytes, &len ) );
  98. }
  99.