Mercurial > repos > blastem
annotate paths.c @ 2648:e16f567be36c
Fix lsl/lsr/asl/asr with memory operand
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 01 Mar 2025 17:23:43 -0800 |
parents | 2972a8e16ed2 |
children | c4256ce2c45a |
rev | line source |
---|---|
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include <string.h> |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include <stdlib.h> |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include "blastem.h" |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #include "util.h" |
2477
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
5 #include "config.h" |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
6 #include "paths.h" |
2262
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
7 #ifdef _WIN32 |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
8 #include <windows.h> |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
9 #else |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
10 #include <unistd.h> |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
11 #include <errno.h> |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
12 #endif |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 static char **current_path; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 |
2477
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
16 static char *sticky_path_path(void) |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
17 { |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
18 if (is_config_in_exe_dir(config)) { |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
19 return path_append(get_exe_dir(), "sticky_path"); |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
20 } else { |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
21 return path_append(get_config_dir(), "sticky_path"); |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
22 } |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
23 } |
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
24 |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 static void persist_path(void) |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 { |
2477
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
27 char *pathfname = sticky_path_path(); |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 FILE *f = fopen(pathfname, "wb"); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 if (f) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 if (fwrite(*current_path, 1, strlen(*current_path), f) != strlen(*current_path)) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 warning("Failed to save menu path"); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 fclose(f); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 } else { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 warning("Failed to save menu path: Could not open %s for writing\n", pathfname); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 free(pathfname); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 #ifdef __ANDROID__ |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 #include <SDL.h> |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 #include <jni.h> |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 static char *get_external_storage_path() |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 static char *ret; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 if (ret) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 return ret; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 JNIEnv *env = SDL_AndroidGetJNIEnv(); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 if ((*env)->PushLocalFrame(env, 8) < 0) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 return NULL; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 jclass Environment = (*env)->FindClass(env, "android/os/Environment"); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 jmethodID getExternalStorageDirectory = |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 (*env)->GetStaticMethodID(env, Environment, "getExternalStorageDirectory", "()Ljava/io/File;"); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 jobject file = (*env)->CallStaticObjectMethod(env, Environment, getExternalStorageDirectory); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 if (!file) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 goto cleanup; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 jmethodID getAbsolutePath = (*env)->GetMethodID(env, (*env)->GetObjectClass(env, file), |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 "getAbsolutePath", "()Ljava/lang/String;"); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 jstring path = (*env)->CallObjectMethod(env, file, getAbsolutePath); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 char const *tmp = (*env)->GetStringUTFChars(env, path, NULL); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 ret = strdup(tmp); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 (*env)->ReleaseStringUTFChars(env, path, tmp); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 cleanup: |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 (*env)->PopLocalFrame(env, NULL); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 return ret; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 #endif |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 void get_initial_browse_path(char **dst) |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 { |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
79 char *base = NULL; |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 char *remember_path = tern_find_path(config, "ui\0remember_path\0", TVAL_PTR).ptrval; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 if (!remember_path || !strcmp("on", remember_path)) { |
2477
2972a8e16ed2
Make sticky_path respect save config with EXE option
Michael Pavone <pavone@retrodev.com>
parents:
2262
diff
changeset
|
82 char *pathfname = sticky_path_path(); |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 FILE *f = fopen(pathfname, "rb"); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 if (f) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 long pathsize = file_size(f); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 if (pathsize > 0) { |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
87 base = malloc(pathsize + 1); |
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
88 if (fread(base, 1, pathsize, f) != pathsize) { |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 warning("Error restoring saved file browser path"); |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
90 free(base); |
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
91 base = NULL; |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 } else { |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
93 base[pathsize] = 0; |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 fclose(f); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 free(pathfname); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 if (!current_path) { |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 atexit(persist_path); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 current_path = dst; |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 } |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
104 if (!base) { |
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
105 base = tern_find_path(config, "ui\0initial_path\0", TVAL_PTR).ptrval; |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 } |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
107 if (!base){ |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 #ifdef __ANDROID__ |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
109 base = get_external_storage_path(); |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 #else |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
111 base = "$HOME"; |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 #endif |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 } |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 tern_node *vars = tern_insert_ptr(NULL, "HOME", get_home_dir()); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 vars = tern_insert_ptr(vars, "EXEDIR", get_exe_dir()); |
1858
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
116 *dst = replace_vars(base, vars, 1); |
dda7479f3bbb
Fix a couple of small memory leaks
Michael Pavone <pavone@retrodev.com>
parents:
1673
diff
changeset
|
117 free(base); |
1473
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 tern_free(vars); |
152a60c6787e
Moved initial path logic out of menu so it can be shared with new UI
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 } |
1481
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
120 |
1489
637fbc3b5063
Added code to persist config back to a file
Michael Pavone <pavone@retrodev.com>
parents:
1481
diff
changeset
|
121 char *path_append(const char *base, const char *suffix) |
1481
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
122 { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
123 if (!strcmp(suffix, "..")) { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
124 #ifdef _WIN32 |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
125 //handle transition from root of a drive to virtual root |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
126 if (base[1] == ':' && !base[2]) { |
1524
b96f9fae757f
Fix Windows build, added Windows default_font_path implementation
Michael Pavone <pavone@retrodev.com>
parents:
1489
diff
changeset
|
127 return strdup(PATH_SEP); |
1481
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
128 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
129 #endif |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
130 size_t len = strlen(base); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
131 while (len > 0) { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
132 --len; |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
133 if (is_path_sep(base[len])) { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
134 if (!len) { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
135 //special handling for / |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
136 len++; |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
137 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
138 char *ret = malloc(len+1); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
139 memcpy(ret, base, len); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
140 ret[len] = 0; |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
141 return ret; |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
142 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
143 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
144 return strdup(PATH_SEP); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
145 } else { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
146 #ifdef _WIN32 |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
147 if (base[0] == PATH_SEP[0] && !base[1]) { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
148 //handle transition from virtual root to root of a drive |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
149 return strdup(suffix); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
150 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
151 #endif |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
152 if (is_path_sep(base[strlen(base) - 1])) { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
153 return alloc_concat(base, suffix); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
154 } else { |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
155 char const *pieces[] = {base, PATH_SEP, suffix}; |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
156 return alloc_concat_m(3, pieces); |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
157 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
158 } |
77a401044935
Fix directory navigation in ROM file chooser in Nuklear UI
Michael Pavone <pavone@retrodev.com>
parents:
1473
diff
changeset
|
159 } |
2262
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
160 |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
161 char *path_current_dir(void) |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
162 { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
163 size_t size = 128; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
164 char *res = malloc(size); |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
165 for (;;) { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
166 #ifdef _WIN32 |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
167 DWORD actual = GetCurrentDirectoryA(size, res); |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
168 if (actual > size) { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
169 res = realloc(res, actual); |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
170 size = actual; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
171 } else { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
172 return res; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
173 } |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
174 #else |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
175 errno = 0; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
176 char *tmp = getcwd(res, size); |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
177 if (!tmp) { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
178 if (errno == ERANGE) { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
179 size *= 2; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
180 res = realloc(res, size); |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
181 } else { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
182 free(res); |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
183 return NULL; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
184 } |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
185 } else { |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
186 return res; |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
187 } |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
188 #endif |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
189 } |
bc68560b4a04
Fix bug when loading cue sheet without leading path
Michael Pavone <pavone@retrodev.com>
parents:
1858
diff
changeset
|
190 } |