mirror of
https://github.com/momo5502/emulator.git
synced 2026-02-01 00:41:02 +00:00
Use working directory provider
This commit is contained in:
@@ -4,20 +4,26 @@
|
||||
|
||||
#include <platform/compiler.hpp>
|
||||
|
||||
struct working_directory_provider
|
||||
{
|
||||
virtual windows_path get_working_directory() = 0;
|
||||
};
|
||||
|
||||
class file_system
|
||||
{
|
||||
public:
|
||||
file_system(std::filesystem::path root, windows_path working_dir = "C:\\")
|
||||
: root_(std::move(root))
|
||||
file_system(std::filesystem::path root, working_directory_provider& working_dir_provider)
|
||||
: root_(std::move(root)),
|
||||
working_dir_provider_(&working_dir_provider)
|
||||
{
|
||||
this->set_working_directory(std::move(working_dir));
|
||||
}
|
||||
|
||||
std::filesystem::path translate(const windows_path& win_path) const
|
||||
{
|
||||
assert(win_path.is_absolute() && "Path should always be absolute");
|
||||
const auto& full_path = win_path.is_absolute() //
|
||||
? win_path
|
||||
: (this->working_dir_ / win_path);
|
||||
: (this->working_dir_provider_->get_working_directory() / win_path);
|
||||
|
||||
const auto mapping = this->mappings_.find(full_path);
|
||||
if (mapping != this->mappings_.end())
|
||||
@@ -36,20 +42,10 @@ class file_system
|
||||
return this->root_ / full_path.to_portable_path();
|
||||
}
|
||||
|
||||
void set_working_directory(windows_path working_dir)
|
||||
{
|
||||
if (!working_dir.is_absolute())
|
||||
{
|
||||
throw std::runtime_error("Working directory is not an absolute path: " + working_dir.string());
|
||||
}
|
||||
|
||||
this->working_dir_ = std::move(working_dir);
|
||||
}
|
||||
|
||||
const windows_path& get_working_directory() const
|
||||
/*const windows_path& get_working_directory() const
|
||||
{
|
||||
return this->working_dir_;
|
||||
}
|
||||
}*/
|
||||
|
||||
windows_path local_to_windows_path(const std::filesystem::path& local_path) const
|
||||
{
|
||||
@@ -81,16 +77,6 @@ class file_system
|
||||
return windows_path{drive, std::move(folders)};
|
||||
}
|
||||
|
||||
void serialize(utils::buffer_serializer& buffer) const
|
||||
{
|
||||
buffer.write(this->working_dir_);
|
||||
}
|
||||
|
||||
void deserialize(utils::buffer_deserializer& buffer)
|
||||
{
|
||||
buffer.read(this->working_dir_);
|
||||
}
|
||||
|
||||
void map(windows_path src, std::filesystem::path dest)
|
||||
{
|
||||
this->mappings_[std::move(src)] = std::move(dest);
|
||||
@@ -98,6 +84,6 @@ class file_system
|
||||
|
||||
private:
|
||||
std::filesystem::path root_{};
|
||||
windows_path working_dir_{};
|
||||
working_directory_provider* working_dir_provider_{};
|
||||
std::unordered_map<windows_path, std::filesystem::path> mappings_{};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user