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