mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-04-20 08:05:45 +00:00
Utils: Change FileAPI and PathUtils to make use std::filesystem
This commit is contained in:
parent
4a616b6c24
commit
d176e137a5
@ -2,31 +2,26 @@
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
|
||||
bool FileAPI::FileExists(const std::string& fileName)
|
||||
{
|
||||
struct stat st{};
|
||||
|
||||
return stat(fileName.c_str(), &st) >= 0 && !(st.st_mode & S_IFDIR);
|
||||
return std::filesystem::exists(fileName);
|
||||
}
|
||||
|
||||
uint64_t FileAPI::FileSize(const std::string& fileName)
|
||||
{
|
||||
struct stat st{};
|
||||
return std::filesystem::file_size(fileName);
|
||||
}
|
||||
|
||||
if (stat(fileName.c_str(), &st) >= 0 && !(st.st_mode & S_IFDIR))
|
||||
{
|
||||
return st.st_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
bool FileAPI::CreateDirectory(const std::string& directoryPath)
|
||||
{
|
||||
return std::filesystem::create_directories(directoryPath);
|
||||
}
|
||||
|
||||
bool FileAPI::DirectoryExists(const std::string& directoryName)
|
||||
{
|
||||
struct stat st{};
|
||||
|
||||
return stat(directoryName.c_str(), &st) >= 0 && st.st_mode & S_IFDIR;
|
||||
return std::filesystem::is_directory(directoryName);
|
||||
}
|
||||
|
||||
FileAPI::File FileAPI::Open(const std::string& filename, const Mode mode)
|
||||
|
@ -38,6 +38,9 @@ public:
|
||||
|
||||
static bool FileExists(const std::string& fileName);
|
||||
static uint64_t FileSize(const std::string& fileName);
|
||||
|
||||
static bool CreateDirectory(const std::string& directoryPath);
|
||||
static bool DirectoryExists(const std::string& directoryName);
|
||||
|
||||
static File Open(const std::string& filename, Mode mode);
|
||||
};
|
@ -1,79 +1,41 @@
|
||||
#include "PathUtils.h"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
std::string Path::GetFilename(std::string path)
|
||||
std::string Path::GetFilename(const std::string& pathInput)
|
||||
{
|
||||
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||
if (std::string::npos != lastSlashIndex)
|
||||
{
|
||||
path.erase(0, lastSlashIndex);
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.filename().string();
|
||||
}
|
||||
|
||||
return path;
|
||||
std::string Path::GetFilenameWithoutExtension(const std::string& pathInput)
|
||||
{
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.filename().replace_extension().string();
|
||||
}
|
||||
|
||||
std::string Path::GetFilenameWithoutExtension(std::string path)
|
||||
std::string Path::GetExtension(const std::string& pathInput)
|
||||
{
|
||||
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||
if (std::string::npos != lastSlashIndex)
|
||||
{
|
||||
path.erase(0, lastSlashIndex + 1);
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.extension().string();
|
||||
}
|
||||
|
||||
// Remove extension if present.
|
||||
const size_t dotIndex = path.rfind('.');
|
||||
if (std::string::npos != dotIndex)
|
||||
std::string Path::GetDirectory(const std::string& pathInput)
|
||||
{
|
||||
path.erase(dotIndex);
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.relative_path().string();
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
std::string Path::GetExtension(std::string path)
|
||||
std::string Path::Combine(const std::string& p1, const std::string& p2)
|
||||
{
|
||||
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||
const size_t lastDotIndex = path.find_last_of('.');
|
||||
if (std::string::npos != lastDotIndex
|
||||
&& (lastSlashIndex == std::string::npos || lastDotIndex > lastSlashIndex))
|
||||
{
|
||||
path.erase(0, lastDotIndex);
|
||||
std::filesystem::path path(p1);
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
std::string Path::GetDirectory(std::string path)
|
||||
{
|
||||
const size_t lastSlashIndex = path.find_last_of("\\/");
|
||||
if (std::string::npos != lastSlashIndex)
|
||||
{
|
||||
path.erase(lastSlashIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
return "./";
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
std::string Path::Combine(std::string p1, std::string p2)
|
||||
{
|
||||
char c;
|
||||
|
||||
while (!p1.empty() && (c = p1[p1.size() - 1], c == '\\' || c == '/'))
|
||||
p1.erase(p1.size() - 1);
|
||||
|
||||
while (!p2.empty() && (c = p2[0], c == '\\' || c == '/'))
|
||||
p2.erase(0);
|
||||
|
||||
if (!p1.empty())
|
||||
p1 += '/';
|
||||
|
||||
return p1 + p2;
|
||||
return path.append(p2).string();
|
||||
}
|
||||
}
|
||||
|
@ -7,10 +7,10 @@ namespace utils
|
||||
class Path
|
||||
{
|
||||
public:
|
||||
static std::string GetFilename(std::string path);
|
||||
static std::string GetFilenameWithoutExtension(std::string path);
|
||||
static std::string GetExtension(std::string path);
|
||||
static std::string GetDirectory(std::string path);
|
||||
static std::string Combine(std::string p1, std::string p2);
|
||||
static std::string GetFilename(const std::string& pathInput);
|
||||
static std::string GetFilenameWithoutExtension(const std::string& pathInput);
|
||||
static std::string GetExtension(const std::string& pathInput);
|
||||
static std::string GetDirectory(const std::string& pathInput);
|
||||
static std::string Combine(const std::string& p1, const std::string& p2);
|
||||
};
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user