diff options
Diffstat (limited to 'contrib/SDL-3.2.8/src/filesystem/windows')
| -rw-r--r-- | contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c | 382 | ||||
| -rw-r--r-- | contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c | 231 |
2 files changed, 613 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c new file mode 100644 index 0000000..39ba414 --- /dev/null +++ b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfilesystem.c | |||
| @@ -0,0 +1,382 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | #include "SDL_internal.h" | ||
| 22 | |||
| 23 | #ifdef SDL_FILESYSTEM_WINDOWS | ||
| 24 | |||
| 25 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 26 | // System dependent filesystem routines | ||
| 27 | |||
| 28 | #include "../SDL_sysfilesystem.h" | ||
| 29 | |||
| 30 | #include "../../core/windows/SDL_windows.h" | ||
| 31 | #include <shlobj.h> | ||
| 32 | #include <initguid.h> | ||
| 33 | |||
| 34 | // These aren't all defined in older SDKs, so define them here | ||
| 35 | DEFINE_GUID(SDL_FOLDERID_Profile, 0x5E6C858F, 0x0E22, 0x4760, 0x9A, 0xFE, 0xEA, 0x33, 0x17, 0xB6, 0x71, 0x73); | ||
| 36 | DEFINE_GUID(SDL_FOLDERID_Desktop, 0xB4BFCC3A, 0xDB2C, 0x424C, 0xB0, 0x29, 0x7F, 0xE9, 0x9A, 0x87, 0xC6, 0x41); | ||
| 37 | DEFINE_GUID(SDL_FOLDERID_Documents, 0xFDD39AD0, 0x238F, 0x46AF, 0xAD, 0xB4, 0x6C, 0x85, 0x48, 0x03, 0x69, 0xC7); | ||
| 38 | DEFINE_GUID(SDL_FOLDERID_Downloads, 0x374de290, 0x123f, 0x4565, 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b); | ||
| 39 | DEFINE_GUID(SDL_FOLDERID_Music, 0x4BD8D571, 0x6D19, 0x48D3, 0xBE, 0x97, 0x42, 0x22, 0x20, 0x08, 0x0E, 0x43); | ||
| 40 | DEFINE_GUID(SDL_FOLDERID_Pictures, 0x33E28130, 0x4E1E, 0x4676, 0x83, 0x5A, 0x98, 0x39, 0x5C, 0x3B, 0xC3, 0xBB); | ||
| 41 | DEFINE_GUID(SDL_FOLDERID_SavedGames, 0x4c5c32ff, 0xbb9d, 0x43b0, 0xb5, 0xb4, 0x2d, 0x72, 0xe5, 0x4e, 0xaa, 0xa4); | ||
| 42 | DEFINE_GUID(SDL_FOLDERID_Screenshots, 0xb7bede81, 0xdf94, 0x4682, 0xa7, 0xd8, 0x57, 0xa5, 0x26, 0x20, 0xb8, 0x6f); | ||
| 43 | DEFINE_GUID(SDL_FOLDERID_Templates, 0xA63293E8, 0x664E, 0x48DB, 0xA0, 0x79, 0xDF, 0x75, 0x9E, 0x05, 0x09, 0xF7); | ||
| 44 | DEFINE_GUID(SDL_FOLDERID_Videos, 0x18989B1D, 0x99B5, 0x455B, 0x84, 0x1C, 0xAB, 0x7C, 0x74, 0xE4, 0xDD, 0xFC); | ||
| 45 | |||
| 46 | char *SDL_SYS_GetBasePath(void) | ||
| 47 | { | ||
| 48 | DWORD buflen = 128; | ||
| 49 | WCHAR *path = NULL; | ||
| 50 | char *result = NULL; | ||
| 51 | DWORD len = 0; | ||
| 52 | int i; | ||
| 53 | |||
| 54 | while (true) { | ||
| 55 | void *ptr = SDL_realloc(path, buflen * sizeof(WCHAR)); | ||
| 56 | if (!ptr) { | ||
| 57 | SDL_free(path); | ||
| 58 | return NULL; | ||
| 59 | } | ||
| 60 | |||
| 61 | path = (WCHAR *)ptr; | ||
| 62 | |||
| 63 | len = GetModuleFileNameW(NULL, path, buflen); | ||
| 64 | // if it truncated, then len >= buflen - 1 | ||
| 65 | // if there was enough room (or failure), len < buflen - 1 | ||
| 66 | if (len < buflen - 1) { | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | |||
| 70 | // buffer too small? Try again. | ||
| 71 | buflen *= 2; | ||
| 72 | } | ||
| 73 | |||
| 74 | if (len == 0) { | ||
| 75 | SDL_free(path); | ||
| 76 | WIN_SetError("Couldn't locate our .exe"); | ||
| 77 | return NULL; | ||
| 78 | } | ||
| 79 | |||
| 80 | for (i = len - 1; i > 0; i--) { | ||
| 81 | if (path[i] == '\\') { | ||
| 82 | break; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | SDL_assert(i > 0); // Should have been an absolute path. | ||
| 87 | path[i + 1] = '\0'; // chop off filename. | ||
| 88 | |||
| 89 | result = WIN_StringToUTF8W(path); | ||
| 90 | SDL_free(path); | ||
| 91 | |||
| 92 | return result; | ||
| 93 | } | ||
| 94 | |||
| 95 | char *SDL_SYS_GetPrefPath(const char *org, const char *app) | ||
| 96 | { | ||
| 97 | /* | ||
| 98 | * Vista and later has a new API for this, but SHGetFolderPath works there, | ||
| 99 | * and apparently just wraps the new API. This is the new way to do it: | ||
| 100 | * | ||
| 101 | * SHGetKnownFolderPath(SDL_FOLDERID_RoamingAppData, KF_FLAG_CREATE, | ||
| 102 | * NULL, &wszPath); | ||
| 103 | */ | ||
| 104 | |||
| 105 | HRESULT hr = E_FAIL; | ||
| 106 | WCHAR path[MAX_PATH]; | ||
| 107 | char *result = NULL; | ||
| 108 | WCHAR *worg = NULL; | ||
| 109 | WCHAR *wapp = NULL; | ||
| 110 | size_t new_wpath_len = 0; | ||
| 111 | BOOL api_result = FALSE; | ||
| 112 | |||
| 113 | if (!app) { | ||
| 114 | SDL_InvalidParamError("app"); | ||
| 115 | return NULL; | ||
| 116 | } | ||
| 117 | if (!org) { | ||
| 118 | org = ""; | ||
| 119 | } | ||
| 120 | |||
| 121 | hr = SHGetFolderPathW(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, path); | ||
| 122 | if (!SUCCEEDED(hr)) { | ||
| 123 | WIN_SetErrorFromHRESULT("Couldn't locate our prefpath", hr); | ||
| 124 | return NULL; | ||
| 125 | } | ||
| 126 | |||
| 127 | worg = WIN_UTF8ToStringW(org); | ||
| 128 | if (!worg) { | ||
| 129 | return NULL; | ||
| 130 | } | ||
| 131 | |||
| 132 | wapp = WIN_UTF8ToStringW(app); | ||
| 133 | if (!wapp) { | ||
| 134 | SDL_free(worg); | ||
| 135 | return NULL; | ||
| 136 | } | ||
| 137 | |||
| 138 | new_wpath_len = SDL_wcslen(worg) + SDL_wcslen(wapp) + SDL_wcslen(path) + 3; | ||
| 139 | |||
| 140 | if ((new_wpath_len + 1) > MAX_PATH) { | ||
| 141 | SDL_free(worg); | ||
| 142 | SDL_free(wapp); | ||
| 143 | WIN_SetError("Path too long."); | ||
| 144 | return NULL; | ||
| 145 | } | ||
| 146 | |||
| 147 | if (*worg) { | ||
| 148 | SDL_wcslcat(path, L"\\", SDL_arraysize(path)); | ||
| 149 | SDL_wcslcat(path, worg, SDL_arraysize(path)); | ||
| 150 | } | ||
| 151 | SDL_free(worg); | ||
| 152 | |||
| 153 | api_result = CreateDirectoryW(path, NULL); | ||
| 154 | if (api_result == FALSE) { | ||
| 155 | if (GetLastError() != ERROR_ALREADY_EXISTS) { | ||
| 156 | SDL_free(wapp); | ||
| 157 | WIN_SetError("Couldn't create a prefpath."); | ||
| 158 | return NULL; | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | SDL_wcslcat(path, L"\\", SDL_arraysize(path)); | ||
| 163 | SDL_wcslcat(path, wapp, SDL_arraysize(path)); | ||
| 164 | SDL_free(wapp); | ||
| 165 | |||
| 166 | api_result = CreateDirectoryW(path, NULL); | ||
| 167 | if (api_result == FALSE) { | ||
| 168 | if (GetLastError() != ERROR_ALREADY_EXISTS) { | ||
| 169 | WIN_SetError("Couldn't create a prefpath."); | ||
| 170 | return NULL; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | SDL_wcslcat(path, L"\\", SDL_arraysize(path)); | ||
| 175 | |||
| 176 | result = WIN_StringToUTF8W(path); | ||
| 177 | |||
| 178 | return result; | ||
| 179 | } | ||
| 180 | |||
| 181 | char *SDL_SYS_GetUserFolder(SDL_Folder folder) | ||
| 182 | { | ||
| 183 | typedef HRESULT (WINAPI *pfnSHGetKnownFolderPath)(REFGUID /* REFKNOWNFOLDERID */, DWORD, HANDLE, PWSTR*); | ||
| 184 | HMODULE lib = LoadLibrary(L"Shell32.dll"); | ||
| 185 | pfnSHGetKnownFolderPath pSHGetKnownFolderPath = NULL; | ||
| 186 | char *result = NULL; | ||
| 187 | |||
| 188 | if (lib) { | ||
| 189 | pSHGetKnownFolderPath = (pfnSHGetKnownFolderPath)GetProcAddress(lib, "SHGetKnownFolderPath"); | ||
| 190 | } | ||
| 191 | |||
| 192 | if (pSHGetKnownFolderPath) { | ||
| 193 | GUID type; // KNOWNFOLDERID | ||
| 194 | HRESULT hr; | ||
| 195 | wchar_t *path; | ||
| 196 | |||
| 197 | switch (folder) { | ||
| 198 | case SDL_FOLDER_HOME: | ||
| 199 | type = SDL_FOLDERID_Profile; | ||
| 200 | break; | ||
| 201 | |||
| 202 | case SDL_FOLDER_DESKTOP: | ||
| 203 | type = SDL_FOLDERID_Desktop; | ||
| 204 | break; | ||
| 205 | |||
| 206 | case SDL_FOLDER_DOCUMENTS: | ||
| 207 | type = SDL_FOLDERID_Documents; | ||
| 208 | break; | ||
| 209 | |||
| 210 | case SDL_FOLDER_DOWNLOADS: | ||
| 211 | type = SDL_FOLDERID_Downloads; | ||
| 212 | break; | ||
| 213 | |||
| 214 | case SDL_FOLDER_MUSIC: | ||
| 215 | type = SDL_FOLDERID_Music; | ||
| 216 | break; | ||
| 217 | |||
| 218 | case SDL_FOLDER_PICTURES: | ||
| 219 | type = SDL_FOLDERID_Pictures; | ||
| 220 | break; | ||
| 221 | |||
| 222 | case SDL_FOLDER_PUBLICSHARE: | ||
| 223 | SDL_SetError("Public share unavailable on Windows"); | ||
| 224 | goto done; | ||
| 225 | |||
| 226 | case SDL_FOLDER_SAVEDGAMES: | ||
| 227 | type = SDL_FOLDERID_SavedGames; | ||
| 228 | break; | ||
| 229 | |||
| 230 | case SDL_FOLDER_SCREENSHOTS: | ||
| 231 | type = SDL_FOLDERID_Screenshots; | ||
| 232 | break; | ||
| 233 | |||
| 234 | case SDL_FOLDER_TEMPLATES: | ||
| 235 | type = SDL_FOLDERID_Templates; | ||
| 236 | break; | ||
| 237 | |||
| 238 | case SDL_FOLDER_VIDEOS: | ||
| 239 | type = SDL_FOLDERID_Videos; | ||
| 240 | break; | ||
| 241 | |||
| 242 | default: | ||
| 243 | SDL_SetError("Invalid SDL_Folder: %d", (int)folder); | ||
| 244 | goto done; | ||
| 245 | }; | ||
| 246 | |||
| 247 | hr = pSHGetKnownFolderPath(&type, 0x00008000 /* KF_FLAG_CREATE */, NULL, &path); | ||
| 248 | if (SUCCEEDED(hr)) { | ||
| 249 | result = WIN_StringToUTF8W(path); | ||
| 250 | } else { | ||
| 251 | WIN_SetErrorFromHRESULT("Couldn't get folder", hr); | ||
| 252 | } | ||
| 253 | |||
| 254 | } else { | ||
| 255 | int type; | ||
| 256 | HRESULT hr; | ||
| 257 | wchar_t path[MAX_PATH]; | ||
| 258 | |||
| 259 | switch (folder) { | ||
| 260 | case SDL_FOLDER_HOME: | ||
| 261 | type = CSIDL_PROFILE; | ||
| 262 | break; | ||
| 263 | |||
| 264 | case SDL_FOLDER_DESKTOP: | ||
| 265 | type = CSIDL_DESKTOP; | ||
| 266 | break; | ||
| 267 | |||
| 268 | case SDL_FOLDER_DOCUMENTS: | ||
| 269 | type = CSIDL_MYDOCUMENTS; | ||
| 270 | break; | ||
| 271 | |||
| 272 | case SDL_FOLDER_DOWNLOADS: | ||
| 273 | SDL_SetError("Downloads folder unavailable before Vista"); | ||
| 274 | goto done; | ||
| 275 | |||
| 276 | case SDL_FOLDER_MUSIC: | ||
| 277 | type = CSIDL_MYMUSIC; | ||
| 278 | break; | ||
| 279 | |||
| 280 | case SDL_FOLDER_PICTURES: | ||
| 281 | type = CSIDL_MYPICTURES; | ||
| 282 | break; | ||
| 283 | |||
| 284 | case SDL_FOLDER_PUBLICSHARE: | ||
| 285 | SDL_SetError("Public share unavailable on Windows"); | ||
| 286 | goto done; | ||
| 287 | |||
| 288 | case SDL_FOLDER_SAVEDGAMES: | ||
| 289 | SDL_SetError("Saved games unavailable before Vista"); | ||
| 290 | goto done; | ||
| 291 | |||
| 292 | case SDL_FOLDER_SCREENSHOTS: | ||
| 293 | SDL_SetError("Screenshots folder unavailable before Vista"); | ||
| 294 | goto done; | ||
| 295 | |||
| 296 | case SDL_FOLDER_TEMPLATES: | ||
| 297 | type = CSIDL_TEMPLATES; | ||
| 298 | break; | ||
| 299 | |||
| 300 | case SDL_FOLDER_VIDEOS: | ||
| 301 | type = CSIDL_MYVIDEO; | ||
| 302 | break; | ||
| 303 | |||
| 304 | default: | ||
| 305 | SDL_SetError("Unsupported SDL_Folder on Windows before Vista: %d", (int)folder); | ||
| 306 | goto done; | ||
| 307 | }; | ||
| 308 | |||
| 309 | // Create the OS-specific folder if it doesn't already exist | ||
| 310 | type |= CSIDL_FLAG_CREATE; | ||
| 311 | |||
| 312 | #if 0 | ||
| 313 | // Apparently the oldest, but not supported in modern Windows | ||
| 314 | HRESULT hr = SHGetSpecialFolderPath(NULL, path, type, TRUE); | ||
| 315 | #endif | ||
| 316 | |||
| 317 | /* Windows 2000/XP and later, deprecated as of Windows 10 (still | ||
| 318 | available), available in Wine (tested 6.0.3) */ | ||
| 319 | hr = SHGetFolderPathW(NULL, type, NULL, SHGFP_TYPE_CURRENT, path); | ||
| 320 | |||
| 321 | // use `== TRUE` for SHGetSpecialFolderPath | ||
| 322 | if (SUCCEEDED(hr)) { | ||
| 323 | result = WIN_StringToUTF8W(path); | ||
| 324 | } else { | ||
| 325 | WIN_SetErrorFromHRESULT("Couldn't get folder", hr); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | if (result) { | ||
| 330 | char *newresult = (char *) SDL_realloc(result, SDL_strlen(result) + 2); | ||
| 331 | |||
| 332 | if (!newresult) { | ||
| 333 | SDL_free(result); | ||
| 334 | result = NULL; // will be returned | ||
| 335 | goto done; | ||
| 336 | } | ||
| 337 | |||
| 338 | result = newresult; | ||
| 339 | SDL_strlcat(result, "\\", SDL_strlen(result) + 2); | ||
| 340 | } | ||
| 341 | |||
| 342 | done: | ||
| 343 | if (lib) { | ||
| 344 | FreeLibrary(lib); | ||
| 345 | } | ||
| 346 | return result; | ||
| 347 | } | ||
| 348 | |||
| 349 | char *SDL_SYS_GetCurrentDirectory(void) | ||
| 350 | { | ||
| 351 | WCHAR *wstr = NULL; | ||
| 352 | DWORD buflen = 0; | ||
| 353 | while (true) { | ||
| 354 | const DWORD bw = GetCurrentDirectoryW(buflen, wstr); | ||
| 355 | if (bw == 0) { | ||
| 356 | WIN_SetError("GetCurrentDirectoryW failed"); | ||
| 357 | return NULL; | ||
| 358 | } else if (bw < buflen) { // we got it! | ||
| 359 | // make sure there's a path separator at the end. | ||
| 360 | SDL_assert(bw < (buflen + 2)); | ||
| 361 | if ((bw == 0) || (wstr[bw-1] != '\\')) { | ||
| 362 | wstr[bw] = '\\'; | ||
| 363 | wstr[bw + 1] = '\0'; | ||
| 364 | } | ||
| 365 | break; | ||
| 366 | } | ||
| 367 | |||
| 368 | void *ptr = SDL_realloc(wstr, (bw + 1) * sizeof (WCHAR)); | ||
| 369 | if (!ptr) { | ||
| 370 | SDL_free(wstr); | ||
| 371 | return NULL; | ||
| 372 | } | ||
| 373 | wstr = (WCHAR *) ptr; | ||
| 374 | buflen = bw; | ||
| 375 | } | ||
| 376 | |||
| 377 | char *retval = WIN_StringToUTF8W(wstr); | ||
| 378 | SDL_free(wstr); | ||
| 379 | return retval; | ||
| 380 | } | ||
| 381 | |||
| 382 | #endif // SDL_FILESYSTEM_WINDOWS | ||
diff --git a/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c new file mode 100644 index 0000000..9c48ba9 --- /dev/null +++ b/contrib/SDL-3.2.8/src/filesystem/windows/SDL_sysfsops.c | |||
| @@ -0,0 +1,231 @@ | |||
| 1 | /* | ||
| 2 | Simple DirectMedia Layer | ||
| 3 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 4 | |||
| 5 | This software is provided 'as-is', without any express or implied | ||
| 6 | warranty. In no event will the authors be held liable for any damages | ||
| 7 | arising from the use of this software. | ||
| 8 | |||
| 9 | Permission is granted to anyone to use this software for any purpose, | ||
| 10 | including commercial applications, and to alter it and redistribute it | ||
| 11 | freely, subject to the following restrictions: | ||
| 12 | |||
| 13 | 1. The origin of this software must not be misrepresented; you must not | ||
| 14 | claim that you wrote the original software. If you use this software | ||
| 15 | in a product, an acknowledgment in the product documentation would be | ||
| 16 | appreciated but is not required. | ||
| 17 | 2. Altered source versions must be plainly marked as such, and must not be | ||
| 18 | misrepresented as being the original software. | ||
| 19 | 3. This notice may not be removed or altered from any source distribution. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #include "SDL_internal.h" | ||
| 23 | |||
| 24 | #if defined(SDL_FSOPS_WINDOWS) | ||
| 25 | |||
| 26 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ | ||
| 27 | // System dependent filesystem routines | ||
| 28 | |||
| 29 | #include "../../core/windows/SDL_windows.h" | ||
| 30 | #include "../SDL_sysfilesystem.h" | ||
| 31 | |||
| 32 | bool SDL_SYS_EnumerateDirectory(const char *path, SDL_EnumerateDirectoryCallback cb, void *userdata) | ||
| 33 | { | ||
| 34 | SDL_EnumerationResult result = SDL_ENUM_CONTINUE; | ||
| 35 | if (*path == '\0') { // if empty (completely at the root), we need to enumerate drive letters. | ||
| 36 | const DWORD drives = GetLogicalDrives(); | ||
| 37 | char name[] = { 0, ':', '\\', '\0' }; | ||
| 38 | for (int i = 'A'; (result == SDL_ENUM_CONTINUE) && (i <= 'Z'); i++) { | ||
| 39 | if (drives & (1 << (i - 'A'))) { | ||
| 40 | name[0] = (char) i; | ||
| 41 | result = cb(userdata, "", name); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } else { | ||
| 45 | // you need a wildcard to enumerate through FindFirstFileEx(), but the wildcard is only checked in the | ||
| 46 | // filename element at the end of the path string, so always tack on a "\\*" to get everything, and | ||
| 47 | // also prevent any wildcards inserted by the app from being respected. | ||
| 48 | char *pattern = NULL; | ||
| 49 | int patternlen = SDL_asprintf(&pattern, "%s\\\\", path); // we'll replace that second '\\' in the trimdown. | ||
| 50 | if ((patternlen == -1) || (!pattern)) { | ||
| 51 | return false; | ||
| 52 | } | ||
| 53 | |||
| 54 | // trim down to a single path separator at the end, in case the caller added one or more. | ||
| 55 | patternlen--; | ||
| 56 | while ((patternlen >= 0) && ((pattern[patternlen] == '\\') || (pattern[patternlen] == '/'))) { | ||
| 57 | pattern[patternlen--] ='\0'; | ||
| 58 | } | ||
| 59 | pattern[++patternlen] = '\\'; | ||
| 60 | pattern[++patternlen] = '*'; | ||
| 61 | pattern[++patternlen] = '\0'; | ||
| 62 | |||
| 63 | WCHAR *wpattern = WIN_UTF8ToStringW(pattern); | ||
| 64 | if (!wpattern) { | ||
| 65 | SDL_free(pattern); | ||
| 66 | return false; | ||
| 67 | } | ||
| 68 | |||
| 69 | pattern[--patternlen] = '\0'; // chop off the '*' so we just have the dirname with a path separator. | ||
| 70 | |||
| 71 | WIN32_FIND_DATAW entw; | ||
| 72 | HANDLE dir = FindFirstFileExW(wpattern, FindExInfoStandard, &entw, FindExSearchNameMatch, NULL, 0); | ||
| 73 | SDL_free(wpattern); | ||
| 74 | if (dir == INVALID_HANDLE_VALUE) { | ||
| 75 | SDL_free(pattern); | ||
| 76 | return WIN_SetError("Failed to enumerate directory"); | ||
| 77 | } | ||
| 78 | |||
| 79 | do { | ||
| 80 | const WCHAR *fn = entw.cFileName; | ||
| 81 | |||
| 82 | if (fn[0] == '.') { // ignore "." and ".." | ||
| 83 | if ((fn[1] == '\0') || ((fn[1] == '.') && (fn[2] == '\0'))) { | ||
| 84 | continue; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | char *utf8fn = WIN_StringToUTF8W(fn); | ||
| 89 | if (!utf8fn) { | ||
| 90 | result = SDL_ENUM_FAILURE; | ||
| 91 | } else { | ||
| 92 | result = cb(userdata, pattern, utf8fn); | ||
| 93 | SDL_free(utf8fn); | ||
| 94 | } | ||
| 95 | } while ((result == SDL_ENUM_CONTINUE) && (FindNextFileW(dir, &entw) != 0)); | ||
| 96 | |||
| 97 | FindClose(dir); | ||
| 98 | SDL_free(pattern); | ||
| 99 | } | ||
| 100 | |||
| 101 | return (result != SDL_ENUM_FAILURE); | ||
| 102 | } | ||
| 103 | |||
| 104 | bool SDL_SYS_RemovePath(const char *path) | ||
| 105 | { | ||
| 106 | WCHAR *wpath = WIN_UTF8ToStringW(path); | ||
| 107 | if (!wpath) { | ||
| 108 | return false; | ||
| 109 | } | ||
| 110 | |||
| 111 | WIN32_FILE_ATTRIBUTE_DATA info; | ||
| 112 | if (!GetFileAttributesExW(wpath, GetFileExInfoStandard, &info)) { | ||
| 113 | SDL_free(wpath); | ||
| 114 | if (GetLastError() == ERROR_FILE_NOT_FOUND) { | ||
| 115 | // Note that ERROR_PATH_NOT_FOUND means a parent dir is missing, and we consider that an error. | ||
| 116 | return true; // thing is already gone, call it a success. | ||
| 117 | } | ||
| 118 | return WIN_SetError("Couldn't get path's attributes"); | ||
| 119 | } | ||
| 120 | |||
| 121 | const int isdir = (info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY); | ||
| 122 | const BOOL rc = isdir ? RemoveDirectoryW(wpath) : DeleteFileW(wpath); | ||
| 123 | SDL_free(wpath); | ||
| 124 | if (!rc) { | ||
| 125 | return WIN_SetError("Couldn't remove path"); | ||
| 126 | } | ||
| 127 | return true; | ||
| 128 | } | ||
| 129 | |||
| 130 | bool SDL_SYS_RenamePath(const char *oldpath, const char *newpath) | ||
| 131 | { | ||
| 132 | WCHAR *woldpath = WIN_UTF8ToStringW(oldpath); | ||
| 133 | if (!woldpath) { | ||
| 134 | return false; | ||
| 135 | } | ||
| 136 | |||
| 137 | WCHAR *wnewpath = WIN_UTF8ToStringW(newpath); | ||
| 138 | if (!wnewpath) { | ||
| 139 | SDL_free(woldpath); | ||
| 140 | return false; | ||
| 141 | } | ||
| 142 | |||
| 143 | const BOOL rc = MoveFileExW(woldpath, wnewpath, MOVEFILE_REPLACE_EXISTING); | ||
| 144 | SDL_free(wnewpath); | ||
| 145 | SDL_free(woldpath); | ||
| 146 | if (!rc) { | ||
| 147 | return WIN_SetError("Couldn't rename path"); | ||
| 148 | } | ||
| 149 | return true; | ||
| 150 | } | ||
| 151 | |||
| 152 | bool SDL_SYS_CopyFile(const char *oldpath, const char *newpath) | ||
| 153 | { | ||
| 154 | WCHAR *woldpath = WIN_UTF8ToStringW(oldpath); | ||
| 155 | if (!woldpath) { | ||
| 156 | return false; | ||
| 157 | } | ||
| 158 | |||
| 159 | WCHAR *wnewpath = WIN_UTF8ToStringW(newpath); | ||
| 160 | if (!wnewpath) { | ||
| 161 | SDL_free(woldpath); | ||
| 162 | return false; | ||
| 163 | } | ||
| 164 | |||
| 165 | const BOOL rc = CopyFileExW(woldpath, wnewpath, NULL, NULL, NULL, COPY_FILE_ALLOW_DECRYPTED_DESTINATION|COPY_FILE_NO_BUFFERING); | ||
| 166 | SDL_free(wnewpath); | ||
| 167 | SDL_free(woldpath); | ||
| 168 | if (!rc) { | ||
| 169 | return WIN_SetError("Couldn't copy path"); | ||
| 170 | } | ||
| 171 | return true; | ||
| 172 | } | ||
| 173 | |||
| 174 | bool SDL_SYS_CreateDirectory(const char *path) | ||
| 175 | { | ||
| 176 | WCHAR *wpath = WIN_UTF8ToStringW(path); | ||
| 177 | if (!wpath) { | ||
| 178 | return false; | ||
| 179 | } | ||
| 180 | |||
| 181 | DWORD rc = CreateDirectoryW(wpath, NULL); | ||
| 182 | if (!rc && (GetLastError() == ERROR_ALREADY_EXISTS)) { | ||
| 183 | WIN32_FILE_ATTRIBUTE_DATA winstat; | ||
| 184 | if (GetFileAttributesExW(wpath, GetFileExInfoStandard, &winstat)) { | ||
| 185 | if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | ||
| 186 | rc = 1; // exists and is already a directory: cool. | ||
| 187 | } | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | SDL_free(wpath); | ||
| 192 | if (!rc) { | ||
| 193 | return WIN_SetError("Couldn't create directory"); | ||
| 194 | } | ||
| 195 | return true; | ||
| 196 | } | ||
| 197 | |||
| 198 | bool SDL_SYS_GetPathInfo(const char *path, SDL_PathInfo *info) | ||
| 199 | { | ||
| 200 | WCHAR *wpath = WIN_UTF8ToStringW(path); | ||
| 201 | if (!wpath) { | ||
| 202 | return false; | ||
| 203 | } | ||
| 204 | |||
| 205 | WIN32_FILE_ATTRIBUTE_DATA winstat; | ||
| 206 | const BOOL rc = GetFileAttributesExW(wpath, GetFileExInfoStandard, &winstat); | ||
| 207 | SDL_free(wpath); | ||
| 208 | if (!rc) { | ||
| 209 | return WIN_SetError("Can't stat"); | ||
| 210 | } | ||
| 211 | |||
| 212 | if (winstat.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { | ||
| 213 | info->type = SDL_PATHTYPE_DIRECTORY; | ||
| 214 | info->size = 0; | ||
| 215 | } else if (winstat.dwFileAttributes & (FILE_ATTRIBUTE_OFFLINE | FILE_ATTRIBUTE_DEVICE)) { | ||
| 216 | info->type = SDL_PATHTYPE_OTHER; | ||
| 217 | info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow); | ||
| 218 | } else { | ||
| 219 | info->type = SDL_PATHTYPE_FILE; | ||
| 220 | info->size = ((((Uint64) winstat.nFileSizeHigh) << 32) | winstat.nFileSizeLow); | ||
| 221 | } | ||
| 222 | |||
| 223 | info->create_time = SDL_TimeFromWindows(winstat.ftCreationTime.dwLowDateTime, winstat.ftCreationTime.dwHighDateTime); | ||
| 224 | info->modify_time = SDL_TimeFromWindows(winstat.ftLastWriteTime.dwLowDateTime, winstat.ftLastWriteTime.dwHighDateTime); | ||
| 225 | info->access_time = SDL_TimeFromWindows(winstat.ftLastAccessTime.dwLowDateTime, winstat.ftLastAccessTime.dwHighDateTime); | ||
| 226 | |||
| 227 | return true; | ||
| 228 | } | ||
| 229 | |||
| 230 | #endif // SDL_FSOPS_WINDOWS | ||
| 231 | |||
