feat(FileManager): Added FileManager for easier file management

This commit is contained in:
Yimura 2022-02-21 18:03:44 +01:00
parent e1632c329c
commit 13cdff3be5
5 changed files with 203 additions and 0 deletions

View File

@ -0,0 +1,71 @@
#pragma once
#include "common.hpp"
#include "file_manager/file.hpp"
#include "file_manager/folder.hpp"
namespace big
{
class file_manager;
inline file_manager* g_file_manager{};
class file_manager final
{
public:
file_manager(std::filesystem::path base_dir)
: m_base_dir(base_dir)
{
file_manager::ensure_folder_exists(m_base_dir);
g_file_manager = this;
}
~file_manager()
{
g_file_manager = nullptr;
}
std::filesystem::path get_base_dir()
{
return m_base_dir;
}
file get_project_file(std::filesystem::path file_path)
{
if (file_path.is_absolute())
throw std::exception("Project files are relative to the BaseDir, don't use absolute paths!");
return file(this, file_path);
}
folder get_project_folder(std::filesystem::path folder_path)
{
if (folder_path.is_absolute())
throw std::exception("Project folders are relative to the BaseDir, don't use absolute paths!");
return folder(this, folder_path);
}
static std::filesystem::path ensure_file_can_be_created(const std::filesystem::path file_path)
{
file_manager::ensure_folder_exists(file_path.parent_path());
return file_path;
}
static std::filesystem::path ensure_folder_exists(const std::filesystem::path folder_path)
{
bool create_path = !std::filesystem::exists(folder_path);
if (!create_path && !std::filesystem::is_directory(folder_path))
{
std::filesystem::remove(folder_path);
create_path = true;
}
if (create_path)
std::filesystem::create_directory(folder_path);
return folder_path;
}
private:
std::filesystem::path m_base_dir;
};
}

View File

@ -0,0 +1,41 @@
#include "common.hpp"
#include "file.hpp"
#include "file_manager.hpp"
namespace big
{
file::file(file_manager* file_manager, std::filesystem::path file_path)
: file(file_manager->get_base_dir() / file_path)
{
m_is_project_file = true;
}
file::file(std::filesystem::path file_path)
: m_file_path(file_manager::ensure_file_can_be_created(file_path)), m_is_project_file(false)
{
}
bool file::exists() const
{
return std::filesystem::exists(m_file_path);
}
const std::filesystem::path file::get_path() const
{
return m_file_path;
}
file file::move(std::filesystem::path new_path)
{
if (new_path.is_relative())
new_path = m_file_path.parent_path() / new_path;
file_manager::ensure_file_can_be_created(new_path);
if (std::filesystem::exists(m_file_path))
std::filesystem::rename(m_file_path, new_path);
return { new_path };
}
}

View File

@ -0,0 +1,28 @@
#pragma once
namespace big
{
class file_manager;
class file
{
public:
file(std::filesystem::path file_path);
file copy(std::filesystem::path new_path);
bool exists() const;
const std::filesystem::path get_path() const;
file move(std::filesystem::path new_path);
protected:
file(file_manager* file_manager, std::filesystem::path file_path);
private:
friend class file_manager;
bool m_is_project_file;
std::filesystem::path m_file_path;
};
}

View File

@ -0,0 +1,33 @@
#include "file_manager.hpp"
#include "folder.hpp"
namespace big
{
folder::folder(file_manager* file_manager, std::filesystem::path file_path)
: folder(file_manager->get_base_dir() / file_path)
{
m_file_manager = file_manager;
m_is_project_file = true;
}
folder::folder(std::filesystem::path folder_path)
: m_folder_path(file_manager::ensure_folder_exists(folder_path))
{
}
file folder::get_file(std::filesystem::path file_path) const
{
if (file_path.is_absolute())
throw std::exception("folder#get_file requires a relative path.");
return file(
m_folder_path / file_path
);
}
const std::filesystem::path folder::get_path() const
{
return m_folder_path;
}
}

View File

@ -0,0 +1,30 @@
#pragma once
namespace big
{
class file;
class file_manager;
class folder
{
public:
folder(std::filesystem::path folder_path);
file get_file(std::filesystem::path file_path) const;
const std::filesystem::path get_path() const;
protected:
folder(file_manager* file_manager, std::filesystem::path file_path);
private:
friend class file_manager;
file_manager* m_file_manager;
bool m_is_project_file;
std::filesystem::path m_folder_path;
};
}