share/gui: don't use less of widget width for truncation than available
[neverball] / share / base_config.c
index d7f751d..221d544 100644 (file)
  * General Public License for more details.
  */
 
-#include <SDL.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
-#include <errno.h>
 #include <math.h>
 
 #include "base_config.h"
 #include "glext.h"
 #include "vec3.h"
+#include "common.h"
+#include "fs.h"
+#include "dir.h"
+#include "array.h"
 
 /*---------------------------------------------------------------------------*/
 
-/* Define the mkdir symbol. */
-
-#ifdef _WIN32
-#include <direct.h>
-#else
-#include <sys/stat.h>
-#endif
-
-/*---------------------------------------------------------------------------*/
-
-static char data_path[MAXSTR];
-static char user_path[MAXSTR];
-
-/*
- * Given  a path  and a  file name  relative to  that path,  create an
- * absolute path name and return a temporary pointer to it.
- */
-static const char *config_file(const char *path, const char *file)
+static const char *pick_data_path(const char *arg_data_path)
 {
-    static char absolute[MAXSTR];
+    static char dir[MAXSTR];
+    char *env;
 
-    size_t d = strlen(path);
+    if (arg_data_path)
+        return arg_data_path;
 
-    strncpy(absolute, path, MAXSTR - 1);
-    strncat(absolute, "/",  MAXSTR - d - 1);
-    strncat(absolute, file, MAXSTR - d - 2);
+    if ((env = getenv("NEVERBALL_DATA")))
+        return env;
 
-    return absolute;
-}
+    if (path_is_abs(CONFIG_DATA))
+        return CONFIG_DATA;
 
-static int config_test(const char *path, const char *file)
-{
-    if (file)
-    {
-        FILE *fp;
-
-        if ((fp = fopen(config_file(path, file), "r")))
-        {
-            fclose(fp);
-            return 1;
-        }
-        return 0;
-    }
-    return 1;
-}
+    strncpy(dir, fs_base_dir(), sizeof (dir) - 1);
+    strncat(dir, "/",           sizeof (dir) - strlen(dir) - 1);
+    strncat(dir, CONFIG_DATA,   sizeof (dir) - strlen(dir) - 1);
 
-const char *config_data(const char *file)
-{
-    return config_file(data_path, file);
+    return dir;
 }
 
-const char *config_user(const char *file)
+static const char *pick_home_path(void)
 {
-    return config_file(user_path, file);
-}
+    const char *path;
 
-/*---------------------------------------------------------------------------*/
+#ifdef _WIN32
+    return (path = getenv("APPDATA")) ? path : fs_base_dir();
+#else
+    return (path = getenv("HOME")) ? path : fs_base_dir();
+#endif
+}
 
-/*
- * Attempt to find  the game data directory.  Search  the command line
- * parameter,  the environment,  and the  hard-coded default,  in that
- * order.  Confirm it by checking for presence of the named file.
- */
-int config_data_path(const char *path, const char *file)
+void config_paths(const char *arg_data_path)
 {
-    char *dir;
+    const char *data, *home, *user;
 
-    if (path && config_test(path, file))
-    {
-        strncpy(data_path, path, MAXSTR);
-        return 1;
-    }
+    /*
+     * Scan in turn the game data and user directories for archives,
+     * adding each archive to the search path.  Archives with names
+     * further down the alphabet take precedence.  After each scan,
+     * add the directory itself, taking precedence over archives added
+     * so far.
+     */
 
-    if ((dir = getenv("NEVERBALL_DATA")) && config_test(dir, file))
-    {
-        strncpy(data_path, dir, MAXSTR);
-        return 1;
-    }
+    /* Data directory. */
 
-    if (CONFIG_DATA && config_test(CONFIG_DATA, file))
-    {
-        strncpy(data_path, CONFIG_DATA, MAXSTR);
-        return 1;
-    }
+    data = pick_data_path(arg_data_path);
 
-    return 0;
-}
+    fs_add_path_with_archives(data);
 
-/*
- * Determine the location of  the user's home directory.  Ensure there
- * is a  directory there for  storing configuration, high  scores, and
- * replays.
- *
- * HACK: under Windows just assume the user has permission to write to
- * the data  directory.  This is  more reliable than trying  to divine
- * anything reasonable from the environment.
- */
-int config_user_path(const char *file)
-{
-#ifdef _WIN32
-    size_t d = strlen(data_path);
+    /* User directory. */
 
-    strncpy(user_path, data_path,   MAXSTR - 1);
-    strncat(user_path, "\\",        MAXSTR - d - 1);
-    strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
+    home = pick_home_path();
+    user = concat_string(home, "/", CONFIG_USER, NULL);
 
-    if ((mkdir(user_path) == 0) || (errno = EEXIST))
-        if (config_test(user_path, file))
-            return 1;
-#else
-    char *dir;
+    /* Set up directory for writing, create if needed. */
 
-    if ((dir = getenv("HOME")))
+    if (!fs_set_write_dir(user))
     {
-        size_t d = strlen(dir);
-
-        strncpy(user_path, dir,         MAXSTR - 1);
-        strncat(user_path, "/",         MAXSTR - d - 1);
-        strncat(user_path, CONFIG_USER, MAXSTR - d - 2);
+        if (fs_set_write_dir(home) && fs_mkdir(CONFIG_USER))
+            fs_set_write_dir(user);
     }
 
-    if ((mkdir(user_path, 0777) == 0) || (errno = EEXIST))
-        if (config_test(user_path, file))
-            return 1;
-#endif
+    fs_add_path_with_archives(user);
 
-    return 0;
+    free((void *) user);
 }
 
 /*---------------------------------------------------------------------------*/