From: Owen Shepherd Date: Tue, 11 Dec 2012 01:42:35 +0000 (+0000) Subject: PDCLIB-18: Add _PDCLIB_getchars to _PDCLIB_io.h. Change fread & fgets to go through... X-Git-Url: https://pd.if.org/git/?p=pdclib;a=commitdiff_plain;h=e6d28b5afddd8b3e3564af2264aa8b705e711b67 PDCLIB-18: Add _PDCLIB_getchars to _PDCLIB_io.h. Change fread & fgets to go through this function. All narrow character reads are going to be directed through this function --- diff --git a/functions/stdio/fgets.c b/functions/stdio/fgets.c index 0451058..6fbc2ef 100644 --- a/functions/stdio/fgets.c +++ b/functions/stdio/fgets.c @@ -28,20 +28,9 @@ char * fgets_unlocked( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_ return NULL; } char * dest = s; - while ( ( ( *dest++ = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 ) - { - if ( stream->bufidx == stream->bufend ) - { - if ( _PDCLIB_fillbuffer( stream ) == EOF ) - { - /* In case of error / EOF before a character is read, this - will lead to a \0 be written anyway. Since the results - are "indeterminate" by definition, this does not hurt. - */ - break; - } - } - } + + dest += _PDCLIB_getchars( dest, size - 1, '\n', stream ); + *dest = '\0'; return ( dest == s ) ? NULL : s; } diff --git a/functions/stdio/fread.c b/functions/stdio/fread.c index 1fdc753..f1ab3a9 100644 --- a/functions/stdio/fread.c +++ b/functions/stdio/fread.c @@ -27,18 +27,10 @@ size_t fread_unlocked( void * _PDCLIB_restrict ptr, size_t nmemb_i; for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i ) { - for ( size_t size_i = 0; size_i < size; ++size_i ) - { - if ( stream->bufidx == stream->bufend ) - { - if ( _PDCLIB_fillbuffer( stream ) == EOF ) - { - /* Could not read requested data */ - return nmemb_i; - } - } - dest[ nmemb_i * size + size_i ] = stream->buffer[ stream->bufidx++ ]; - } + size_t numread = _PDCLIB_getchars( &dest[ nmemb_i * size ], size, EOF, + stream ); + if( numread != size ) + break; } return nmemb_i; } diff --git a/functions/stdio/ungetc.c b/functions/stdio/ungetc.c index 2e556ab..4e9388b 100644 --- a/functions/stdio/ungetc.c +++ b/functions/stdio/ungetc.c @@ -31,6 +31,7 @@ int ungetc( int c, struct _PDCLIB_file_t * stream ) #ifdef TEST #include <_PDCLIB_test.h> +#include const char* hellostr = "Hello, world!"; diff --git a/internals/_PDCLIB_io.h b/internals/_PDCLIB_io.h index 829bb45..2ece56c 100644 --- a/internals/_PDCLIB_io.h +++ b/internals/_PDCLIB_io.h @@ -151,5 +151,40 @@ struct _PDCLIB_file_t struct _PDCLIB_file_t * next; /* Pointer to next struct (internal) */ }; +static inline _PDCLIB_size_t _PDCLIB_getchars( char * out, _PDCLIB_size_t n, + int stopchar, + struct _PDCLIB_file_t * stream ) +{ + _PDCLIB_size_t i = 0; + int c; + while ( stream->ungetidx > 0 && i != n ) + { + c = (unsigned char) + ( out[ i++ ] = stream->ungetbuf[ --(stream->ungetidx) ] ); + if( c == stopchar ) + return i; + } + + while ( i != n ) + { + while ( stream->bufidx != stream->bufend && i != n) + { + c = (unsigned char) + ( out[ i++ ] = stream->buffer[ stream->bufidx++ ] ); + if( c == stopchar ) + return i; + } + + if ( stream->bufidx == stream->bufend ) + { + if( _PDCLIB_fillbuffer( stream ) == -1 ) + { + return i; + } + } + } + + return i; +} #endif