Fix accidental switch/teleporter behavior changes
[neverball] / share / binary.c
index c66672e..4093d3b 100644 (file)
 #include <stdlib.h>
 #include <string.h>
 
-#include <SDL.h>
-#include <SDL_byteorder.h>
+#include <SDL_endian.h>
+
+#include "fs.h"
 
 /*---------------------------------------------------------------------------*/
 
-void put_float(FILE *fout, const float *f)
+void put_float(fs_file fout, const float *f)
 {
-    unsigned char *p = (unsigned char *) f;
+    const unsigned char *p = (const unsigned char *) f;
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    fputc((int) p[3], fout);
-    fputc((int) p[2], fout);
-    fputc((int) p[1], fout);
-    fputc((int) p[0], fout);
+    fs_putc((int) p[3], fout);
+    fs_putc((int) p[2], fout);
+    fs_putc((int) p[1], fout);
+    fs_putc((int) p[0], fout);
 #else
-    fputc((int) p[0], fout);
-    fputc((int) p[1], fout);
-    fputc((int) p[2], fout);
-    fputc((int) p[3], fout);
+    fs_putc((int) p[0], fout);
+    fs_putc((int) p[1], fout);
+    fs_putc((int) p[2], fout);
+    fs_putc((int) p[3], fout);
 #endif
 }
 
-void put_index(FILE *fout, const int *i)
+void put_index(fs_file fout, const int *i)
 {
-    unsigned char *p = (unsigned char *) i;
+    const unsigned char *p = (const unsigned char *) i;
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    fputc((int) p[3], fout);
-    fputc((int) p[2], fout);
-    fputc((int) p[1], fout);
-    fputc((int) p[0], fout);
+    fs_putc((int) p[3], fout);
+    fs_putc((int) p[2], fout);
+    fs_putc((int) p[1], fout);
+    fs_putc((int) p[0], fout);
 #else
-    fputc((int) p[0], fout);
-    fputc((int) p[1], fout);
-    fputc((int) p[2], fout);
-    fputc((int) p[3], fout);
+    fs_putc((int) p[0], fout);
+    fs_putc((int) p[1], fout);
+    fs_putc((int) p[2], fout);
+    fs_putc((int) p[3], fout);
 #endif
 }
 
-void put_array(FILE *fout, const float *v, size_t n)
+void put_short(fs_file fout, const short *s)
 {
-    size_t i;
+    const unsigned char *p = (const unsigned char *) s;
 
-    for (i = 0; i < n; i++)
-        put_float(fout, v + i);
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+    fs_putc((int) p[1], fout);
+    fs_putc((int) p[0], fout);
+#else
+    fs_putc((int) p[0], fout);
+    fs_putc((int) p[1], fout);
+#endif
 }
 
-void put_string(FILE *fp, char *str)
+void put_array(fs_file fout, const float *v, size_t n)
 {
-    int len = strlen(str) + 1;
-    fwrite(str, 1, len,  fp);
-}
+    size_t i;
 
+    for (i = 0; i < n; i++)
+        put_float(fout, v + i);
+}
 
 /*---------------------------------------------------------------------------*/
 
-void get_float(FILE *fin, float *f)
+void get_float(fs_file fin, float *f)
 {
     unsigned char *p = (unsigned char *) f;
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    p[3] = (unsigned char) fgetc(fin);
-    p[2] = (unsigned char) fgetc(fin);
-    p[1] = (unsigned char) fgetc(fin);
-    p[0] = (unsigned char) fgetc(fin);
+    p[3] = (unsigned char) fs_getc(fin);
+    p[2] = (unsigned char) fs_getc(fin);
+    p[1] = (unsigned char) fs_getc(fin);
+    p[0] = (unsigned char) fs_getc(fin);
 #else
-    p[0] = (unsigned char) fgetc(fin);
-    p[1] = (unsigned char) fgetc(fin);
-    p[2] = (unsigned char) fgetc(fin);
-    p[3] = (unsigned char) fgetc(fin);
+    p[0] = (unsigned char) fs_getc(fin);
+    p[1] = (unsigned char) fs_getc(fin);
+    p[2] = (unsigned char) fs_getc(fin);
+    p[3] = (unsigned char) fs_getc(fin);
 #endif
 }
 
-void get_index(FILE *fin, int *i)
+void get_index(fs_file fin, int *i)
 {
     unsigned char *p = (unsigned char *) i;
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    p[3] = (unsigned char) fgetc(fin);
-    p[2] = (unsigned char) fgetc(fin);
-    p[1] = (unsigned char) fgetc(fin);
-    p[0] = (unsigned char) fgetc(fin);
+    p[3] = (unsigned char) fs_getc(fin);
+    p[2] = (unsigned char) fs_getc(fin);
+    p[1] = (unsigned char) fs_getc(fin);
+    p[0] = (unsigned char) fs_getc(fin);
+#else
+    p[0] = (unsigned char) fs_getc(fin);
+    p[1] = (unsigned char) fs_getc(fin);
+    p[2] = (unsigned char) fs_getc(fin);
+    p[3] = (unsigned char) fs_getc(fin);
+#endif
+}
+
+void get_short(fs_file fin, short *s)
+{
+    unsigned char *p = (unsigned char *) s;
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+    p[1] = (unsigned char) fs_getc(fin);
+    p[0] = (unsigned char) fs_getc(fin);
 #else
-    p[0] = (unsigned char) fgetc(fin);
-    p[1] = (unsigned char) fgetc(fin);
-    p[2] = (unsigned char) fgetc(fin);
-    p[3] = (unsigned char) fgetc(fin);
+    p[0] = (unsigned char) fs_getc(fin);
+    p[1] = (unsigned char) fs_getc(fin);
 #endif
 }
 
-void get_array(FILE *fin, float *v, size_t n)
+void get_array(fs_file fin, float *v, size_t n)
 {
     size_t i;
 
@@ -114,23 +134,34 @@ void get_array(FILE *fin, float *v, size_t n)
         get_float(fin, v + i);
 }
 
-void get_string(FILE *fp, char *str, int len)
-/* len includes room for '\0'.  If len is too small, the string is truncated. */
+/*---------------------------------------------------------------------------*/
+
+void put_string(fs_file fout, const char *s)
 {
-    char b;
+    fs_puts(s, fout);
+    fs_putc('\0', fout);
+}
+
+void get_string(fs_file fin, char *s, int max)
+{
+    int c;
 
-    while (1)
+    while ((c = fs_getc(fin)) >= 0)
     {
-        fread(&b, 1, 1, fp);
-        if (len > 0)
+        if (max > 0)
         {
-            *(str++) = (len > 1 ? b : '\0');
-            len--;
+            *s++ = c;
+            max--;
+
+            /* Terminate the string, but keep reading until NUL. */
+
+            if (max == 0)
+                *(s - 1) = 0;
         }
-        if (b == '\0')
-            return;
+
+        if (c == 0)
+            break;
     }
 }
 
-
 /*---------------------------------------------------------------------------*/