comparison paths.c @ 1541:f8ef74e7c800

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