mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2025-06-11 07:18:11 -05:00
Replace FileAPI with c++ streams and std::filesystem
This commit is contained in:
@ -10,13 +10,15 @@
|
||||
#define _CPP_VERSION 0
|
||||
#endif
|
||||
|
||||
#ifndef _NODISCARD
|
||||
#ifdef _NODISCARD
|
||||
#undef _NODISCARD
|
||||
#endif
|
||||
|
||||
#if _CPP_VERSION >= 201703L
|
||||
#define _NODISCARD [[nodiscard]]
|
||||
#else
|
||||
#define _NODISCARD
|
||||
#endif
|
||||
#endif
|
||||
|
||||
template <class T>
|
||||
struct Movable
|
||||
|
@ -1,145 +0,0 @@
|
||||
#include "FileAPI.h"
|
||||
|
||||
#include <cstdarg>
|
||||
#include <cstdio>
|
||||
#include <filesystem>
|
||||
|
||||
bool FileAPI::FileExists(const std::string& fileName)
|
||||
{
|
||||
return std::filesystem::exists(fileName);
|
||||
}
|
||||
|
||||
uint64_t FileAPI::FileSize(const std::string& fileName)
|
||||
{
|
||||
return std::filesystem::file_size(fileName);
|
||||
}
|
||||
|
||||
bool FileAPI::DirectoryCreate(const std::string& directoryPath)
|
||||
{
|
||||
return std::filesystem::create_directories(directoryPath);
|
||||
}
|
||||
|
||||
bool FileAPI::DirectoryExists(const std::string& directoryName)
|
||||
{
|
||||
return std::filesystem::is_directory(directoryName);
|
||||
}
|
||||
|
||||
FileAPI::File FileAPI::Open(const std::string& filename, const Mode mode)
|
||||
{
|
||||
const char* modeStr;
|
||||
switch (mode)
|
||||
{
|
||||
default:
|
||||
case Mode::MODE_READ:
|
||||
modeStr = "rb";
|
||||
break;
|
||||
|
||||
case Mode::MODE_WRITE:
|
||||
modeStr = "wb";
|
||||
break;
|
||||
}
|
||||
|
||||
FILE* handle;
|
||||
if (fopen_s(&handle, filename.c_str(), modeStr) != 0)
|
||||
{
|
||||
return File(nullptr);
|
||||
}
|
||||
|
||||
return File(handle);
|
||||
}
|
||||
|
||||
FileAPI::File::File()
|
||||
{
|
||||
this->m_handle = nullptr;
|
||||
}
|
||||
|
||||
FileAPI::File::File(void* handle)
|
||||
{
|
||||
this->m_handle = handle;
|
||||
}
|
||||
|
||||
FileAPI::File::File(File&& f) noexcept
|
||||
{
|
||||
m_handle = f.m_handle;
|
||||
f.m_handle = nullptr;
|
||||
}
|
||||
|
||||
FileAPI::File::~File()
|
||||
{
|
||||
this->Close();
|
||||
}
|
||||
|
||||
FileAPI::File& FileAPI::File::operator=(File&& f) noexcept
|
||||
{
|
||||
m_handle = f.m_handle;
|
||||
f.m_handle = nullptr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool FileAPI::File::IsOpen()
|
||||
{
|
||||
return this->m_handle != nullptr;
|
||||
}
|
||||
|
||||
size_t FileAPI::File::Read(void* buffer, const size_t elementSize, const size_t elementCount)
|
||||
{
|
||||
if (!this->IsOpen())
|
||||
return 0;
|
||||
|
||||
return fread(buffer, elementSize, elementCount, static_cast<FILE*>(m_handle));
|
||||
}
|
||||
|
||||
size_t FileAPI::File::Write(const void* data, const size_t elementSize, const size_t elementCount)
|
||||
{
|
||||
if (!this->IsOpen())
|
||||
return 0;
|
||||
|
||||
return fwrite(data, elementSize, elementCount, static_cast<FILE*>(m_handle));
|
||||
}
|
||||
|
||||
void FileAPI::File::Skip(const int64_t amount)
|
||||
{
|
||||
if (!this->IsOpen())
|
||||
return;
|
||||
|
||||
_fseeki64(static_cast<FILE*>(m_handle), amount, SEEK_CUR);
|
||||
}
|
||||
|
||||
void FileAPI::File::GotoEnd()
|
||||
{
|
||||
_fseeki64(static_cast<FILE*>(m_handle), 0, SEEK_END);
|
||||
}
|
||||
|
||||
size_t FileAPI::File::Printf(const char* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (!this->IsOpen())
|
||||
return 0;
|
||||
|
||||
va_start(ap, fmt);
|
||||
const int result = vfprintf(static_cast<FILE*>(m_handle), fmt, ap);
|
||||
va_end(ap);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int64_t FileAPI::File::Pos()
|
||||
{
|
||||
return _ftelli64(static_cast<FILE*>(m_handle));
|
||||
}
|
||||
|
||||
void FileAPI::File::Goto(const int64_t pos)
|
||||
{
|
||||
_fseeki64(static_cast<FILE*>(m_handle), pos, SEEK_SET);
|
||||
}
|
||||
|
||||
void FileAPI::File::Close()
|
||||
{
|
||||
if (this->m_handle != nullptr)
|
||||
{
|
||||
fclose(static_cast<FILE*>(m_handle));
|
||||
this->m_handle = nullptr;
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
|
||||
class FileAPI
|
||||
{
|
||||
public:
|
||||
enum class Mode
|
||||
{
|
||||
MODE_READ = 0,
|
||||
MODE_WRITE = 1
|
||||
};
|
||||
|
||||
class IFile
|
||||
{
|
||||
public:
|
||||
virtual ~IFile() = default;
|
||||
|
||||
virtual bool IsOpen() = 0;
|
||||
virtual size_t Read(void* buffer, size_t elementSize, size_t elementCount) = 0;
|
||||
virtual size_t Write(const void* data, size_t elementSize, size_t elementCount) = 0;
|
||||
virtual void Skip(int64_t amount) = 0;
|
||||
virtual size_t Printf(const char* fmt, ...) = 0;
|
||||
virtual int64_t Pos() = 0;
|
||||
virtual void Goto(int64_t pos) = 0;
|
||||
virtual void GotoEnd() = 0;
|
||||
virtual void Close() = 0;
|
||||
};
|
||||
|
||||
class File final : public IFile
|
||||
{
|
||||
void* m_handle;
|
||||
|
||||
public:
|
||||
File();
|
||||
explicit File(void* handle);
|
||||
File(File&) = delete;
|
||||
File(File&& f) noexcept;
|
||||
~File() override;
|
||||
|
||||
File& operator=(File&) = delete;
|
||||
File& operator=(File&& f) noexcept;
|
||||
|
||||
bool IsOpen() override;
|
||||
size_t Read(void* buffer, size_t elementSize, size_t elementCount) override;
|
||||
size_t Write(const void* data, size_t elementSize, size_t elementCount) override;
|
||||
void Skip(int64_t amount) override;
|
||||
size_t Printf(const char* fmt, ...) override;
|
||||
int64_t Pos() override;
|
||||
void Goto(int64_t pos) override;
|
||||
void GotoEnd() override;
|
||||
void Close() override;
|
||||
};
|
||||
|
||||
static bool FileExists(const std::string& fileName);
|
||||
static uint64_t FileSize(const std::string& fileName);
|
||||
|
||||
static bool DirectoryCreate(const std::string& directoryPath);
|
||||
static bool DirectoryExists(const std::string& directoryName);
|
||||
|
||||
static File Open(const std::string& filename, Mode mode);
|
||||
};
|
@ -1,41 +0,0 @@
|
||||
#include "PathUtils.h"
|
||||
|
||||
#include <filesystem>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
std::string Path::GetFilename(const std::string& pathInput)
|
||||
{
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.filename().string();
|
||||
}
|
||||
|
||||
std::string Path::GetFilenameWithoutExtension(const std::string& pathInput)
|
||||
{
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.filename().replace_extension().string();
|
||||
}
|
||||
|
||||
std::string Path::GetExtension(const std::string& pathInput)
|
||||
{
|
||||
const std::filesystem::path path(pathInput);
|
||||
|
||||
return path.extension().string();
|
||||
}
|
||||
|
||||
std::string Path::GetDirectory(const std::string& pathInput)
|
||||
{
|
||||
std::filesystem::path path(pathInput);
|
||||
|
||||
return path.remove_filename().string();
|
||||
}
|
||||
|
||||
std::string Path::Combine(const std::string& p1, const std::string& p2)
|
||||
{
|
||||
std::filesystem::path path(p1);
|
||||
|
||||
return path.append(p2).string();
|
||||
}
|
||||
}
|
@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace utils
|
||||
{
|
||||
class Path
|
||||
{
|
||||
public:
|
||||
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);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user