TmpMenu/src/file_manager.cpp
Andreas Maerten 5c6fb1064b refactor!: File Manager (#1633)
* feat(vscode): added launch.json to dbg
* feat(file_manager): Make use of a global instance
* feat(file_manager): Allow for file and folder instances to be empty
* refactor(GlobalsService): Update code of global service (untested)
2023-07-08 17:54:59 +02:00

56 lines
1.6 KiB
C++

#include "file_manager.hpp"
namespace big
{
bool file_manager::init(const std::filesystem::path& base_dir)
{
m_base_dir = base_dir;
file_manager::ensure_folder_exists(m_base_dir);
return true;
}
const std::filesystem::path& file_manager::get_base_dir()
{
return m_base_dir;
}
file file_manager::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_manager::ensure_file_can_be_created(m_base_dir / file_path);
}
folder file_manager::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 file_manager::ensure_folder_exists(m_base_dir / folder_path);
}
std::filesystem::path file_manager::ensure_file_can_be_created(const std::filesystem::path file_path)
{
file_manager::ensure_folder_exists(file_path.parent_path());
return file_path;
}
std::filesystem::path file_manager::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;
}
}