From 9f0b720b2559184974d9167654e52f6058fd3178 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Fri, 3 Jan 2020 17:34:52 +0100 Subject: [PATCH] -second function freed of std::filesystem. --- source/common/gameconfigfile.cpp | 1 + source/common/searchpaths.cpp | 41 +++++++++++++++++++------------- source/common/utility/cmdlib.cpp | 32 +++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/source/common/gameconfigfile.cpp b/source/common/gameconfigfile.cpp index 64bb2b221..8ae39bf06 100644 --- a/source/common/gameconfigfile.cpp +++ b/source/common/gameconfigfile.cpp @@ -522,6 +522,7 @@ void G_ReadConfig(const char* game) void G_SaveConfig() { + if (!GameConfig) return; GameConfig->ArchiveGlobalData(); GameConfig->ArchiveGameData(GameName); GameConfig->WriteConfigFile(); diff --git a/source/common/searchpaths.cpp b/source/common/searchpaths.cpp index 066265668..25285598e 100644 --- a/source/common/searchpaths.cpp +++ b/source/common/searchpaths.cpp @@ -463,10 +463,14 @@ void CollectSubdirectories(TArray &searchpath, const char *dirmatch) { do { - if (!(I_FindAttr(&findstate) & FA_DIREC)) + if (I_FindAttr(&findstate) & FA_DIREC) { - FStringf fullpath("%s/%s", AbsPath.GetChars(), I_FindName(&findstate)); - searchpath.Push(fullpath); + auto p = I_FindName(&findstate); + if (strcmp(p, ".") && strcmp(p, "..")) + { + FStringf fullpath("%s/%s", AbsPath.GetChars(), p); + searchpath.Push(fullpath); + } } } while (I_FindNext(handle, &findstate) == 0); I_FindClose(handle); @@ -538,8 +542,8 @@ TArray CollectSearchPaths() struct FileEntry { FString FileName; - uintmax_t FileLength; - uint64_t FileTime; + size_t FileLength; + time_t FileTime; uint32_t CRCValue; uint32_t Index; }; @@ -563,20 +567,25 @@ TArray CollectAllFilesInSearchPath() auto paths = CollectSearchPaths(); for(auto &path : paths) { - auto fpath = fs::u8path(path.GetChars()); - if (fs::exists(fpath) && fs::is_directory(fpath)) + if (DirExists(path)) { - for (const auto& entry : fs::directory_iterator(fpath)) + findstate_t findstate; + void* handle; + if ((handle = I_FindFirst(path + "/*.*", &findstate)) != (void*)-1) { - if (fs::is_regular_file(entry.status())) + do { - filelist.Reserve(1); - auto& flentry = filelist.Last(); - flentry.FileName = absolute(entry.path()).u8string().c_str(); - flentry.FileLength = entry.file_size(); - flentry.FileTime = entry.last_write_time().time_since_epoch().count(); - flentry.Index = index++; // to preserve order when working on the list. - } + if (!(I_FindAttr(&findstate) & FA_DIREC)) + { + auto p = I_FindName(&findstate); + filelist.Reserve(1); + auto& flentry = filelist.Last(); + flentry.FileName.Format("%s/%s", path.GetChars(), p); + GetFileInfo(flentry.FileName, &flentry.FileLength, &flentry.FileTime); + flentry.Index = index++; // to preserve order when working on the list. + } + } while (I_FindNext(handle, &findstate) == 0); + I_FindClose(handle); } } } diff --git a/source/common/utility/cmdlib.cpp b/source/common/utility/cmdlib.cpp index a750ec8f4..5ee885db5 100644 --- a/source/common/utility/cmdlib.cpp +++ b/source/common/utility/cmdlib.cpp @@ -125,13 +125,41 @@ bool DirEntryExists(const char *pathname, bool *isdir) #else // Windows must use the wide version of stat to preserve non-standard paths. auto wstr = WideString(pathname); - struct _stat64i32 info; - bool res = _wstat64i32(wstr.c_str(), &info) == 0; + struct _stat64 info; + bool res = _wstat64(wstr.c_str(), &info) == 0; #endif if (isdir) *isdir = !!(info.st_mode & S_IFDIR); return res; } +//========================================================================== +// +// DirEntryExists +// +// Returns true if the given path exists, be it a directory or a file. +// +//========================================================================== + +bool GetFileInfo(const char* pathname, size_t *size, time_t *time) +{ + if (pathname == NULL || *pathname == 0) + return false; + +#ifndef _WIN32 + struct stat info; + bool res = stat(pathname, &info) == 0; +#else + // Windows must use the wide version of stat to preserve non-standard paths. + auto wstr = WideString(pathname); + struct _stat64 info; + bool res = _wstat64(wstr.c_str(), &info) == 0; +#endif + if (!res || (info.st_mode & S_IFDIR)) return false; + if (size) *size = info.st_size; + if (time) *time = info.st_mtime; + return res; +} + //========================================================================== // // DefaultExtension -- FString version