]> pd.if.org Git - pdclib/commitdiff
PDCLIB-18: Add _PDCLIB_getchars to _PDCLIB_io.h. Change fread & fgets to go through...
authorOwen Shepherd <owen.shepherd@e43.eu>
Tue, 11 Dec 2012 01:42:35 +0000 (01:42 +0000)
committerOwen Shepherd <owen.shepherd@e43.eu>
Tue, 11 Dec 2012 01:42:35 +0000 (01:42 +0000)
All narrow character reads are going to be directed through this function

functions/stdio/fgets.c
functions/stdio/fread.c
functions/stdio/ungetc.c
internals/_PDCLIB_io.h

index 04510583ad29e82b46671106b81caac76399594e..6fbc2ef7961509b7e0c93aeae610d001f1a84f90 100644 (file)
@@ -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;
 }
index 1fdc75326c887ac6bcfc76717e0f20ce38114954..f1ab3a9adeb84745336a2043e8d0d4c8e95a8882 100644 (file)
@@ -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;
 }
index 2e556ab4f9c16f5985c3bf7929f6c885e9a140c8..4e9388b40d7f3e83094a00331630bf8c2d832f6f 100644 (file)
@@ -31,6 +31,7 @@ int ungetc( int c, struct _PDCLIB_file_t * stream )
 
 #ifdef TEST
 #include <_PDCLIB_test.h>
+#include <stdlib.h>
 
 const char* hellostr = "Hello, world!";
 
index 829bb4586ed66781c2b2aca73f88f934651678a5..2ece56ce194b4ea3b0387f099770d5ef0a001142 100644 (file)
@@ -151,5 +151,40 @@ struct _PDCLIB_file_t
     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