Changed some more FileSystemUtil functions to use std::filesystem facilities

This commit is contained in:
Leon Styhre 2023-12-14 00:25:08 +01:00
parent 79e12e0898
commit 932cb60027
2 changed files with 92 additions and 14 deletions

View file

@ -100,6 +100,26 @@ namespace Utils
return contentList; return contentList;
} }
FileList getDirContentSTD(const std::filesystem::path& path, const bool recursive)
{
FileList fileList;
if (!isDirectorySTD(path))
return fileList;
if (recursive) {
for (auto& entry : std::filesystem::recursive_directory_iterator(path))
fileList.emplace_back(entry);
}
else {
for (auto& entry : std::filesystem::directory_iterator(path))
fileList.emplace_back(entry);
}
fileList.sort();
return fileList;
}
StringList getMatchingFiles(const std::string& pattern) StringList getMatchingFiles(const std::string& pattern)
{ {
StringList files; StringList files;
@ -235,10 +255,10 @@ namespace Utils
envHomeDrive = _wgetenv(L"HOMEDRIVE"); envHomeDrive = _wgetenv(L"HOMEDRIVE");
envHomePath = _wgetenv(L"HOMEPATH"); envHomePath = _wgetenv(L"HOMEPATH");
#endif #endif
if (envHomeDrive.length() && envHomePath.length()) if (envHomeDrive.length() && envHomePath.length()) {
homePathSTD = std::filesystem::path { homePathSTD = envHomeDrive;
getGenericPath(Utils::String::wideStringToString(envHomeDrive) + "/" + homePathSTD.append(envHomePath);
Utils::String::wideStringToString(envHomePath))}; }
#else #else
@ -359,7 +379,6 @@ namespace Utils
void setExePath(const std::string& path) void setExePath(const std::string& path)
{ {
std::string exePathTemp; std::string exePathTemp;
std::string esBinaryTemp;
constexpr int pathMax {32767}; constexpr int pathMax {32767};
#if defined(_WIN64) #if defined(_WIN64)
@ -373,19 +392,18 @@ namespace Utils
#endif #endif
exePathTemp.erase(std::find(exePathTemp.begin(), exePathTemp.end(), '\0'), exePathTemp.erase(std::find(exePathTemp.begin(), exePathTemp.end(), '\0'),
exePathTemp.end()); exePathTemp.end());
esBinaryTemp = exePathTemp; esBinary = exePathTemp;
exePathTemp = getCanonicalPath(exePathTemp); exePath = exePathTemp;
exePath = getCanonicalPathSTD(exePath);
// Fallback to argv[0] if everything else fails. // Fallback to argv[0] if everything else fails.
if (exePathTemp.empty()) { if (exePath.empty()) {
esBinaryTemp = path; esBinary = path;
exePathTemp = getCanonicalPath(path); exePath = getCanonicalPathSTD(esBinary);
} }
if (isRegularFile(exePathTemp))
exePathTemp = getParent(exePathTemp);
exePath = std::filesystem::path {exePathTemp}; if (isRegularFileSTD(exePath))
esBinary = std::filesystem::path {esBinaryTemp}; exePath = exePath.parent_path();
#if defined(APPIMAGE_BUILD) #if defined(APPIMAGE_BUILD)
// We need to check that the APPIMAGE variable is available as the APPIMAGE_BUILD // We need to check that the APPIMAGE variable is available as the APPIMAGE_BUILD
@ -504,6 +522,9 @@ namespace Utils
std::string getCanonicalPath(const std::string& path) std::string getCanonicalPath(const std::string& path)
{ {
if (path.empty())
return "";
// Hack for builtin resources. // Hack for builtin resources.
if ((path[0] == ':') && (path[1] == '/')) if ((path[0] == ':') && (path[1] == '/'))
return path; return path;
@ -564,6 +585,18 @@ namespace Utils
return canonicalPath; return canonicalPath;
} }
std::filesystem::path getCanonicalPathSTD(const std::filesystem::path& path)
{
if (path.empty())
return path;
// Hack for builtin resources.
if ((path.string()[0] == ':') && (path.string()[1] == '/'))
return path;
return std::filesystem::canonical(path);
}
std::string getAbsolutePath(const std::string& path, const std::string& base) std::string getAbsolutePath(const std::string& path, const std::string& base)
{ {
const std::string& absolutePath {getGenericPath(path)}; const std::string& absolutePath {getGenericPath(path)};
@ -601,6 +634,11 @@ namespace Utils
return genericPath; return genericPath;
} }
std::filesystem::path getFileNameSTD(const std::filesystem::path& path)
{
return path.filename();
}
std::string getStem(const std::string& path) std::string getStem(const std::string& path)
{ {
std::string fileName {getFileName(path)}; std::string fileName {getFileName(path)};
@ -1018,6 +1056,17 @@ namespace Utils
} }
} }
bool isRegularFileSTD(const std::filesystem::path& path)
{
try {
return std::filesystem::is_regular_file(path);
}
catch (std::filesystem::filesystem_error& error) {
LOG(LogError) << "FileSystemUtil::isRegularFile(): " << error.what();
return false;
}
}
bool isDirectory(const std::string& path) bool isDirectory(const std::string& path)
{ {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
@ -1035,6 +1084,17 @@ namespace Utils
} }
} }
bool isDirectorySTD(const std::filesystem::path& path)
{
try {
return std::filesystem::is_directory(path);
}
catch (std::filesystem::filesystem_error& error) {
LOG(LogError) << "FileSystemUtil::isDirectory(): " << error.what();
return false;
}
}
bool isSymlink(const std::string& path) bool isSymlink(const std::string& path)
{ {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};
@ -1051,6 +1111,17 @@ namespace Utils
} }
} }
bool isSymlinkSTD(const std::filesystem::path& path)
{
try {
return std::filesystem::is_symlink(path);
}
catch (std::filesystem::filesystem_error& error) {
LOG(LogError) << "FileSystemUtil::isSymlink(): " << error.what();
return false;
}
}
bool isHidden(const std::string& path) bool isHidden(const std::string& path)
{ {
const std::string& genericPath {getGenericPath(path)}; const std::string& genericPath {getGenericPath(path)};

View file

@ -20,8 +20,10 @@ namespace Utils
namespace FileSystem namespace FileSystem
{ {
using StringList = std::list<std::string>; using StringList = std::list<std::string>;
using FileList = std::list<std::filesystem::path>;
StringList getDirContent(const std::string& path, const bool recursive = false); StringList getDirContent(const std::string& path, const bool recursive = false);
FileList getDirContentSTD(const std::filesystem::path& path, const bool recursive = false);
StringList getMatchingFiles(const std::string& pattern); StringList getMatchingFiles(const std::string& pattern);
StringList getPathList(const std::string& path); StringList getPathList(const std::string& path);
void setHomePath(const std::string& path); void setHomePath(const std::string& path);
@ -40,11 +42,13 @@ namespace Utils
std::string getGenericPath(const std::string& path); std::string getGenericPath(const std::string& path);
std::string getEscapedPath(const std::string& path); std::string getEscapedPath(const std::string& path);
std::string getCanonicalPath(const std::string& path); std::string getCanonicalPath(const std::string& path);
std::filesystem::path getCanonicalPathSTD(const std::filesystem::path& path);
std::string getAbsolutePath( std::string getAbsolutePath(
const std::string& path, const std::string& path,
const std::string& base = std::filesystem::current_path().string()); const std::string& base = std::filesystem::current_path().string());
std::string getParent(const std::string& path); std::string getParent(const std::string& path);
std::string getFileName(const std::string& path); std::string getFileName(const std::string& path);
std::filesystem::path getFileNameSTD(const std::filesystem::path& path);
std::string getStem(const std::string& path); std::string getStem(const std::string& path);
std::string getExtension(const std::string& path); std::string getExtension(const std::string& path);
long getFileSize(const std::filesystem::path& path); long getFileSize(const std::filesystem::path& path);
@ -74,8 +78,11 @@ namespace Utils
bool driveExists(const std::string& path); bool driveExists(const std::string& path);
bool isAbsolute(const std::string& path); bool isAbsolute(const std::string& path);
bool isRegularFile(const std::string& path); bool isRegularFile(const std::string& path);
bool isRegularFileSTD(const std::filesystem::path& path);
bool isDirectory(const std::string& path); bool isDirectory(const std::string& path);
bool isDirectorySTD(const std::filesystem::path& path);
bool isSymlink(const std::string& path); bool isSymlink(const std::string& path);
bool isSymlinkSTD(const std::filesystem::path& path);
bool isHidden(const std::string& path); bool isHidden(const std::string& path);
} // namespace FileSystem } // namespace FileSystem