1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-09-05 18:41:05 +00:00

Apply some refactoring/cleanup to mainly util functions

This commit is contained in:
sfan5 2025-03-26 19:08:31 +01:00
parent 89e3bc8d56
commit e73eed247e
19 changed files with 190 additions and 160 deletions

View file

@ -136,20 +136,23 @@ bool IsDir(const std::string &path)
(attr & FILE_ATTRIBUTE_DIRECTORY));
}
bool IsFile(const std::string &path)
{
DWORD attr = GetFileAttributes(path.c_str());
return (attr != INVALID_FILE_ATTRIBUTES &&
!(attr & FILE_ATTRIBUTE_DIRECTORY));
}
bool IsExecutable(const std::string &path)
{
DWORD type;
return GetBinaryType(path.c_str(), &type) != 0;
}
bool IsDirDelimiter(char c)
{
return c == '/' || c == '\\';
}
bool RecursiveDelete(const std::string &path)
{
infostream << "Recursively deleting \"" << path << "\"" << std::endl;
assert(IsPathAbsolute(path));
if (!IsDir(path)) {
infostream << "RecursiveDelete: Deleting file " << path << std::endl;
if (!DeleteFile(path.c_str())) {
@ -181,19 +184,9 @@ bool RecursiveDelete(const std::string &path)
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
{
DWORD attr = GetFileAttributes(path.c_str());
bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
(attr & FILE_ATTRIBUTE_DIRECTORY));
if(!is_directory)
{
bool did = DeleteFile(path.c_str());
return did;
}
else
{
bool did = RemoveDirectory(path.c_str());
return did;
}
if (!IsDir(path))
return DeleteFile(path.c_str());
return RemoveDirectory(path.c_str());
}
std::string TempPath()
@ -336,8 +329,7 @@ bool CreateDir(const std::string &path)
bool PathExists(const std::string &path)
{
struct stat st{};
return (stat(path.c_str(),&st) == 0);
return access(path.c_str(), F_OK) == 0;
}
bool IsPathAbsolute(const std::string &path)
@ -348,21 +340,29 @@ bool IsPathAbsolute(const std::string &path)
bool IsDir(const std::string &path)
{
struct stat statbuf{};
if(stat(path.c_str(), &statbuf))
if (stat(path.c_str(), &statbuf))
return false; // Actually error; but certainly not a directory
return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
}
bool IsFile(const std::string &path)
{
struct stat statbuf{};
if (stat(path.c_str(), &statbuf))
return false;
#ifdef S_IFSOCK
// sockets cannot be opened in any way, so they are not files.
if ((statbuf.st_mode & S_IFSOCK) == S_IFSOCK)
return false;
#endif
return ((statbuf.st_mode & S_IFDIR) != S_IFDIR);
}
bool IsExecutable(const std::string &path)
{
return access(path.c_str(), X_OK) == 0;
}
bool IsDirDelimiter(char c)
{
return c == '/';
}
bool RecursiveDelete(const std::string &path)
{
/*
@ -877,7 +877,7 @@ const char *GetFilenameFromPath(const char *path)
{
const char *filename = strrchr(path, DIR_DELIM_CHAR);
// Consistent with IsDirDelimiter this function handles '/' too
if (DIR_DELIM_CHAR != '/') {
if constexpr (DIR_DELIM_CHAR != '/') {
const char *tmp = strrchr(path, '/');
if (tmp && tmp > filename)
filename = tmp;