comparison 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
comparison
equal deleted inserted replaced
874:b6842dfb8edf 875:54ffba3768d6
80 } 80 }
81 } 81 }
82 return head; 82 return head;
83 } 83 }
84 84
85 tern_node * parse_config(char * config_data) 85 tern_node *parse_config(char * config_data)
86 { 86 {
87 int line = 1; 87 int line = 1;
88 return parse_config_int(&config_data, 0, &line); 88 return parse_config_int(&config_data, 0, &line);
89 } 89 }
90 90
91 tern_node * parse_config_file(char * config_path) 91 tern_node *parse_config_file(char *config_path)
92 { 92 {
93 tern_node * ret = NULL; 93 tern_node * ret = NULL;
94 FILE * config_file = fopen(config_path, "rb"); 94 FILE * config_file = fopen(config_path, "rb");
95 if (!config_file) { 95 if (!config_file) {
96 goto open_fail; 96 goto open_fail;
112 fclose(config_file); 112 fclose(config_file);
113 open_fail: 113 open_fail:
114 return ret; 114 return ret;
115 } 115 }
116 116
117 #ifdef __ANDROID__ 117 tern_node *parse_bundled_config(char *config_name)
118 #include <SDL.h>
119
120 tern_node * parse_config_file_assets(char *config_path)
121 { 118 {
122 tern_node * ret = NULL; 119 long confsize;
123 SDL_RWops *rw = SDL_RWFromFile(config_path, "rb"); 120 char *confdata = read_bundled_file(config_name, &confsize);
124 if (!rw) { 121 tern_node *ret = NULL;
125 goto open_fail; 122 if (confdata) {
123 confdata[confsize] = 0;
124 ret = parse_config(confdata);
125 free(confdata);
126 } 126 }
127 size_t config_size = rw->size(rw);
128 if (!config_size) {
129 goto config_empty;
130 }
131
132 char * config_data = malloc(config_size+1);
133 if (SDL_RWread(rw, config_data, 1, config_size) != config_size) {
134 goto config_read_fail;
135 }
136 config_data[config_size] = '\0';
137
138 ret = parse_config(config_data);
139 config_read_fail:
140 free(config_data);
141 config_empty:
142 SDL_RWclose(rw);
143 open_fail:
144 return ret; 127 return ret;
145 } 128 }
146 129
147 tern_node * load_config() 130 tern_node *load_config()
148 { 131 {
149 char *path = alloc_concat(SDL_AndroidGetInternalStoragePath(), "/blastem.cfg"); 132 char *confdir = get_config_dir();
150 tern_node * ret = parse_config_file(path); 133 char *confpath = NULL;
151 free(path);
152 if (ret) {
153 return ret;
154 }
155
156 ret = parse_config_file_assets("default.cfg");
157 if (ret) {
158 return ret;
159 }
160
161 fatal_error("Failed to find a config file in internal storage or in the blastem APK\n");
162 //this will never get reached, but the compiler doesn't know that. Let's make it happy
163 return NULL;
164 }
165
166 #else
167
168 tern_node * load_config()
169 {
170 char * exe_dir;
171 char * home = get_home_dir();
172 tern_node *ret; 134 tern_node *ret;
173 if (home) { 135 if (confdir) {
174 char * path = alloc_concat(home, "/.config/blastem/blastem.cfg"); 136 confpath = alloc_concat(confdir, "/blastem.cfg");
175 ret = parse_config_file(path); 137 ret = parse_config_file(confpath);
176 free(path);
177 if (ret) { 138 if (ret) {
139 free(confpath);
178 return ret; 140 return ret;
179 } 141 }
180 } 142 }
181 143
182 exe_dir = get_exe_dir(); 144 ret = parse_bundled_config("default.cfg");
183 if (exe_dir) { 145 if (ret) {
184 char *path = alloc_concat(exe_dir, "/default.cfg"); 146 free(confpath);
185 ret = parse_config_file(path); 147 return ret;
186 free(path);
187 if (ret) {
188 return ret;
189 }
190 } 148 }
191 149
192 fatal_error("Failed to find a config file in ~/.config/blastem/blastem.cfg or in the blastem executable directory\n"); 150 if (confpath) {
151 fatal_error("Failed to find a config file at %s or in the blastem executable directory\n", confpath);
152 } else {
153 fatal_error("Failed to find a config file in the BlastEm executable directory and the config directory path could not be determined\n");
154 }
193 //this will never get reached, but the compiler doesn't know that. Let's make it happy 155 //this will never get reached, but the compiler doesn't know that. Let's make it happy
194 return NULL; 156 return NULL;
195 } 157 }
196
197 #endif
198