comparison paths.c @ 1692:5dacaef602a7 segacd

Merge from default
author Michael Pavone <pavone@retrodev.com>
date Sat, 05 Jan 2019 00:58:08 -0800
parents ab3b465c052c
children dda7479f3bbb
comparison
equal deleted inserted replaced
1504:95b3a1a8b26c 1692:5dacaef602a7
1 #include <string.h>
2 #include <stdlib.h>
3 #include "blastem.h"
4 #include "util.h"
5
6 static char **current_path;
7
8 static void persist_path(void)
9 {
10 char *pathfname = alloc_concat(get_userdata_dir(), PATH_SEP "blastem" PATH_SEP "sticky_path");
11 FILE *f = fopen(pathfname, "wb");
12 if (f) {
13 if (fwrite(*current_path, 1, strlen(*current_path), f) != strlen(*current_path)) {
14 warning("Failed to save menu path");
15 }
16 fclose(f);
17 } else {
18 warning("Failed to save menu path: Could not open %s for writing\n", pathfname);
19
20 }
21 free(pathfname);
22 }
23
24 #ifdef __ANDROID__
25 #include <SDL.h>
26 #include <jni.h>
27 static char *get_external_storage_path()
28 {
29 static char *ret;
30 if (ret) {
31 return ret;
32 }
33 JNIEnv *env = SDL_AndroidGetJNIEnv();
34 if ((*env)->PushLocalFrame(env, 8) < 0) {
35 return NULL;
36 }
37
38 jclass Environment = (*env)->FindClass(env, "android/os/Environment");
39 jmethodID getExternalStorageDirectory =
40 (*env)->GetStaticMethodID(env, Environment, "getExternalStorageDirectory", "()Ljava/io/File;");
41 jobject file = (*env)->CallStaticObjectMethod(env, Environment, getExternalStorageDirectory);
42 if (!file) {
43 goto cleanup;
44 }
45
46 jmethodID getAbsolutePath = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, file),
47 "getAbsolutePath", "()Ljava/lang/String;");
48 jstring path = (*env)->CallObjectMethod(env, file, getAbsolutePath);
49
50 char const *tmp = (*env)->GetStringUTFChars(env, path, NULL);
51 ret = strdup(tmp);
52 (*env)->ReleaseStringUTFChars(env, path, tmp);
53
54 cleanup:
55 (*env)->PopLocalFrame(env, NULL);
56 return ret;
57 }
58 #endif
59
60 void get_initial_browse_path(char **dst)
61 {
62 *dst = NULL;
63 char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval;
64 if (!remember_path || !strcmp("on", remember_path)) {
65 char *pathfname = alloc_concat(get_userdata_dir(), PATH_SEP "blastem" PATH_SEP "sticky_path");
66 FILE *f = fopen(pathfname, "rb");
67 if (f) {
68 long pathsize = file_size(f);
69 if (pathsize > 0) {
70 *dst = malloc(pathsize + 1);
71 if (fread(*dst, 1, pathsize, f) != pathsize) {
72 warning("Error restoring saved file browser path");
73 free(*dst);
74 *dst = NULL;
75 } else {
76 (*dst)[pathsize] = 0;
77 }
78 }
79 fclose(f);
80 }
81 free(pathfname);
82 if (!current_path) {
83 atexit(persist_path);
84 current_path = dst;
85 }
86 }
87 if (!*dst) {
88 *dst = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval;
89 }
90 if (!*dst){
91 #ifdef __ANDROID__
92 *dst = get_external_storage_path();
93 #else
94 *dst = "$HOME";
95 #endif
96 }
97 tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir());
98 vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir());
99 *dst = replace_vars(*dst, vars, 1);
100 tern_free(vars);
101 }
102
103 char *path_append(const char *base, const char *suffix)
104 {
105 if (!strcmp(suffix, "..")) {
106 #ifdef _WIN32
107 //handle transition from root of a drive to virtual root
108 if (base[1] == ':' && !base[2]) {
109 return strdup(PATH_SEP);
110 }
111 #endif
112 size_t len = strlen(base);
113 while (len > 0) {
114 --len;
115 if (is_path_sep(base[len])) {
116 if (!len) {
117 //special handling for /
118 len++;
119 }
120 char *ret = malloc(len+1);
121 memcpy(ret, base, len);
122 ret[len] = 0;
123 return ret;
124 }
125 }
126 return strdup(PATH_SEP);
127 } else {
128 #ifdef _WIN32
129 if (base[0] == PATH_SEP[0] && !base[1]) {
130 //handle transition from virtual root to root of a drive
131 return strdup(suffix);
132 }
133 #endif
134 if (is_path_sep(base[strlen(base) - 1])) {
135 return alloc_concat(base, suffix);
136 } else {
137 char const *pieces[] = {base, PATH_SEP, suffix};
138 return alloc_concat_m(3, pieces);
139 }
140 }
141 }