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

View file

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