|
39 | 39 | #include "../../core/android/SDL_android.h" |
40 | 40 | #endif |
41 | 41 |
|
| 42 | + |
42 | 43 | bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata) |
43 | 44 | { |
44 | | -#ifdef SDL_PLATFORM_ANDROID |
45 | | - if (*path != '/') { |
46 | | - char *apath = NULL; |
47 | | - SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path); |
48 | | - if (!apath) { |
49 | | - return false; |
50 | | - } |
51 | | - const bool retval = SDL_SYS_EnumerateDirectory(apath, cb, userdata); |
52 | | - SDL_free(apath); |
53 | | - if (retval) { |
54 | | - return true; |
55 | | - } |
56 | | - } |
57 | | -#endif |
| 45 | + char *apath = NULL; // absolute path (for Android, iOS, etc). Overrides `path`. |
58 | 46 |
|
59 | | -#ifdef SDL_PLATFORM_IOS |
| 47 | +#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_IOS) |
60 | 48 | if (*path != '/') { |
| 49 | + #ifdef SDL_PLATFORM_ANDROID |
| 50 | + SDL_asprintf(&apath, "%s/%s", SDL_GetAndroidInternalStoragePath(), path); |
| 51 | + #elif defined(SDL_PLATFORM_IOS) |
61 | 52 | char *base = SDL_GetPrefPath("", ""); |
62 | 53 | if (!base) { |
63 | 54 | return false; |
64 | 55 | } |
65 | 56 |
|
66 | | - char *apath = NULL; |
67 | 57 | SDL_asprintf(&apath, "%s%s", base, path); |
68 | 58 | SDL_free(base); |
| 59 | + #endif |
| 60 | + |
69 | 61 | if (!apath) { |
70 | 62 | return false; |
71 | 63 | } |
72 | | - const bool retval = SDL_SYS_EnumerateDirectory(apath, cb, userdata); |
73 | | - SDL_free(apath); |
74 | | - if (retval) { |
75 | | - return true; |
| 64 | + } |
| 65 | +#elif 0 // this is just for testing that `apath` works when you aren't on iOS or Android. |
| 66 | + if (*path != '/') { |
| 67 | + char *c = SDL_SYS_GetCurrentDirectory(); |
| 68 | + SDL_asprintf(&apath, "%s%s", c, path); |
| 69 | + SDL_free(c); |
| 70 | + if (!apath) { |
| 71 | + return false; |
76 | 72 | } |
77 | 73 | } |
78 | 74 | #endif |
79 | 75 |
|
80 | 76 | char *pathwithsep = NULL; |
81 | | - int pathwithseplen = SDL_asprintf(&pathwithsep, "%s/", path); |
| 77 | + int pathwithseplen = SDL_asprintf(&pathwithsep, "%s/", apath ? apath : path); |
| 78 | + const size_t extralen = apath ? (SDL_strlen(apath) - SDL_strlen(path)) : 0; |
| 79 | + SDL_free(apath); |
82 | 80 | if ((pathwithseplen == -1) || (!pathwithsep)) { |
83 | 81 | return false; |
84 | 82 | } |
@@ -112,7 +110,7 @@ bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback |
112 | 110 | if ((SDL_strcmp(name, ".") == 0) || (SDL_strcmp(name, "..") == 0)) { |
113 | 111 | continue; |
114 | 112 | } |
115 | | - result = cb(userdata, pathwithsep, name); |
| 113 | + result = cb(userdata, pathwithsep + extralen, name); |
116 | 114 | } |
117 | 115 |
|
118 | 116 | closedir(dir); |
|
0 commit comments