Start of port of AsyncIO library.
[AROS-Contrib.git] / workbench / libs / asyncio / src / ReadLineAsync.c
blobcae7a8ff6c520e61b7bd694f89e99e44cc412764
1 #include "async.h"
4 _LIBCALL LONG
5 ReadLineAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buffer, _REG( d0 ) LONG bufSize )
7 LONG len;
9 /* First read any data up to the LF or the buffer is full */
10 if( FGetsLenAsync( file, buffer, bufSize, &len ) )
12 UBYTE *end;
14 end = ( ( UBYTE * ) buffer ) + len - 1;
16 if( *end != '\n' )
18 UBYTE ch = 0;
20 /* We didn't reach EOF yet */
21 while( TRUE )
23 UBYTE *ptr;
24 LONG i, count;
26 ptr = ( UBYTE * ) file->af_Offset;
28 if( count = file->af_BytesLeft )
30 /* Scan for LF char in buffer */
31 for( i = 0; ( i < count ) && ( *ptr != '\n' ); ++i, ++ptr )
35 /* If i < count, then the loop above aborted
36 * due to LF char.
38 if( i < count )
40 ch = '\n';
41 ++i;
44 file->af_BytesLeft -= i;
45 file->af_Offset += i;
47 if( i < count )
49 /* All done */
50 break;
54 if( ReadAsync( file, &ch, 1 ) < 1 )
56 break;
59 if( ch == '\n' )
61 /* All done */
62 break;
66 if( ch == '\n' )
68 /* Overwrite last char with LF */
69 *end++ = '\n';
70 *end = '\0';
75 return( len );