|
22 | 22 | #include <windows.h> |
23 | 23 | #endif |
24 | 24 |
|
25 | | -#ifdef SCP_UNIX |
26 | | -#include <glob.h> |
27 | | -#endif |
28 | | - |
29 | 25 | #include "cfile/cfile.h" |
30 | 26 | #include "cfile/cfilearchive.h" |
31 | 27 | #include "cfile/cfilesystem.h" |
@@ -369,56 +365,41 @@ int cfile_pop_dir() |
369 | 365 | int cfile_flush_dir(int dir_type) |
370 | 366 | { |
371 | 367 | int del_count; |
| 368 | + SDL_PathInfo pinfo; |
| 369 | + SCP_string filespec; |
| 370 | + SCP_string fullpath; |
372 | 371 |
|
373 | 372 | Assert( CF_TYPE_SPECIFIED(dir_type) ); |
374 | 373 |
|
375 | | - // attempt to change the directory to the passed type |
376 | | - if(cfile_push_chdir(dir_type)){ |
377 | | - return 0; |
378 | | - } |
| 374 | + cf_create_default_path_string(filespec, dir_type); |
379 | 375 |
|
380 | 376 | // proceed to delete the files |
381 | 377 | del_count = 0; |
382 | | -#if defined _WIN32 |
383 | | - intptr_t find_handle; |
384 | | - _finddata_t find; |
385 | | - find_handle = _findfirst( "*", &find ); |
386 | | - if (find_handle != -1) { |
387 | | - do { |
388 | | - if (!(find.attrib & _A_SUBDIR) && !(find.attrib & _A_RDONLY)) { |
389 | | - // delete the file |
390 | | - cf_delete(find.name,dir_type); |
391 | 378 |
|
392 | | - // increment the deleted count |
393 | | - del_count++; |
| 379 | + auto results = SDL_GlobDirectory(filespec.c_str(), "*", 0, nullptr); |
| 380 | + |
| 381 | + if (results) { |
| 382 | + for (int i = 0; results[i]; ++i) { |
| 383 | + fullpath = filespec; |
| 384 | + fullpath += results[i]; |
| 385 | + |
| 386 | + if ( !SDL_GetPathInfo(fullpath.c_str(), &pinfo) ) { |
| 387 | + continue; |
| 388 | + } |
| 389 | + |
| 390 | + if (pinfo.type != SDL_PATHTYPE_FILE) { |
| 391 | + continue; |
394 | 392 | } |
395 | | - } while (!_findnext(find_handle, &find)); |
396 | | - _findclose( find_handle ); |
397 | | - } |
398 | | -#elif defined SCP_UNIX |
399 | | - glob_t globinfo; |
400 | | - memset(&globinfo, 0, sizeof(globinfo)); |
401 | | - int status = glob("*", 0, NULL, &globinfo); |
402 | | - if (status == 0) { |
403 | | - for (unsigned int i = 0; i < globinfo.gl_pathc; i++) { |
404 | | - // Determine if this is a regular file |
405 | | - struct stat statbuf; |
406 | | - |
407 | | - stat(globinfo.gl_pathv[i], &statbuf); |
408 | | - if (S_ISREG(statbuf.st_mode)) { |
409 | | - // delete the file |
410 | | - cf_delete(globinfo.gl_pathv[i], dir_type); |
411 | 393 |
|
| 394 | + // delete the file |
| 395 | + if ( SDL_RemovePath(fullpath.c_str()) ) { |
412 | 396 | // increment the deleted count |
413 | | - del_count++; |
| 397 | + ++del_count; |
414 | 398 | } |
415 | 399 | } |
416 | | - globfree(&globinfo); |
417 | | - } |
418 | | -#endif |
419 | 400 |
|
420 | | - // pop the directory back |
421 | | - cfile_pop_dir(); |
| 401 | + SDL_free(results); |
| 402 | + } |
422 | 403 |
|
423 | 404 | // return the # of files deleted |
424 | 405 | return del_count; |
@@ -463,7 +444,7 @@ int cf_delete(const char *filename, int path_type, uint32_t location_flags) |
463 | 444 |
|
464 | 445 | cf_create_default_path_string(longname, path_type, filename, location_flags); |
465 | 446 |
|
466 | | - return (_unlink(longname.c_str()) != -1); |
| 447 | + return SDL_RemovePath(longname.c_str()); |
467 | 448 | } |
468 | 449 |
|
469 | 450 |
|
@@ -542,80 +523,31 @@ int cf_rename(const char *old_name, const char *name, int dir_type) |
542 | 523 | { |
543 | 524 | Assert( CF_TYPE_SPECIFIED(dir_type) ); |
544 | 525 |
|
545 | | - int ret_code; |
546 | 526 | SCP_string old_longname; |
547 | 527 | SCP_string new_longname; |
548 | 528 |
|
549 | 529 | cf_create_default_path_string(old_longname, dir_type, old_name); |
550 | 530 | cf_create_default_path_string(new_longname, dir_type, name); |
551 | 531 |
|
552 | | - ret_code = rename(old_longname.c_str(), new_longname.c_str()); |
553 | | - if(ret_code != 0){ |
554 | | - switch(errno){ |
555 | | - case EACCES : |
556 | | - return CF_RENAME_FAIL_ACCESS; |
557 | | - case ENOENT : |
558 | | - default: |
559 | | - return CF_RENAME_FAIL_EXIST; |
560 | | - } |
| 532 | + if (SDL_RenamePath(old_longname.c_str(), new_longname.c_str())) { |
| 533 | + return CF_RENAME_SUCCESS; |
561 | 534 | } |
562 | 535 |
|
563 | | - return CF_RENAME_SUCCESS; |
564 | | - |
565 | | - |
566 | | -} |
567 | | - |
568 | | - |
569 | | -// This takes a path (e.g. "C:\Games\FreeSpace2\Lots\More\Directories") and creates it in its entirety. |
570 | | -// Do note that this requires the path to have normalized directory separators as defined by DIR_SEPARATOR_CHAR |
571 | | -static void mkdir_recursive(const char *path) { |
572 | | - size_t pre = 0, pos; |
573 | | - SCP_string tmp(path); |
574 | | - SCP_string dir; |
575 | | - |
576 | | - if (tmp[tmp.size() - 1] != DIR_SEPARATOR_CHAR) { |
577 | | - // force trailing / so we can handle everything in loop |
578 | | - tmp += DIR_SEPARATOR_CHAR; |
579 | | - } |
580 | | - |
581 | | - while ((pos = tmp.find_first_of(DIR_SEPARATOR_CHAR, pre)) != std::string::npos) { |
582 | | - dir = tmp.substr(0, pos++); |
583 | | - pre = pos; |
584 | | - if (dir.empty()) continue; // if leading / first time is 0 length |
585 | | - |
586 | | - _mkdir(dir.c_str()); |
587 | | - } |
| 536 | + return CF_RENAME_FAIL_ACCESS; |
588 | 537 | } |
589 | 538 |
|
590 | 539 | // Creates the directory path if it doesn't exist. Even creates all its |
591 | 540 | // parent paths. |
592 | 541 | void cf_create_directory(int dir_type, uint32_t location_flags) |
593 | 542 | { |
594 | | - int num_dirs = 0; |
595 | | - int dir_tree[CF_MAX_PATH_TYPES]; |
596 | 543 | SCP_string longname; |
597 | | - struct stat statbuf; |
598 | 544 |
|
599 | 545 | Assertion( CF_TYPE_SPECIFIED(dir_type), "Invalid dir_type passed to cf_create_directory." ); |
600 | 546 |
|
601 | | - int current_dir = dir_type; |
| 547 | + cf_create_default_path_string(longname, dir_type, nullptr, location_flags); |
602 | 548 |
|
603 | | - do { |
604 | | - Assert( num_dirs < CF_MAX_PATH_TYPES ); // Invalid Pathtypes data? |
605 | | - |
606 | | - dir_tree[num_dirs++] = current_dir; |
607 | | - current_dir = Pathtypes[current_dir].parent_index; |
608 | | - |
609 | | - } while( current_dir != CF_TYPE_ROOT ); |
610 | | - |
611 | | - int i; |
612 | | - |
613 | | - for (i=num_dirs-1; i>=0; i-- ) { |
614 | | - cf_create_default_path_string(longname, dir_tree[i], nullptr, location_flags); |
615 | | - if (stat(longname.c_str(), &statbuf) != 0) { |
616 | | - mprintf(( "CFILE: Creating new directory '%s'\n", longname.c_str() )); |
617 | | - mkdir_recursive(longname.c_str()); |
618 | | - } |
| 549 | + if (SDL_CreateDirectory(longname.c_str())) { |
| 550 | + mprintf(("CFILE: Creating new directory '%s'\n", longname.c_str())); |
619 | 551 | } |
620 | 552 | } |
621 | 553 |
|
|
0 commit comments