From 5433430767ef46df3e8d039c8561c872c579ab18 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 21 Aug 2023 22:36:25 +0200 Subject: [PATCH] - FString is always gone. --- src/common/filesystem/filesystem.cpp | 33 +++++++++++++----------- src/common/filesystem/fs_findfile.cpp | 36 ++++++++++++++++++++------- src/common/filesystem/fs_findfile.h | 1 + 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/common/filesystem/filesystem.cpp b/src/common/filesystem/filesystem.cpp index 140c41b38..6032a4ccb 100644 --- a/src/common/filesystem/filesystem.cpp +++ b/src/common/filesystem/filesystem.cpp @@ -311,9 +311,11 @@ int FileSystem::AddExternalFile(const char *filename) int FileSystem::AddFromBuffer(const char* name, const char* type, char* data, int size, int id, int flags) { - FStringf fullname("%s.%s", name, type); + std::string fullname = name; + fullname += ':'; + fullname += type; auto newlump = new FMemoryLump(data, size); - newlump->LumpNameSetup(fullname); + newlump->LumpNameSetup(fullname.c_str()); AddLump(newlump); FileInfo.back().resourceId = id; return (int)FileInfo.size()-1; @@ -338,7 +340,7 @@ void FileSystem::AddFile (const char *filename, FileReader *filer, LumpFilterInf if (filer == nullptr) { // Does this exist? If so, is it a directory? - if (!DirEntryExists(filename, &isdir)) + if (!FS_DirEntryExists(filename, &isdir)) { if (Printf) { @@ -396,10 +398,11 @@ void FileSystem::AddFile (const char *filename, FileReader *filer, LumpFilterInf FResourceLump *lump = resfile->GetLump(i); if (lump->Flags & LUMPF_EMBEDDED) { - FString path; - path.Format("%s:%s", filename, lump->getName()); + std::string path = filename; + path += ':'; + path += lump->getName(); auto embedded = lump->NewReader(); - AddFile(path, &embedded, filter, Printf, hashfile); + AddFile(path.c_str(), &embedded, filter, Printf, hashfile); } } @@ -476,8 +479,8 @@ int FileSystem::CheckIfResourceFileLoaded (const char *name) noexcept { for (i = 0; i < Files.Size(); ++i) { - auto pth = ExtractFileBase(GetResourceFileName(i), true); - if (stricmp (pth.GetChars(), name) == 0) + auto pth = ExtractBaseName(GetResourceFileName(i), true); + if (stricmp (pth.c_str(), name) == 0) { return i; } @@ -1255,10 +1258,10 @@ static int folderentrycmp(const void *a, const void *b) unsigned FileSystem::GetFilesInFolder(const char *inpath, TArray &result, bool atomic) const { - FString path = inpath; - FixPathSeperator(path); - path.ToLower(); - if (path[path.Len() - 1] != '/') path += '/'; + std::string path = inpath; + FixPathSeparator(&path.front()); + for (auto& c : path) c = tolower(c); + if (path.back() != '/') path += '/'; result.Clear(); for (size_t i = 0; i < FileInfo.size(); i++) { @@ -1507,11 +1510,11 @@ const char *FileSystem::GetResourceFileFullName (int rfnum) const noexcept bool FileSystem::CreatePathlessCopy(const char *name, int id, int /*flags*/) { - FString name2=name, type2, path; + std::string name2 = name, type2, path; // The old code said 'filename' and ignored the path, this looked like a bug. - FixPathSeperator(name2); - auto lump = FindFile(name2); + FixPathSeparator(&name2.front()); + auto lump = FindFile(name2.c_str()); if (lump < 0) return false; // Does not exist. auto oldlump = FileInfo[lump]; diff --git a/src/common/filesystem/fs_findfile.cpp b/src/common/filesystem/fs_findfile.cpp index 021494644..9dde8fdcb 100644 --- a/src/common/filesystem/fs_findfile.cpp +++ b/src/common/filesystem/fs_findfile.cpp @@ -157,15 +157,6 @@ static int FS_FindClose(void *const handle) return 0; } -static bool DirEntryExists(const char* pathname, bool* isdir) -{ - if (isdir) *isdir = false; - struct stat info; - bool res = stat(pathname, &info) == 0; - if (isdir) *isdir = !!(info.st_mode & S_IFDIR); - return res; -} - static int FS_FindAttr(findstate_t *const fileinfo) { dirent *const ent = fileinfo->namelist[fileinfo->current]; @@ -386,3 +377,30 @@ bool ScanDirectory(std::vector& list, const char* dirpath, const return DoScanDirectory(list, dirpath, match, "", nosubdir, readhidden); } +//========================================================================== +// +// DirEntryExists +// +// Returns true if the given path exists, be it a directory or a file. +// +//========================================================================== + +bool FS_DirEntryExists(const char* pathname, bool* isdir) +{ + if (isdir) *isdir = false; + 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 = toWide(pathname); + struct _stat64 info; + bool res = _wstat64(wstr.c_str(), &info) == 0; +#endif + if (isdir) *isdir = !!(info.st_mode & S_IFDIR); + return res; +} + diff --git a/src/common/filesystem/fs_findfile.h b/src/common/filesystem/fs_findfile.h index 85d328d43..7b6d3e3d2 100644 --- a/src/common/filesystem/fs_findfile.h +++ b/src/common/filesystem/fs_findfile.h @@ -22,6 +22,7 @@ using FileList = std::vector; struct FCompressedBuffer; bool ScanDirectory(std::vector& list, const char* dirpath, const char* match, bool nosubdir = false, bool readhidden = false); bool WriteZip(const char* filename, const FCompressedBuffer* content, size_t contentcount); +bool FS_DirEntryExists(const char* pathname, bool* isdir); inline void FixPathSeparator(char* path) {