Add newline translationwq
authorOwen Shepherd <owen.shepherd@e43.eu>
Thu, 9 Oct 2014 19:51:02 +0000 (20:51 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Thu, 9 Oct 2014 19:51:02 +0000 (20:51 +0100)
functions/stdio/_PDCLIB_flushbuffer.c
internals/_PDCLIB_io.h
platform/gandr/includes/_PDCLIB_config.h

index 7ee0d93c35730aa08b1baaa28128d2e735f23f16..44066d87dbd24099d40fe16ca657f25ef786c858 100644 (file)
 #include <_PDCLIB_glue.h>\r
 #include <_PDCLIB_io.h>\r
 \r
-int _PDCLIB_flushbuffer( FILE * stream )\r
+static int flushsubbuffer( FILE * stream, size_t length )\r
 {\r
-    if ( ! ( stream->status & _PDCLIB_FBIN ) )\r
-    {\r
-        /* TODO: Text stream conversion here */\r
-    }\r
-\r
     size_t written = 0;\r
+    int rv = 0;\r
 \r
-\r
-    while(written != stream->bufidx) {\r
+    while( written != length )\r
+    {\r
         size_t justWrote;\r
-        size_t toWrite = stream->bufidx - written;\r
-        bool res = stream->ops->write( stream->handle, stream->buffer + written, \r
+        size_t toWrite = length - written;\r
+        bool res = stream->ops->write( stream->handle, stream->buffer + written,\r
                               toWrite, &justWrote);\r
         written += justWrote;\r
         stream->pos.offset += justWrote;\r
 \r
-        if(!res) {\r
+        if (!res)\r
+        {\r
             stream->status |=_PDCLIB_ERRORFLAG;\r
-            stream->bufidx -= written;\r
-            memmove( stream->buffer, stream->buffer + written, stream->bufidx );\r
-            return EOF;\r
+            rv = EOF;\r
+            break;\r
+        }\r
+    }\r
+\r
+    stream->bufidx -= written;\r
+    memmove( stream->buffer, stream->buffer + written, stream->bufidx );\r
+\r
+    return rv;\r
+}\r
+\r
+#if defined(_PDCLIB_NEED_EOL_TRANSLATION)\r
+#undef  _PDCLIB_NEED_EOL_TRANSLATION\r
+#define _PDCLIB_NEED_EOL_TRANSLATION 1\r
+#else\r
+#define _PDCLIB_NEED_EOL_TRANSLATION 0\r
+#endif\r
+\r
+int _PDCLIB_flushbuffer( FILE * stream )\r
+{\r
+    // if a text stream, and this platform needs EOL translation, well...\r
+    if ( ! ( stream->status & _PDCLIB_FBIN ) && _PDCLIB_NEED_EOL_TRANSLATION )\r
+    {\r
+        size_t pos;\r
+        for ( pos = 0; pos < stream->bufidx; pos++ )\r
+        {\r
+            if (stream->buffer[pos] == '\n' ) {\r
+                if ( stream->bufidx == stream->bufend ) {\r
+                    // buffer is full. Need to print out everything up till now\r
+                    if( flushsubbuffer( stream, pos ) )\r
+                    {\r
+                        return EOF;\r
+                    }\r
+\r
+                    pos = 0;\r
+                }\r
+\r
+                // we have spare space in buffer. Shift everything 1char and\r
+                // insert \r\r
+                memmove( &stream->buffer[pos+1], &stream->buffer[pos], stream->bufidx - pos );\r
+                stream->buffer[pos] = '\r';\r
+\r
+                pos += 2;\r
+                stream->bufidx++;\r
+            }\r
         }\r
     }\r
 \r
-    stream->bufidx = 0;\r
-    return 0;\r
+    return flushsubbuffer( stream, stream->bufidx );\r
 }\r
 \r
 #endif\r
index 78e747c0edcc1df81a8184ca7a74372f4ef90a5a..2df3ff5ca6dd1b31927a32abd8097938acef24d6 100644 (file)
@@ -14,7 +14,7 @@
 */\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 +41,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
@@ -206,21 +206,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
index c899cfd6c4580cb3344624072c6629d445663d11..98727c820cb5b0c6950d808679387dcffbefff7e 100644 (file)
@@ -184,6 +184,9 @@ typedef __builtin_va_list _PDCLIB_va_list;
 /* The default size for file buffers. Must be at least 256. */
 #define _PDCLIB_BUFSIZ 1024
 
+/* We need \n -> \r\n translation for text files */
+#define _PDCLIB_NEED_EOL_TRANSLATION 1
+
 /* Minimum number of files we can open simultaneously. C says this must be >= 8,
  * so we say that. May actually be a lie in some cases...
  */