2020-08-11 12:07:21 -04:00
|
|
|
#include "file.hpp"
|
|
|
|
|
2020-08-14 17:45:51 -04:00
|
|
|
#include <cstdio>
|
|
|
|
|
2020-08-11 12:07:21 -04:00
|
|
|
#include "log.hpp"
|
|
|
|
#include "assertions.hpp"
|
|
|
|
|
2021-10-14 08:51:58 -04:00
|
|
|
prism::path prism::root_path(const path& path) {
|
2020-08-11 12:07:21 -04:00
|
|
|
auto p = path;
|
|
|
|
while(p.parent_path() != p && p.parent_path() != "/") {
|
|
|
|
p = p.parent_path();
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2021-10-14 08:51:58 -04:00
|
|
|
std::optional<prism::file> prism::open_file(const prism::path& path, const bool binary_mode) {
|
2020-08-11 12:07:21 -04:00
|
|
|
Expects(!path.empty());
|
|
|
|
|
2020-09-23 11:54:59 -04:00
|
|
|
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) {
|
2021-09-13 23:41:54 -04:00
|
|
|
prism::log("Failed to open file handle from {}!", str);
|
2020-08-11 12:07:21 -04:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-05-12 08:50:02 -04:00
|
|
|
return prism::file(file);
|
2020-08-11 12:07:21 -04:00
|
|
|
}
|
|
|
|
|
2021-05-12 09:05:56 -04:00
|
|
|
prism::path prism::get_file_path(const prism::path& path) {
|
2020-08-17 10:21:32 -04:00
|
|
|
auto fixed_path = path;
|
2020-09-23 11:54:59 -04:00
|
|
|
auto root = root_path(path);
|
|
|
|
if(root == app_domain) {
|
2021-05-12 08:50:02 -04:00
|
|
|
fixed_path = domain_data[static_cast<int>(domain::app)] / path.lexically_relative(root_path(path));
|
2020-09-23 11:54:59 -04:00
|
|
|
} else if(root == internal_domain) {
|
2021-05-12 08:50:02 -04:00
|
|
|
fixed_path = domain_data[static_cast<int>(domain::internal)] / path.lexically_relative(root_path(path));
|
2020-08-17 10:21:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return fixed_path;
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:05:56 -04:00
|
|
|
prism::path prism::get_domain_path(const domain domain) {
|
2020-08-11 12:07:21 -04:00
|
|
|
return domain_data[static_cast<int>(domain)];
|
|
|
|
}
|
|
|
|
|
2021-05-12 09:05:56 -04:00
|
|
|
prism::path parent_domain(const prism::path& path) {
|
2020-08-11 12:07:21 -04:00
|
|
|
return path;
|
|
|
|
}
|
2020-09-21 09:58:42 -04:00
|
|
|
|
2021-10-14 08:51:58 -04:00
|
|
|
prism::path prism::get_relative_path(const domain, const path& path) {
|
2020-09-21 09:58:42 -04:00
|
|
|
// unimplemented
|
2020-09-23 08:44:14 -04:00
|
|
|
return path;
|
2020-09-21 09:58:42 -04:00
|
|
|
}
|