Added a getFileSize() function to FileSystemUtil

Also modernized createEmptyFile() to use std::filesystem::path
This commit is contained in:
Leon Styhre 2023-08-02 19:20:50 +02:00
parent d0346d8c28
commit 5705672957
2 changed files with 28 additions and 21 deletions

View file

@ -23,7 +23,6 @@
#include "utils/PlatformUtil.h"
#include "utils/StringUtil.h"
#include <filesystem>
#include <fstream>
#include <regex>
#include <string>
@ -569,6 +568,21 @@ namespace Utils
return ".";
}
long getFileSize(const std::filesystem::path& path)
{
try {
#if defined(_WIN64)
return static_cast<long>(std::filesystem::file_size(
Utils::String::stringToWideString(path.generic_string())));
#else
return static_cast<long>(std::filesystem::file_size(path));
#endif
}
catch (...) {
return -1;
}
}
std::string expandHomePath(const std::string& path)
{
// Expand home path if ~ is used.
@ -758,31 +772,22 @@ namespace Utils
#endif
}
bool createEmptyFile(const std::string& path)
bool createEmptyFile(const std::filesystem::path& path)
{
const std::filesystem::path cleanPath {path.lexically_normal().make_preferred()};
if (exists(path)) {
LOG(LogError) << "Couldn't create target file \"" << cleanPath.string()
<< "\" as it already exists";
return false;
}
#if defined(_WIN64)
LOG(LogError) << "Couldn't create target file \""
<< Utils::String::replace(path, "/", "\\")
<< "\" as it already exists";
return false;
}
std::ofstream targetFile {Utils::String::stringToWideString(path).c_str(),
std::ofstream targetFile {Utils::String::stringToWideString(cleanPath.string()).c_str(),
std::ios::binary};
if (targetFile.fail()) {
LOG(LogError) << "Couldn't create target file \""
<< Utils::String::replace(path, "/", "\\")
#else
LOG(LogError) << "Couldn't create target file \"" << path
<< "\" as it already exists";
return false;
}
std::ofstream targetFile {path, std::ios::binary};
if (targetFile.fail()) {
LOG(LogError) << "Couldn't create target file \"" << path
std::ofstream targetFile {cleanPath, std::ios::binary};
#endif
if (targetFile.fail()) {
LOG(LogError) << "Couldn't create target file \"" << cleanPath.string()
<< "\", permission problems?";
targetFile.close();
return false;

View file

@ -11,6 +11,7 @@
#ifndef ES_CORE_UTILS_FILE_SYSTEM_UTIL_H
#define ES_CORE_UTILS_FILE_SYSTEM_UTIL_H
#include <filesystem>
#include <list>
#include <string>
@ -42,6 +43,7 @@ namespace Utils
std::string getFileName(const std::string& path);
std::string getStem(const std::string& path);
std::string getExtension(const std::string& path);
long getFileSize(const std::filesystem::path& path);
std::string expandHomePath(const std::string& path);
std::string resolveRelativePath(const std::string& path,
const std::string& relativeTo,
@ -59,7 +61,7 @@ namespace Utils
bool renameFile(const std::string& sourcePath,
const std::string& destinationPath,
bool overwrite);
bool createEmptyFile(const std::string& path);
bool createEmptyFile(const std::filesystem::path& path);
bool removeFile(const std::string& path);
bool removeDirectory(const std::string& path);
bool createDirectory(const std::string& path);