diff config.c @ 875:54ffba3768d6

Make menu stuff work on Android (theoretically)
author Michael Pavone <pavone@retrodev.com>
date Sun, 08 Nov 2015 22:03:34 -0800
parents 69a6ec208111
children 540cc4a7d626
line wrap: on
line diff
--- a/config.c	Sun Nov 08 18:38:33 2015 -0800
+++ b/config.c	Sun Nov 08 22:03:34 2015 -0800
@@ -82,13 +82,13 @@
 	return head;
 }
 
-tern_node * parse_config(char * config_data)
+tern_node *parse_config(char * config_data)
 {
 	int line = 1;
 	return parse_config_int(&config_data, 0, &line);
 }
 
-tern_node * parse_config_file(char * config_path)
+tern_node *parse_config_file(char *config_path)
 {
 	tern_node * ret = NULL;
 	FILE * config_file = fopen(config_path, "rb");
@@ -114,85 +114,44 @@
 	return ret;
 }
 
-#ifdef __ANDROID__
-#include <SDL.h>
-
-tern_node * parse_config_file_assets(char *config_path)
+tern_node *parse_bundled_config(char *config_name)
 {
-	tern_node * ret = NULL;
-	SDL_RWops *rw = SDL_RWFromFile(config_path, "rb");
-	if (!rw) {
-		goto open_fail;
-	}
-	size_t config_size = rw->size(rw);
-	if (!config_size) {
-		goto config_empty;
+	long confsize;
+	char *confdata = read_bundled_file(config_name, &confsize);
+	tern_node *ret = NULL;
+	if (confdata) {
+		confdata[confsize] = 0;
+		ret = parse_config(confdata);
+		free(confdata);
 	}
-
-	char * config_data = malloc(config_size+1);
-	if (SDL_RWread(rw, config_data, 1, config_size) != config_size) {
-		goto config_read_fail;
-	}
-	config_data[config_size] = '\0';
-
-	ret = parse_config(config_data);
-config_read_fail:
-	free(config_data);
-config_empty:
-	SDL_RWclose(rw);
-open_fail:
 	return ret;
 }
 
-tern_node * load_config()
+tern_node *load_config()
 {
-	char *path = alloc_concat(SDL_AndroidGetInternalStoragePath(), "/blastem.cfg");
-	tern_node * ret = parse_config_file(path);
-	free(path);
-	if (ret) {
-		return ret;
-	}
-
-	ret = parse_config_file_assets("default.cfg");
-	if (ret) {
-		return ret;
-	}
-
-	fatal_error("Failed to find a config file in internal storage or in the blastem APK\n");
-	//this will never get reached, but the compiler doesn't know that. Let's make it happy
-	return NULL;
-}
-
-#else
-
-tern_node * load_config()
-{
-	char * exe_dir;
-	char * home = get_home_dir();
+	char *confdir = get_config_dir();
+	char *confpath = NULL;
 	tern_node *ret;
-	if (home) {
-		char * path = alloc_concat(home, "/.config/blastem/blastem.cfg");
-		ret = parse_config_file(path);
-		free(path);
+	if (confdir) {
+		confpath = alloc_concat(confdir, "/blastem.cfg");
+		ret = parse_config_file(confpath);
 		if (ret) {
+			free(confpath);
 			return ret;
 		}
 	}
 
-	exe_dir = get_exe_dir();
-	if (exe_dir) {
-		char *path = alloc_concat(exe_dir, "/default.cfg");
-		ret = parse_config_file(path);
-		free(path);
-		if (ret) {
-			return ret;
-		}
+	ret = parse_bundled_config("default.cfg");
+	if (ret) {
+		free(confpath);
+		return ret;
 	}
 
-	fatal_error("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n");
+	if (confpath) {
+		fatal_error("Failed to find a config file at %s or in the blastem executable directory\n", confpath);
+	} else {
+		fatal_error("Failed to find a config file in the BlastEm executable directory and the config directory path could not be determined\n");
+	}
 	//this will never get reached, but the compiler doesn't know that. Let's make it happy
 	return NULL;
 }
-
-#endif
-