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

54 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 "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());
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) {
prism::log("Failed to open file handle from {}!", str);
2020-08-11 12:07:21 -04:00
return {};
}
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;
auto root = root_path(path);
if(root == app_domain) {
fixed_path = domain_data[static_cast<int>(domain::app)] / path.lexically_relative(root_path(path));
} else if(root == internal_domain) {
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
}