Start of port of AsyncIO library.
[AROS-Contrib.git] / workbench / libs / asyncio / src / FGetsLenAsync.c
blobad1b9dfe30c65fa1dbe81684a6c37747ed7978da
1 #include "async.h"
4 _LIBCALL APTR
5 FGetsLenAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buf,
6 _REG( d0 ) LONG numBytes, _REG( a2 ) LONG *len )
8 UBYTE *p;
9 LONG length = 0;
11 p = ( UBYTE * ) buf;
13 /* Make room for \n and \0 */
14 if( --numBytes <= 0 )
16 /* Handle senseless cases */
17 return( NULL );
20 while( TRUE )
22 UBYTE *ptr;
23 LONG i, count;
25 ptr = ( UBYTE * ) file->af_Offset;
27 if( count = file->af_BytesLeft )
29 count = MIN( count, numBytes );
31 for( i = 0; ( i < count ) && ( *ptr != '\n' ); ++i )
33 *p++ = *ptr++;
36 length += i;
38 /* Check for valid EOL char */
39 if( i < count )
41 /* MH: Since i < count, and count <= numBytes,
42 * there _is_ room for \n\0.
44 *p++ = '\n'; /* "Read" EOL char */
45 ++i;
46 length += 1;
49 file->af_BytesLeft -= i;
50 file->af_Offset += i;
52 if( ( i >= numBytes ) || ( *( p - 1 ) == '\n' ) )
54 /* MH: It is enough to break out of the loop.
55 * no need to "waste" code by making a special
56 * exit here. ;)
58 break;
61 numBytes -= i;
64 /* MH: numBytes must be at least 1 here, so there is still room
65 * for \n\0, in case we read \n.
68 if( ReadAsync( file, p, 1 ) < 1 )
70 break;
73 --numBytes;
74 ++length;
76 if( *p++ == '\n' )
78 break;
82 *p = '\0';
83 *len = length;
85 if( p == ( UBYTE * ) buf )
87 return( NULL );
90 return( buf );
94 _CALL APTR
95 FGetsAsync( _REG( a0 ) AsyncFile *file, _REG( a1 ) APTR buf, _REG( d0 ) LONG numBytes )
97 LONG len;
99 return( FGetsLenAsync( file, buf, numBytes, &len ) );