]> pd.if.org Git - pdclib/blobdiff - internals/_PDCLIB_io.h
Sweeping cleanups. Sorry for the massive commit; I got sidetracked once too often.
[pdclib] / internals / _PDCLIB_io.h
index 78e747c0edcc1df81a8184ca7a74372f4ef90a5a..27c33e7dd7a50d360ee58710bacfee8213ed524d 100644 (file)
@@ -1,20 +1,21 @@
-#ifndef __PDCLIB_IO_H\r
-#define __PDCLIB_IO_H __PDCLIB_IO_H\r
-#include "_PDCLIB_int.h"\r
-#include "_PDCLIB_threadconfig.h"\r
-\r
-/* PDCLib internal I/O logic <_PDCLIB_io.h>\r
+/* PDCLib I/O support <_PDCLIB_io.h>\r
 \r
    This file is part of the Public Domain C Library (PDCLib).\r
    Permission is granted to use, modify, and / or redistribute at will.\r
 */\r
 \r
+#ifndef __PDCLIB_IO_H\r
+#define __PDCLIB_IO_H __PDCLIB_IO_H\r
+\r
+#include "_PDCLIB_int.h"\r
+#include "_PDCLIB_threadconfig.h"\r
+\r
 /* Flags for representing mode (see fopen()). Note these must fit the same\r
    status field as the _IO?BF flags in <stdio.h> and the internal flags below.\r
 */\r
 #define _PDCLIB_FREAD     8u\r
 #define _PDCLIB_FWRITE   16u\r
-#define _PDCLIB_FAPPEND  32u \r
+#define _PDCLIB_FAPPEND  32u\r
 #define _PDCLIB_FRW      64u\r
 #define _PDCLIB_FBIN    128u\r
 \r
@@ -41,7 +42,7 @@ union _PDCLIB_fd
 #endif\r
     void *              pointer;\r
     _PDCLIB_uintptr_t   uval;\r
-    _PDCLIB_intptr_t    sval;     \r
+    _PDCLIB_intptr_t    sval;\r
 };\r
 \r
 /******************************************************************************/\r
@@ -179,6 +180,9 @@ struct _PDCLIB_file
     _PDCLIB_size_t            bufsize;  /* Size of buffer */\r
     _PDCLIB_size_t            bufidx;   /* Index of current position in buffer */\r
     _PDCLIB_size_t            bufend;   /* Index of last pre-read character in buffer */\r
+#ifdef _PDCLIB_NEED_EOL_TRANSLATION\r
+    _PDCLIB_size_t            bufnlexp; /* Current position of buffer newline expansion */\r
+#endif\r
     _PDCLIB_size_t            ungetidx; /* Number of ungetc()'ed characters */\r
     unsigned char *           ungetbuf; /* ungetc() buffer */\r
     unsigned int              status;   /* Status flags; see above */\r
@@ -206,21 +210,43 @@ static inline _PDCLIB_size_t _PDCLIB_getchars( char * out, _PDCLIB_size_t n,
     {\r
         while ( stream->bufidx != stream->bufend && i != n)\r
         {\r
-            c = (unsigned char)\r
-                ( out[ i++ ] = stream->buffer[ stream->bufidx++ ] );\r
+            c = (unsigned char) stream->buffer[ stream->bufidx++ ];\r
+#ifdef _PDCLIB_NEED_EOL_TRANSLATION\r
+            if ( !( stream->status & _PDCLIB_FBIN ) && c == '\r' )\r
+            {\r
+                if ( stream->bufidx == stream->bufend )\r
+                    break;\r
+\r
+                if ( stream->buffer[ stream->bufidx ] == '\n' )\r
+                {\r
+                    c = '\n';\r
+                    stream->bufidx++;\r
+                }\r
+            }\r
+#endif\r
+            out[ i++ ] = c;\r
+\r
             if( c == stopchar )\r
                 return i;\r
         }\r
 \r
-        if ( stream->bufidx == stream->bufend )\r
+        if ( i != n )\r
         {\r
             if( _PDCLIB_fillbuffer( stream ) == -1 )\r
             {\r
-                return i;\r
+                break;\r
             }\r
         }\r
     }\r
 \r
+#ifdef _PDCLIB_NEED_EOL_TRANSLATION\r
+    if ( i != n && stream->bufidx != stream->bufend )\r
+    {\r
+        // we must have EOF'd immediately after a \r\r
+        out[ i++ ] = stream->buffer[ stream->bufidx++ ];\r
+    }\r
+#endif\r
+\r
     return i;\r
 }\r
 \r