Group key config symbols together
[neverball] / share / fs.c
index 4a85852..fd75cbf 100644 (file)
@@ -26,7 +26,13 @@ struct fs_file
 
 int fs_init(const char *argv0)
 {
-    return PHYSFS_init(argv0);
+    if (PHYSFS_init(argv0))
+    {
+        PHYSFS_permitSymbolicLinks(1);
+        return 1;
+    }
+
+    return 0;
 }
 
 int fs_quit(void)
@@ -193,6 +199,11 @@ int fs_eof(fs_file fh)
     return PHYSFS_eof(fh->handle);
 }
 
+int fs_length(fs_file fh)
+{
+    return PHYSFS_fileLength(fh->handle);
+}
+
 /* -------------------------------------------------------------------------- */
 
 /*
@@ -290,8 +301,10 @@ int fs_puts(const char *src, fs_file fh)
 char *fs_gets(char *dst, int count, fs_file fh)
 {
     char *s = dst;
-    char *cr = NULL;
-    int c = 0;
+    int c;
+
+    assert(dst);
+    assert(count > 0);
 
     if (fs_eof(fh))
         return NULL;
@@ -302,30 +315,28 @@ char *fs_gets(char *dst, int count, fs_file fh)
 
         *s = c;
 
-        /* Normalize possible CRLF and break. */
+        /* Keep a newline and break. */
 
         if (*s == '\n')
         {
-            if (cr + 1 == s)
-                *cr = '\n';
-            else
-                s++;
-
+            s++;
             break;
         }
 
-        /* Note carriage return. */
+        /* Ignore carriage returns. */
 
         if (*s == '\r')
-            cr = s;
+        {
+            count++;
+            s--;
+        }
 
         s++;
     }
 
-    if (count > 0)
-        *s = '\0';
+    *s = '\0';
 
-    return c < 0 ? NULL : dst;
+    return dst;
 }
 
 /* -------------------------------------------------------------------------- */
@@ -423,3 +434,33 @@ int fs_printf(fs_file fh, const char *fmt, ...)
 }
 
 /* -------------------------------------------------------------------------- */
+
+void *fs_load(const char *path, int *datalen)
+{
+    fs_file fh;
+    void *data;
+
+    data = NULL;
+
+    if ((fh = fs_open(path, "r")))
+    {
+        if ((*datalen = fs_length(fh)) > 0)
+        {
+            if ((data = malloc(*datalen)))
+            {
+                if (fs_read(data, *datalen, 1, fh) != 1)
+                {
+                    free(data);
+                    data = NULL;
+                    *datalen = 0;
+                }
+            }
+        }
+
+        fs_close(fh);
+    }
+
+    return data;
+}
+
+/* -------------------------------------------------------------------------- */