Reduce mapc dependences (need to split image.[ch])
[neverball] / share / binary.c
index 683f59e..2045f06 100644 (file)
@@ -14,6 +14,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <SDL.h>
 #include <SDL_byteorder.h>
@@ -62,6 +63,13 @@ void put_array(FILE *fout, const float *v, size_t n)
         put_float(fout, v + i);
 }
 
+void put_string(FILE *fp, char * str)
+{
+    int len = strlen(str) + 1;
+    fwrite(str, 1, len,  fp);
+}
+
+
 /*---------------------------------------------------------------------------*/
 
 void get_float(FILE *fin, float *f)
@@ -106,4 +114,23 @@ 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 include the room for the \0 */
+    /* if len is too small, the string is truncated. */
+{
+    char b;
+    while (1)
+    {
+       fread(&b, 1, 1,  fp);
+       if (len > 0)
+       {
+           *(str++) = (len > 1 ? b : '\0');
+           len--;
+       }
+       if (b == '\0')
+           return;
+    }
+}
+
+
 /*---------------------------------------------------------------------------*/