Skip to content

Commit cc37fb9

Browse files
clean up cfile
- replace a lot of platform specific code with SDL filesystem functions - remove support for memory-mapped files (not actually used) - only do special case-sensitive filesystem handling if required - support case-sensitive roots on Windows
1 parent 5e1b94c commit cc37fb9

File tree

2 files changed

+142
-211
lines changed

2 files changed

+142
-211
lines changed

code/cfile/cfile.cpp

Lines changed: 29 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222
#include <windows.h>
2323
#endif
2424

25-
#ifdef SCP_UNIX
26-
#include <glob.h>
27-
#endif
28-
2925
#include "cfile/cfile.h"
3026
#include "cfile/cfilearchive.h"
3127
#include "cfile/cfilesystem.h"
@@ -369,56 +365,41 @@ int cfile_pop_dir()
369365
int cfile_flush_dir(int dir_type)
370366
{
371367
int del_count;
368+
SDL_PathInfo pinfo;
369+
SCP_string filespec;
370+
SCP_string fullpath;
372371

373372
Assert( CF_TYPE_SPECIFIED(dir_type) );
374373

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);
379375

380376
// proceed to delete the files
381377
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);
391378

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;
394392
}
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);
411393

394+
// delete the file
395+
if ( SDL_RemovePath(fullpath.c_str()) ) {
412396
// increment the deleted count
413-
del_count++;
397+
++del_count;
414398
}
415399
}
416-
globfree(&globinfo);
417-
}
418-
#endif
419400

420-
// pop the directory back
421-
cfile_pop_dir();
401+
SDL_free(results);
402+
}
422403

423404
// return the # of files deleted
424405
return del_count;
@@ -463,7 +444,7 @@ int cf_delete(const char *filename, int path_type, uint32_t location_flags)
463444

464445
cf_create_default_path_string(longname, path_type, filename, location_flags);
465446

466-
return (_unlink(longname.c_str()) != -1);
447+
return SDL_RemovePath(longname.c_str());
467448
}
468449

469450

@@ -542,80 +523,31 @@ int cf_rename(const char *old_name, const char *name, int dir_type)
542523
{
543524
Assert( CF_TYPE_SPECIFIED(dir_type) );
544525

545-
int ret_code;
546526
SCP_string old_longname;
547527
SCP_string new_longname;
548528

549529
cf_create_default_path_string(old_longname, dir_type, old_name);
550530
cf_create_default_path_string(new_longname, dir_type, name);
551531

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;
561534
}
562535

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;
588537
}
589538

590539
// Creates the directory path if it doesn't exist. Even creates all its
591540
// parent paths.
592541
void cf_create_directory(int dir_type, uint32_t location_flags)
593542
{
594-
int num_dirs = 0;
595-
int dir_tree[CF_MAX_PATH_TYPES];
596543
SCP_string longname;
597-
struct stat statbuf;
598544

599545
Assertion( CF_TYPE_SPECIFIED(dir_type), "Invalid dir_type passed to cf_create_directory." );
600546

601-
int current_dir = dir_type;
547+
cf_create_default_path_string(longname, dir_type, nullptr, location_flags);
602548

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()));
619551
}
620552
}
621553

0 commit comments

Comments
 (0)