Archived
1
Fork 0
This repository has been archived on 2025-04-12. You can view files and clone it, but cannot push or open issues or pull requests.
prism/engine/core/src/file.cpp

55 lines
1.4 KiB
C++
Raw Normal View History

2020-08-11 12:07:21 -04:00
#include "file.hpp"
#include <cstdio>
2020-08-11 12:07:21 -04:00
#include "string_utils.hpp"
#include "log.hpp"
#include "assertions.hpp"
file::Path file::root_path(const Path path) {
auto p = path;
while(p.parent_path() != p && p.parent_path() != "/") {
p = p.parent_path();
}
return p;
}
std::optional<file::File> file::open(const file::Path path, const bool binary_mode) {
Expects(!path.empty());
auto str = get_file_path(path).string();
FILE* file = fopen(str.c_str(), binary_mode ? "rb" : "r");
2020-08-11 12:07:21 -04:00
if(file == nullptr) {
console::error(System::File, "Failed to open file handle from {}!", str);
2020-08-11 12:07:21 -04:00
return {};
}
return file::File(file);
}
2020-08-17 10:21:32 -04:00
file::Path file::get_file_path(const file::Path path) {
auto fixed_path = path;
auto root = root_path(path);
if(root == app_domain) {
2020-08-17 10:21:32 -04:00
fixed_path = domain_data[static_cast<int>(Domain::App)] / path.lexically_relative(root_path(path));
} else if(root == internal_domain) {
2020-08-17 10:21:32 -04:00
fixed_path = domain_data[static_cast<int>(Domain::Internal)] / path.lexically_relative(root_path(path));
}
return fixed_path;
}
2020-08-11 12:07:21 -04:00
file::Path file::get_domain_path(const Domain domain) {
return domain_data[static_cast<int>(domain)];
}
file::Path parent_domain(const file::Path path) {
return path;
}
2020-09-21 09:58:42 -04:00
file::Path file::get_relative_path(const Domain domain, const Path path) {
// unimplemented
2020-09-23 08:44:14 -04:00
return path;
2020-09-21 09:58:42 -04:00
}