Codechange: use std::string for FiosIsValidFile

This commit is contained in:
Rubidium 2023-05-30 22:35:00 +02:00 committed by rubidium42
parent 1ff0dc11d8
commit 13789d1703
5 changed files with 12 additions and 19 deletions

View File

@ -68,7 +68,7 @@ TarFileList _tar_filelist[NUM_SUBDIRS];
typedef std::map<std::string, std::string> TarLinkList; typedef std::map<std::string, std::string> TarLinkList;
static TarLinkList _tar_linklist[NUM_SUBDIRS]; ///< List of directory links static TarLinkList _tar_linklist[NUM_SUBDIRS]; ///< List of directory links
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); extern bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb);
/** /**
* Checks whether the given search path is a valid search path * Checks whether the given search path is a valid search path

View File

@ -36,7 +36,7 @@ SortingBits _savegame_sort_order = SORT_BY_DATE | SORT_DESCENDING;
/* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */ /* OS-specific functions are taken from their respective files (win32/unix/os2 .c) */
extern bool FiosIsRoot(const char *path); extern bool FiosIsRoot(const char *path);
extern bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb); extern bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb);
extern bool FiosIsHiddenFile(const struct dirent *ent); extern bool FiosIsHiddenFile(const struct dirent *ent);
extern void FiosGetDrives(FileList &file_list); extern void FiosGetDrives(FileList &file_list);
extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot); extern bool FiosGetDiskFreeSpace(const char *path, uint64 *tot);
@ -381,7 +381,7 @@ static void FiosGetFileList(SaveLoadOperation fop, fios_getlist_callback_proc *c
std::string d_name = FS2OTTD(dirent->d_name); std::string d_name = FS2OTTD(dirent->d_name);
/* found file must be directory, but not '.' or '..' */ /* found file must be directory, but not '.' or '..' */
if (FiosIsValidFile(_fios_path->c_str(), dirent, &sb) && S_ISDIR(sb.st_mode) && if (FiosIsValidFile(*_fios_path, dirent, &sb) && S_ISDIR(sb.st_mode) &&
(!FiosIsHiddenFile(dirent) || StrStartsWithIgnoreCase(PERSONAL_DIR, d_name)) && (!FiosIsHiddenFile(dirent) || StrStartsWithIgnoreCase(PERSONAL_DIR, d_name)) &&
d_name != "." && d_name != "..") { d_name != "." && d_name != "..") {
fios = &file_list.emplace_back(); fios = &file_list.emplace_back();

View File

@ -120,12 +120,10 @@ bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
#endif #endif
} }
bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb) bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
{ {
char filename[MAX_PATH]; std::string filename = fmt::format("{}" PATHSEP "{}", path, ent->d_name);
return stat(filename.c_str(), sb) == 0;
snprintf(filename, lengthof(filename), "%s" PATHSEP "%s", path, ent->d_name);
return stat(filename, sb) == 0;
} }
bool FiosIsHiddenFile(const struct dirent *ent) bool FiosIsHiddenFile(const struct dirent *ent)

View File

@ -86,18 +86,13 @@ bool FiosGetDiskFreeSpace(const char *path, uint64 *tot)
return true; return true;
} }
bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb) bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
{ {
char filename[MAX_PATH]; assert(path.back() == PATHSEPCHAR);
int res; if (path.size() > 2) assert(path[path.size() - 2] != PATHSEPCHAR);
assert(path[strlen(path) - 1] == PATHSEPCHAR); std::string filename = fmt::format("{}{}", path, ent->d_name);
if (strlen(path) > 2) assert(path[strlen(path) - 2] != PATHSEPCHAR);
res = seprintf(filename, lastof(filename), "%s%s", path, ent->d_name);
/* Could we fully concatenate the path and filename? */ return stat(filename.c_str(), sb) == 0;
if (res >= (int)lengthof(filename) || res < 0) return false;
return stat(filename, sb) == 0;
} }
bool FiosIsHiddenFile(const struct dirent *ent) bool FiosIsHiddenFile(const struct dirent *ent)

View File

@ -190,7 +190,7 @@ void FiosGetDrives(FileList &file_list)
} }
} }
bool FiosIsValidFile(const char *path, const struct dirent *ent, struct stat *sb) bool FiosIsValidFile(const std::string &path, const struct dirent *ent, struct stat *sb)
{ {
/* hectonanoseconds between Windows and POSIX epoch */ /* hectonanoseconds between Windows and POSIX epoch */
static const int64 posix_epoch_hns = 0x019DB1DED53E8000LL; static const int64 posix_epoch_hns = 0x019DB1DED53E8000LL;