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;
}
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;
}
#ifdef TEST
#include <_PDCLIB_test.h>
+#include <stdlib.h>
const char* hellostr = "Hello, world!";
struct _PDCLIB_file_t * next; /* Pointer to next struct (internal) */\r
};\r
\r
+static inline _PDCLIB_size_t _PDCLIB_getchars( char * out, _PDCLIB_size_t n,\r
+ int stopchar,\r
+ struct _PDCLIB_file_t * stream )\r
+{\r
+ _PDCLIB_size_t i = 0;\r
+ int c;\r
+ while ( stream->ungetidx > 0 && i != n )\r
+ {\r
+ c = (unsigned char) \r
+ ( out[ i++ ] = stream->ungetbuf[ --(stream->ungetidx) ] );\r
+ if( c == stopchar )\r
+ return i;\r
+ }\r
+\r
+ while ( i != n )\r
+ {\r
+ while ( stream->bufidx != stream->bufend && i != n) \r
+ {\r
+ c = (unsigned char) \r
+ ( out[ i++ ] = stream->buffer[ stream->bufidx++ ] );\r
+ if( c == stopchar )\r
+ return i;\r
+ }\r
+\r
+ if ( stream->bufidx == stream->bufend )\r
+ {\r
+ if( _PDCLIB_fillbuffer( stream ) == -1 )\r
+ {\r
+ return i;\r
+ }\r
+ }\r
+ }\r
+\r
+ return i;\r
+}\r
\r
#endif\r