share/binary: change get_string to keep reading after exceeding buffer space
authorparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Sun, 5 Jul 2009 19:15:45 +0000 (19:15 +0000)
committerparasti <parasti@78b8d119-cf0a-0410-b17c-f493084dd1d7>
Sun, 5 Jul 2009 19:15:45 +0000 (19:15 +0000)
(This is how it should work and how Nuncabola does it, I just didn't
catch it when Elviz mentioned it.)

git-svn-id: https://s.snth.net/svn/neverball/trunk@2934 78b8d119-cf0a-0410-b17c-f493084dd1d7

share/binary.c

index 58ac1e7..4093d3b 100644 (file)
@@ -144,24 +144,24 @@ void put_string(fs_file fout, const char *s)
 
 void get_string(fs_file fin, char *s, int max)
 {
-    int c = -1;
+    int c;
 
-    if (max == 0)
-        return;
-
-    while (max && c && (c = fs_getc(fin)) >= 0)
+    while ((c = fs_getc(fin)) >= 0)
     {
-        *s++ = c;
-        max--;
-    }
+        if (max > 0)
+        {
+            *s++ = c;
+            max--;
 
-     /*
-      * Terminate the buffer ourselves, if we ran out of space without
-      * seeing a NUL character.
-      */
+            /* Terminate the string, but keep reading until NUL. */
 
-    if (max == 0 && *(s - 1) != 0)
-        *(s - 1) = 0;
+            if (max == 0)
+                *(s - 1) = 0;
+        }
+
+        if (c == 0)
+            break;
+    }
 }
 
 /*---------------------------------------------------------------------------*/