migrate from std::filesystem::path to io::path (WIP)
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
#include "../path.hpp"
|
||||
|
||||
namespace io {
|
||||
class Device {
|
||||
public:
|
||||
virtual ~Device() = default;
|
||||
|
||||
virtual std::filesystem::path resolve(std::string_view path) = 0;
|
||||
|
||||
virtual void write(std::string_view path, const void* data, size_t size) = 0;
|
||||
virtual void read(std::string_view path, void* data, size_t size) = 0;
|
||||
|
||||
virtual size_t size(std::string_view path) = 0;
|
||||
|
||||
virtual bool exists(std::string_view path) = 0;
|
||||
virtual bool isdir(std::string_view path) = 0;
|
||||
virtual bool isfile(std::string_view path) = 0;
|
||||
virtual void mkdirs(std::string_view path) = 0;
|
||||
virtual bool remove(std::string_view path) = 0;
|
||||
virtual uint64_t removeAll(std::string_view path) = 0;
|
||||
};
|
||||
|
||||
class SubDevice : public Device {
|
||||
public:
|
||||
SubDevice(std::shared_ptr<Device> parent, const std::string& path)
|
||||
: parent(std::move(parent)), root(path) {}
|
||||
|
||||
std::filesystem::path resolve(std::string_view path) override {
|
||||
return parent->resolve((root / path).pathPart());
|
||||
}
|
||||
|
||||
void write(std::string_view path, const void* data, size_t size) override {
|
||||
parent->write((root / path).pathPart(), data, size);
|
||||
}
|
||||
|
||||
void read(std::string_view path, void* data, size_t size) override {
|
||||
parent->read((root / path).pathPart(), data, size);
|
||||
}
|
||||
|
||||
size_t size(std::string_view path) override {
|
||||
return parent->size((root / path).pathPart());
|
||||
}
|
||||
|
||||
bool exists(std::string_view path) override {
|
||||
return parent->exists((root / path).pathPart());
|
||||
}
|
||||
|
||||
bool isdir(std::string_view path) override {
|
||||
return parent->isdir((root / path).pathPart());
|
||||
}
|
||||
|
||||
bool isfile(std::string_view path) override {
|
||||
return parent->isfile((root / path).pathPart());
|
||||
}
|
||||
|
||||
void mkdirs(std::string_view path) override {
|
||||
parent->mkdirs((root / path).pathPart());
|
||||
}
|
||||
|
||||
bool remove(std::string_view path) override {
|
||||
return parent->remove((root / path).pathPart());
|
||||
}
|
||||
|
||||
uint64_t removeAll(std::string_view path) override {
|
||||
return parent->removeAll((root / path).pathPart());
|
||||
}
|
||||
private:
|
||||
std::shared_ptr<Device> parent;
|
||||
path root;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "StdfsDevice.hpp"
|
||||
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
using namespace io;
|
||||
|
||||
std::filesystem::path StdfsDevice::resolve(std::string_view path) {
|
||||
return root / std::filesystem::u8path(path);
|
||||
}
|
||||
|
||||
void StdfsDevice::write(std::string_view path, const void* data, size_t size) {
|
||||
auto resolved = resolve(path);
|
||||
std::ofstream output(resolved, std::ios::binary);
|
||||
if (!output.is_open()) {
|
||||
throw std::runtime_error("could not to open file " + resolved.u8string());
|
||||
}
|
||||
output.write((const char*)data, size);
|
||||
}
|
||||
|
||||
void StdfsDevice::read(std::string_view path, void* data, size_t size) {
|
||||
auto resolved = resolve(path);
|
||||
std::ifstream input(resolved, std::ios::binary);
|
||||
if (!input.is_open()) {
|
||||
throw std::runtime_error("could not to open file " + resolved.u8string());
|
||||
}
|
||||
input.read((char*)data, size);
|
||||
}
|
||||
|
||||
size_t StdfsDevice::size(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
return std::filesystem::file_size(resolved);
|
||||
}
|
||||
|
||||
bool StdfsDevice::exists(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
return std::filesystem::exists(resolved);
|
||||
}
|
||||
|
||||
bool StdfsDevice::isdir(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
return std::filesystem::is_directory(resolved);
|
||||
}
|
||||
|
||||
bool StdfsDevice::isfile(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
return std::filesystem::is_regular_file(resolved);
|
||||
}
|
||||
|
||||
void StdfsDevice::mkdirs(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
std::filesystem::create_directories(resolved);
|
||||
}
|
||||
|
||||
bool StdfsDevice::remove(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
return std::filesystem::remove(resolved);
|
||||
}
|
||||
|
||||
uint64_t StdfsDevice::removeAll(std::string_view path) {
|
||||
auto resolved = resolve(path);
|
||||
return std::filesystem::remove_all(resolved);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "Device.hpp"
|
||||
|
||||
namespace io {
|
||||
class StdfsDevice : public Device {
|
||||
public:
|
||||
StdfsDevice(std::filesystem::path root) : root(std::move(root)) {}
|
||||
|
||||
std::filesystem::path resolve(std::string_view path) override;
|
||||
void write(std::string_view path, const void* data, size_t size) override;
|
||||
void read(std::string_view path, void* data, size_t size) override;
|
||||
size_t size(std::string_view path) override;
|
||||
bool exists(std::string_view path) override;
|
||||
bool isdir(std::string_view path) override;
|
||||
bool isfile(std::string_view path) override;
|
||||
void mkdirs(std::string_view path) override;
|
||||
bool remove(std::string_view path) override;
|
||||
uint64_t removeAll(std::string_view path) override;
|
||||
private:
|
||||
std::filesystem::path root;
|
||||
};
|
||||
}
|
||||
+95
-86
@@ -9,9 +9,12 @@
|
||||
#include "util/stringutil.hpp"
|
||||
#include <utility>
|
||||
|
||||
#include "io/devices/StdfsDevice.hpp"
|
||||
#include "world/files/WorldFiles.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static debug::Logger logger("engine-paths");
|
||||
|
||||
static inline auto SCREENSHOTS_FOLDER = std::filesystem::u8path("screenshots");
|
||||
@@ -22,15 +25,16 @@ static inline auto EXPORT_FOLDER = std::filesystem::u8path("export");
|
||||
static inline auto CONTROLS_FILE = std::filesystem::u8path("controls.toml");
|
||||
static inline auto SETTINGS_FILE = std::filesystem::u8path("settings.toml");
|
||||
|
||||
static std::filesystem::path toCanonic(std::filesystem::path path) {
|
||||
static io::path toCanonic(io::path path) {
|
||||
std::stack<std::string> parts;
|
||||
path = path.lexically_normal();
|
||||
|
||||
path = std::filesystem::u8path(path.string()).lexically_normal().string();
|
||||
do {
|
||||
parts.push(path.filename().u8string());
|
||||
path = path.parent_path();
|
||||
parts.push(path.name());
|
||||
path = path.parent();
|
||||
} while (!path.empty());
|
||||
|
||||
path = fs::u8path("");
|
||||
path = "";
|
||||
|
||||
while (!parts.empty()) {
|
||||
const std::string part = parts.top();
|
||||
@@ -48,44 +52,47 @@ static std::filesystem::path toCanonic(std::filesystem::path path) {
|
||||
}
|
||||
|
||||
void EnginePaths::prepare() {
|
||||
if (!fs::is_directory(resourcesFolder)) {
|
||||
io::set_device("res", std::make_shared<io::StdfsDevice>(resourcesFolder));
|
||||
io::set_device("user", std::make_shared<io::StdfsDevice>(userFilesFolder));
|
||||
|
||||
if (!io::is_directory("res:")) {
|
||||
throw std::runtime_error(
|
||||
resourcesFolder.u8string() + " is not a directory"
|
||||
resourcesFolder.string() + " is not a directory"
|
||||
);
|
||||
}
|
||||
if (!fs::is_directory(userFilesFolder)) {
|
||||
fs::create_directories(userFilesFolder);
|
||||
if (!io::is_directory("user:")) {
|
||||
io::create_directories("user:");
|
||||
}
|
||||
|
||||
logger.info() << "resources folder: " << fs::canonical(resourcesFolder).u8string();
|
||||
logger.info() << "user files folder: " << fs::canonical(userFilesFolder).u8string();
|
||||
|
||||
auto contentFolder = userFilesFolder / CONTENT_FOLDER;
|
||||
if (!fs::is_directory(contentFolder)) {
|
||||
fs::create_directories(contentFolder);
|
||||
auto contentFolder = io::path("user:") / CONTENT_FOLDER;
|
||||
if (!io::is_directory(contentFolder)) {
|
||||
io::create_directories(contentFolder);
|
||||
}
|
||||
auto exportFolder = userFilesFolder / EXPORT_FOLDER;
|
||||
if (!fs::is_directory(exportFolder)) {
|
||||
fs::create_directories(exportFolder);
|
||||
auto exportFolder = io::path("user:") / EXPORT_FOLDER;
|
||||
if (!io::is_directory(exportFolder)) {
|
||||
io::create_directories(exportFolder);
|
||||
}
|
||||
auto configFolder = userFilesFolder / CONFIG_FOLDER;
|
||||
if (!fs::is_directory(configFolder)) {
|
||||
fs::create_directories(configFolder);
|
||||
auto configFolder = io::path("user:") / CONFIG_FOLDER;
|
||||
if (!io::is_directory(configFolder)) {
|
||||
io::create_directories(configFolder);
|
||||
}
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getUserFilesFolder() const {
|
||||
const std::filesystem::path& EnginePaths::getUserFilesFolder() const {
|
||||
return userFilesFolder;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getResourcesFolder() const {
|
||||
const std::filesystem::path& EnginePaths::getResourcesFolder() const {
|
||||
return resourcesFolder;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
|
||||
auto folder = userFilesFolder / SCREENSHOTS_FOLDER;
|
||||
if (!fs::is_directory(folder)) {
|
||||
fs::create_directory(folder);
|
||||
io::path EnginePaths::getNewScreenshotFile(const std::string& ext) {
|
||||
auto folder = io::path("user:") / SCREENSHOTS_FOLDER;
|
||||
if (!io::is_directory(folder)) {
|
||||
io::create_directories(folder);
|
||||
}
|
||||
|
||||
auto t = std::time(nullptr);
|
||||
@@ -96,55 +103,55 @@ std::filesystem::path EnginePaths::getNewScreenshotFile(const std::string& ext)
|
||||
ss << std::put_time(&tm, format);
|
||||
std::string datetimestr = ss.str();
|
||||
|
||||
auto filename = folder / fs::u8path("screenshot-" + datetimestr + "." + ext);
|
||||
auto file = folder / ("screenshot-" + datetimestr + "." + ext);
|
||||
uint index = 0;
|
||||
while (fs::exists(filename)) {
|
||||
filename = folder / fs::u8path(
|
||||
"screenshot-" + datetimestr + "-" +
|
||||
std::to_string(index) + "." + ext
|
||||
);
|
||||
while (io::exists(file)) {
|
||||
file = folder / fs::u8path(
|
||||
"screenshot-" + datetimestr + "-" +
|
||||
std::to_string(index) + "." + ext
|
||||
);
|
||||
index++;
|
||||
}
|
||||
return filename;
|
||||
return file;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getWorldsFolder() const {
|
||||
return userFilesFolder / WORLDS_FOLDER;
|
||||
io::path EnginePaths::getWorldsFolder() const {
|
||||
return io::path("user:") / WORLDS_FOLDER;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getConfigFolder() const {
|
||||
return userFilesFolder / CONFIG_FOLDER;
|
||||
io::path EnginePaths::getConfigFolder() const {
|
||||
return io::path("user:") / CONFIG_FOLDER;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getCurrentWorldFolder() {
|
||||
io::path EnginePaths::getCurrentWorldFolder() {
|
||||
return currentWorldFolder;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getWorldFolderByName(const std::string& name) {
|
||||
io::path EnginePaths::getWorldFolderByName(const std::string& name) {
|
||||
return getWorldsFolder() / std::filesystem::path(name);
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getControlsFile() const {
|
||||
return userFilesFolder / CONTROLS_FILE;
|
||||
io::path EnginePaths::getControlsFile() const {
|
||||
return io::path("user:") / CONTROLS_FILE;
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::getSettingsFile() const {
|
||||
return userFilesFolder / SETTINGS_FILE;
|
||||
io::path EnginePaths::getSettingsFile() const {
|
||||
return io::path("user:") / SETTINGS_FILE;
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> EnginePaths::scanForWorlds() const {
|
||||
std::vector<std::filesystem::path> folders;
|
||||
std::vector<io::path> EnginePaths::scanForWorlds() const {
|
||||
std::vector<io::path> folders;
|
||||
|
||||
auto folder = getWorldsFolder();
|
||||
if (!fs::is_directory(folder)) return folders;
|
||||
if (!io::is_directory(folder)) return folders;
|
||||
|
||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||
for (const auto& entry : std::filesystem::directory_iterator(io::resolve(folder))) {
|
||||
if (!entry.is_directory()) {
|
||||
continue;
|
||||
}
|
||||
const auto& worldFolder = entry.path();
|
||||
auto worldFile = worldFolder / fs::u8path(WorldFiles::WORLD_FILE);
|
||||
if (!fs::is_regular_file(worldFile)) {
|
||||
io::path worldFolder = folder / entry.path().filename().u8string();
|
||||
auto worldFile = worldFolder / WorldFiles::WORLD_FILE;
|
||||
if (!io::is_regular_file(worldFile)) {
|
||||
continue;
|
||||
}
|
||||
folders.push_back(worldFolder);
|
||||
@@ -152,10 +159,11 @@ std::vector<std::filesystem::path> EnginePaths::scanForWorlds() const {
|
||||
std::sort(
|
||||
folders.begin(),
|
||||
folders.end(),
|
||||
[](std::filesystem::path a, std::filesystem::path b) {
|
||||
a = a / fs::u8path(WorldFiles::WORLD_FILE);
|
||||
b = b / fs::u8path(WorldFiles::WORLD_FILE);
|
||||
return fs::last_write_time(a) > fs::last_write_time(b);
|
||||
[](io::path a, io::path b) {
|
||||
a = a / WorldFiles::WORLD_FILE;
|
||||
b = b / WorldFiles::WORLD_FILE;
|
||||
return fs::last_write_time(io::resolve(a)) >
|
||||
fs::last_write_time(io::resolve(b));
|
||||
}
|
||||
);
|
||||
return folders;
|
||||
@@ -170,11 +178,13 @@ void EnginePaths::setResourcesFolder(std::filesystem::path folder) {
|
||||
}
|
||||
|
||||
void EnginePaths::setScriptFolder(std::filesystem::path folder) {
|
||||
io::set_device("script", std::make_shared<io::StdfsDevice>(folder));
|
||||
this->scriptFolder = std::move(folder);
|
||||
}
|
||||
|
||||
void EnginePaths::setCurrentWorldFolder(std::filesystem::path folder) {
|
||||
void EnginePaths::setCurrentWorldFolder(io::path folder) {
|
||||
this->currentWorldFolder = std::move(folder);
|
||||
io::create_subdevice("world", "user", currentWorldFolder);
|
||||
}
|
||||
|
||||
void EnginePaths::setContentPacks(std::vector<ContentPack>* contentPacks) {
|
||||
@@ -191,65 +201,64 @@ std::tuple<std::string, std::string> EnginePaths::parsePath(std::string_view pat
|
||||
return {prefix, filename};
|
||||
}
|
||||
|
||||
std::filesystem::path EnginePaths::resolve(
|
||||
// TODO: remove
|
||||
io::path EnginePaths::resolve(
|
||||
const std::string& path, bool throwErr
|
||||
) const {
|
||||
auto [prefix, filename] = EnginePaths::parsePath(path);
|
||||
if (prefix.empty()) {
|
||||
throw files_access_error("no entry point specified");
|
||||
}
|
||||
filename = toCanonic(fs::u8path(filename)).u8string();
|
||||
filename = toCanonic(filename).string();
|
||||
|
||||
if (prefix == "res" || prefix == "core") {
|
||||
return resourcesFolder / fs::u8path(filename);
|
||||
if (prefix == "core") {
|
||||
return io::path("res:") / filename;
|
||||
}
|
||||
if (prefix == "user") {
|
||||
return userFilesFolder / fs::u8path(filename);
|
||||
|
||||
if (prefix == "res" || prefix == "user" || prefix == "script") {
|
||||
return prefix + ":" + filename;
|
||||
}
|
||||
if (prefix == "config") {
|
||||
return getConfigFolder() / fs::u8path(filename);
|
||||
return getConfigFolder() / filename;
|
||||
}
|
||||
if (prefix == "world") {
|
||||
return currentWorldFolder / fs::u8path(filename);
|
||||
return currentWorldFolder / filename;
|
||||
}
|
||||
if (prefix == "export") {
|
||||
return userFilesFolder / EXPORT_FOLDER / fs::u8path(filename);
|
||||
}
|
||||
if (prefix == "script" && scriptFolder) {
|
||||
return scriptFolder.value() / fs::u8path(filename);
|
||||
return io::path("user:") / EXPORT_FOLDER / filename;
|
||||
}
|
||||
if (contentPacks) {
|
||||
for (auto& pack : *contentPacks) {
|
||||
if (pack.id == prefix) {
|
||||
return pack.folder / fs::u8path(filename);
|
||||
return pack.folder / filename;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (throwErr) {
|
||||
throw files_access_error("unknown entry point '" + prefix + "'");
|
||||
}
|
||||
return std::filesystem::path(filename);
|
||||
return filename;
|
||||
}
|
||||
|
||||
ResPaths::ResPaths(std::filesystem::path mainRoot, std::vector<PathsRoot> roots)
|
||||
ResPaths::ResPaths(io::path mainRoot, std::vector<PathsRoot> roots)
|
||||
: mainRoot(std::move(mainRoot)), roots(std::move(roots)) {
|
||||
}
|
||||
|
||||
std::filesystem::path ResPaths::find(const std::string& filename) const {
|
||||
io::path ResPaths::find(const std::string& filename) const {
|
||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||
auto& root = roots[i];
|
||||
auto file = root.path / fs::u8path(filename);
|
||||
if (fs::exists(file)) {
|
||||
auto file = root.path / filename;
|
||||
if (io::exists(file)) {
|
||||
return file;
|
||||
}
|
||||
}
|
||||
return mainRoot / fs::u8path(filename);
|
||||
return mainRoot / filename;
|
||||
}
|
||||
|
||||
std::string ResPaths::findRaw(const std::string& filename) const {
|
||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||
auto& root = roots[i];
|
||||
if (fs::exists(root.path / std::filesystem::path(filename))) {
|
||||
if (io::exists(root.path / filename)) {
|
||||
return root.name + ":" + filename;
|
||||
}
|
||||
}
|
||||
@@ -261,8 +270,8 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) con
|
||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||
auto& root = roots[i];
|
||||
auto folder = root.path / fs::u8path(folderName);
|
||||
if (!fs::is_directory(folder)) continue;
|
||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||
if (!io::is_directory(folder)) continue;
|
||||
for (const auto& entry : fs::directory_iterator(io::resolve(folder))) {
|
||||
auto name = entry.path().filename().u8string();
|
||||
entries.emplace_back(root.name + ":" + folderName + "/" + name);
|
||||
}
|
||||
@@ -270,16 +279,16 @@ std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) con
|
||||
return entries;
|
||||
}
|
||||
|
||||
std::vector<std::filesystem::path> ResPaths::listdir(
|
||||
std::vector<io::path> ResPaths::listdir(
|
||||
const std::string& folderName
|
||||
) const {
|
||||
std::vector<std::filesystem::path> entries;
|
||||
std::vector<io::path> entries;
|
||||
for (int i = roots.size() - 1; i >= 0; i--) {
|
||||
auto& root = roots[i];
|
||||
std::filesystem::path folder = root.path / fs::u8path(folderName);
|
||||
if (!fs::is_directory(folder)) continue;
|
||||
for (const auto& entry : fs::directory_iterator(folder)) {
|
||||
entries.push_back(entry.path());
|
||||
io::path folder = root.path / folderName;
|
||||
if (!io::is_directory(folder)) continue;
|
||||
for (const auto& entry : fs::directory_iterator(io::resolve(folder))) {
|
||||
entries.push_back(folder / entry.path().filename().u8string());
|
||||
}
|
||||
}
|
||||
return entries;
|
||||
@@ -288,8 +297,8 @@ std::vector<std::filesystem::path> ResPaths::listdir(
|
||||
dv::value ResPaths::readCombinedList(const std::string& filename) const {
|
||||
dv::value list = dv::list();
|
||||
for (const auto& root : roots) {
|
||||
auto path = root.path / fs::u8path(filename);
|
||||
if (!fs::exists(path)) {
|
||||
auto path = root.path / filename;
|
||||
if (!io::exists(path)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
@@ -313,8 +322,8 @@ dv::value ResPaths::readCombinedList(const std::string& filename) const {
|
||||
dv::value ResPaths::readCombinedObject(const std::string& filename) const {
|
||||
dv::value object = dv::object();
|
||||
for (const auto& root : roots) {
|
||||
auto path = root.path / fs::u8path(filename);
|
||||
if (!fs::exists(path)) {
|
||||
auto path = root.path / filename;
|
||||
if (!io::exists(path)) {
|
||||
continue;
|
||||
}
|
||||
try {
|
||||
@@ -335,6 +344,6 @@ dv::value ResPaths::readCombinedObject(const std::string& filename) const {
|
||||
return object;
|
||||
}
|
||||
|
||||
const std::filesystem::path& ResPaths::getMainRoot() const {
|
||||
const io::path& ResPaths::getMainRoot() const {
|
||||
return mainRoot;
|
||||
}
|
||||
|
||||
+20
-21
@@ -1,16 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <filesystem>
|
||||
#include <stdexcept>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <tuple>
|
||||
|
||||
#include "io.hpp"
|
||||
#include "data/dv.hpp"
|
||||
#include "content/ContentPack.hpp"
|
||||
|
||||
|
||||
class files_access_error : public std::runtime_error {
|
||||
public:
|
||||
files_access_error(const std::string& msg) : std::runtime_error(msg) {
|
||||
@@ -22,29 +21,29 @@ public:
|
||||
void prepare();
|
||||
|
||||
void setUserFilesFolder(std::filesystem::path folder);
|
||||
std::filesystem::path getUserFilesFolder() const;
|
||||
const std::filesystem::path& getUserFilesFolder() const;
|
||||
|
||||
void setResourcesFolder(std::filesystem::path folder);
|
||||
std::filesystem::path getResourcesFolder() const;
|
||||
const std::filesystem::path& getResourcesFolder() const;
|
||||
|
||||
void setScriptFolder(std::filesystem::path folder);
|
||||
|
||||
std::filesystem::path getWorldFolderByName(const std::string& name);
|
||||
std::filesystem::path getWorldsFolder() const;
|
||||
std::filesystem::path getConfigFolder() const;
|
||||
io::path getWorldFolderByName(const std::string& name);
|
||||
io::path getWorldsFolder() const;
|
||||
io::path getConfigFolder() const;
|
||||
|
||||
void setCurrentWorldFolder(std::filesystem::path folder);
|
||||
std::filesystem::path getCurrentWorldFolder();
|
||||
void setCurrentWorldFolder(io::path folder);
|
||||
io::path getCurrentWorldFolder();
|
||||
|
||||
std::filesystem::path getNewScreenshotFile(const std::string& ext);
|
||||
std::filesystem::path getControlsFile() const;
|
||||
std::filesystem::path getSettingsFile() const;
|
||||
io::path getNewScreenshotFile(const std::string& ext);
|
||||
io::path getControlsFile() const;
|
||||
io::path getSettingsFile() const;
|
||||
|
||||
void setContentPacks(std::vector<ContentPack>* contentPacks);
|
||||
|
||||
std::vector<std::filesystem::path> scanForWorlds() const;
|
||||
std::vector<io::path> scanForWorlds() const;
|
||||
|
||||
std::filesystem::path resolve(const std::string& path, bool throwErr = true) const;
|
||||
io::path resolve(const std::string& path, bool throwErr = true) const;
|
||||
|
||||
static std::tuple<std::string, std::string> parsePath(std::string_view view);
|
||||
|
||||
@@ -53,23 +52,23 @@ public:
|
||||
private:
|
||||
std::filesystem::path userFilesFolder {"."};
|
||||
std::filesystem::path resourcesFolder {"res"};
|
||||
std::filesystem::path currentWorldFolder;
|
||||
io::path currentWorldFolder;
|
||||
std::optional<std::filesystem::path> scriptFolder;
|
||||
std::vector<ContentPack>* contentPacks = nullptr;
|
||||
};
|
||||
|
||||
struct PathsRoot {
|
||||
std::string name;
|
||||
std::filesystem::path path;
|
||||
io::path path;
|
||||
};
|
||||
|
||||
class ResPaths {
|
||||
public:
|
||||
ResPaths(std::filesystem::path mainRoot, std::vector<PathsRoot> roots);
|
||||
ResPaths(io::path mainRoot, std::vector<PathsRoot> roots);
|
||||
|
||||
std::filesystem::path find(const std::string& filename) const;
|
||||
io::path find(const std::string& filename) const;
|
||||
std::string findRaw(const std::string& filename) const;
|
||||
std::vector<std::filesystem::path> listdir(const std::string& folder) const;
|
||||
std::vector<io::path> listdir(const std::string& folder) const;
|
||||
std::vector<std::string> listdirRaw(const std::string& folder) const;
|
||||
|
||||
/// @brief Read all found list versions from all packs and combine into a
|
||||
@@ -79,9 +78,9 @@ public:
|
||||
|
||||
dv::value readCombinedObject(const std::string& file) const;
|
||||
|
||||
const std::filesystem::path& getMainRoot() const;
|
||||
const io::path& getMainRoot() const;
|
||||
|
||||
private:
|
||||
std::filesystem::path mainRoot;
|
||||
io::path mainRoot;
|
||||
std::vector<PathsRoot> roots;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
namespace io {
|
||||
class path;
|
||||
}
|
||||
+142
-72
@@ -1,7 +1,7 @@
|
||||
#include "io.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
@@ -13,10 +13,44 @@
|
||||
#include "coders/toml.hpp"
|
||||
#include "util/stringutil.hpp"
|
||||
|
||||
#include "devices/Device.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
io::rafile::rafile(const fs::path& filename)
|
||||
: file(filename, std::ios::binary | std::ios::ate) {
|
||||
static std::map<std::string, std::shared_ptr<io::Device>> devices;
|
||||
|
||||
void io::set_device(const std::string& name, std::shared_ptr<io::Device> device) {
|
||||
devices[name] = device;
|
||||
}
|
||||
|
||||
std::shared_ptr<io::Device> io::get_device(const std::string& name) {
|
||||
const auto& found = devices.find(name);
|
||||
if (found == devices.end()) {
|
||||
return nullptr;
|
||||
}
|
||||
return found->second;
|
||||
}
|
||||
|
||||
io::Device& io::require_device(const std::string& name) {
|
||||
auto device = get_device(name);
|
||||
if (!device) {
|
||||
throw std::runtime_error("io-device not found: " + name);
|
||||
}
|
||||
return *device;
|
||||
}
|
||||
|
||||
void io::create_subdevice(
|
||||
const std::string& name, const std::string& parent, const io::path& root
|
||||
) {
|
||||
auto parentDevice = get_device(parent);
|
||||
if (!parentDevice) {
|
||||
throw std::runtime_error("parent device not found for entry-point: " + parent);
|
||||
}
|
||||
set_device(name, std::make_shared<io::SubDevice>(parentDevice, root.pathPart()));
|
||||
}
|
||||
|
||||
io::rafile::rafile(const io::path& filename)
|
||||
: file(io::resolve(filename), std::ios::binary | std::ios::ate) {
|
||||
if (!file) {
|
||||
throw std::runtime_error("could not to open file " + filename.string());
|
||||
}
|
||||
@@ -37,121 +71,92 @@ void io::rafile::read(char* buffer, std::streamsize size) {
|
||||
}
|
||||
|
||||
bool io::write_bytes(
|
||||
const fs::path& filename, const ubyte* data, size_t size
|
||||
const io::path& filename, const ubyte* data, size_t size
|
||||
) {
|
||||
std::ofstream output(filename, std::ios::binary);
|
||||
if (!output.is_open()) return false;
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
auto device = io::get_device(filename.entryPoint());
|
||||
if (device == nullptr) {
|
||||
return false;
|
||||
}
|
||||
device->write(filename.pathPart(), data, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint io::append_bytes(
|
||||
const fs::path& filename, const ubyte* data, size_t size
|
||||
) {
|
||||
std::ofstream output(filename, std::ios::binary | std::ios::app);
|
||||
if (!output.is_open()) return 0;
|
||||
uint position = output.tellp();
|
||||
output.write((const char*)data, size);
|
||||
output.close();
|
||||
return position;
|
||||
}
|
||||
|
||||
bool io::read(const fs::path& filename, char* data, size_t size) {
|
||||
std::ifstream output(filename, std::ios::binary);
|
||||
if (!output.is_open()) return false;
|
||||
output.read(data, size);
|
||||
output.close();
|
||||
bool io::read(const io::path& filename, char* data, size_t size) {
|
||||
auto device = io::get_device(filename.entryPoint());
|
||||
if (device == nullptr) {
|
||||
return false;
|
||||
}
|
||||
device->read(filename.pathPart(), data, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
util::Buffer<ubyte> io::read_bytes_buffer(const fs::path& path) {
|
||||
util::Buffer<ubyte> io::read_bytes_buffer(const path& file) {
|
||||
size_t size;
|
||||
auto bytes = io::read_bytes(path, size);
|
||||
auto bytes = io::read_bytes(file, size);
|
||||
return util::Buffer<ubyte>(std::move(bytes), size);
|
||||
}
|
||||
|
||||
std::unique_ptr<ubyte[]> io::read_bytes(
|
||||
const fs::path& filename, size_t& length
|
||||
const io::path& filename, size_t& length
|
||||
) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open()) {
|
||||
throw std::runtime_error(
|
||||
"could not to load file '" + filename.string() + "'"
|
||||
);
|
||||
}
|
||||
input.seekg(0, std::ios_base::end);
|
||||
length = input.tellg();
|
||||
input.seekg(0, std::ios_base::beg);
|
||||
|
||||
auto& device = io::require_device(filename.entryPoint());
|
||||
length = device.size(filename.pathPart());
|
||||
auto data = std::make_unique<ubyte[]>(length);
|
||||
input.read((char*)data.get(), length);
|
||||
input.close();
|
||||
device.read(filename.pathPart(), data.get(), length);
|
||||
return data;
|
||||
}
|
||||
|
||||
std::vector<ubyte> io::read_bytes(const fs::path& filename) {
|
||||
std::ifstream input(filename, std::ios::binary);
|
||||
if (!input.is_open()) return {};
|
||||
input.seekg(0, std::ios_base::end);
|
||||
size_t length = input.tellg();
|
||||
input.seekg(0, std::ios_base::beg);
|
||||
|
||||
std::vector<ubyte> io::read_bytes(const path& filename) {
|
||||
auto& device = io::require_device(filename.entryPoint());
|
||||
size_t length = device.size(filename.pathPart());
|
||||
std::vector<ubyte> data(length);
|
||||
data.resize(length);
|
||||
input.read((char*)data.data(), length);
|
||||
input.close();
|
||||
device.read(filename.pathPart(), data.data(), length);
|
||||
return data;
|
||||
}
|
||||
|
||||
std::string io::read_string(const fs::path& filename) {
|
||||
std::string io::read_string(const path& filename) {
|
||||
size_t size;
|
||||
auto bytes = read_bytes(filename, size);
|
||||
return std::string((const char*)bytes.get(), size);
|
||||
}
|
||||
|
||||
bool io::write_string(const fs::path& filename, std::string_view content) {
|
||||
std::ofstream file(filename);
|
||||
if (!file) {
|
||||
return false;
|
||||
}
|
||||
file << content;
|
||||
return true;
|
||||
bool io::write_string(const io::path& file, std::string_view content) {
|
||||
return io::write_bytes(file, (const ubyte*)content.data(), content.size());
|
||||
}
|
||||
|
||||
bool io::write_json(
|
||||
const fs::path& filename, const dv::value& obj, bool nice
|
||||
const io::path& file, const dv::value& obj, bool nice
|
||||
) {
|
||||
return io::write_string(filename, json::stringify(obj, nice, " "));
|
||||
return io::write_string(file, json::stringify(obj, nice, " "));
|
||||
}
|
||||
|
||||
bool io::write_binary_json(
|
||||
const fs::path& filename, const dv::value& obj, bool compression
|
||||
const io::path& file, const dv::value& obj, bool compression
|
||||
) {
|
||||
auto bytes = json::to_binary(obj, compression);
|
||||
return io::write_bytes(filename, bytes.data(), bytes.size());
|
||||
return io::write_bytes(file, bytes.data(), bytes.size());
|
||||
}
|
||||
|
||||
dv::value io::read_json(const fs::path& filename) {
|
||||
dv::value io::read_json(const path& filename) {
|
||||
std::string text = io::read_string(filename);
|
||||
return json::parse(filename.string(), text);
|
||||
}
|
||||
|
||||
dv::value io::read_binary_json(const fs::path& file) {
|
||||
dv::value io::read_binary_json(const path& file) {
|
||||
size_t size;
|
||||
auto bytes = io::read_bytes(file, size);
|
||||
return json::from_binary(bytes.get(), size);
|
||||
}
|
||||
|
||||
dv::value io::read_toml(const fs::path& file) {
|
||||
return toml::parse(file.u8string(), io::read_string(file));
|
||||
dv::value io::read_toml(const path& file) {
|
||||
return toml::parse(file.string(), io::read_string(file));
|
||||
}
|
||||
|
||||
std::vector<std::string> io::read_list(const fs::path& filename) {
|
||||
std::ifstream file(filename);
|
||||
std::vector<std::string> io::read_list(const io::path& filename) {
|
||||
std::ifstream file(resolve(filename)); // FIXME
|
||||
if (!file) {
|
||||
throw std::runtime_error(
|
||||
"could not to open file " + filename.u8string()
|
||||
"could not to open file " + filename.string()
|
||||
);
|
||||
}
|
||||
std::vector<std::string> lines;
|
||||
@@ -165,6 +170,71 @@ std::vector<std::string> io::read_list(const fs::path& filename) {
|
||||
return lines;
|
||||
}
|
||||
|
||||
bool io::is_regular_file(const io::path& file) {
|
||||
if (file.empty()) {
|
||||
return false;
|
||||
}
|
||||
auto device = io::get_device(file.entryPoint());
|
||||
if (device == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return device->isfile(file.pathPart());
|
||||
}
|
||||
|
||||
bool io::is_directory(const io::path& file) {
|
||||
if (file.empty()) {
|
||||
return false;
|
||||
}
|
||||
auto device = io::get_device(file.entryPoint());
|
||||
if (device == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return device->isdir(file.pathPart());
|
||||
}
|
||||
|
||||
bool io::exists(const io::path& file) {
|
||||
if (file.empty()) {
|
||||
return false;
|
||||
}
|
||||
auto device = io::get_device(file.entryPoint());
|
||||
if (device == nullptr) {
|
||||
return false;
|
||||
}
|
||||
return device->exists(file.pathPart());
|
||||
}
|
||||
|
||||
bool io::create_directories(const io::path& file) {
|
||||
auto& device = io::require_device(file.entryPoint());
|
||||
if (device.isdir(file.pathPart())) {
|
||||
return false;
|
||||
}
|
||||
device.mkdirs(file.pathPart());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool io::remove(const io::path& file) {
|
||||
auto& device = io::require_device(file.entryPoint());
|
||||
return device.remove(file.pathPart());
|
||||
}
|
||||
|
||||
uint64_t io::remove_all(const io::path& file) {
|
||||
auto& device = io::require_device(file.entryPoint());
|
||||
return device.removeAll(file.pathPart());
|
||||
}
|
||||
|
||||
size_t io::file_size(const io::path& file) {
|
||||
auto& device = io::require_device(file.entryPoint());
|
||||
return device.size(file.pathPart());
|
||||
}
|
||||
|
||||
std::filesystem::path io::resolve(const io::path& file) {
|
||||
auto device = io::get_device(file.entryPoint());
|
||||
if (device == nullptr) {
|
||||
return {};
|
||||
}
|
||||
return device->resolve(file.pathPart());
|
||||
}
|
||||
|
||||
#include <map>
|
||||
|
||||
#include "coders/json.hpp"
|
||||
@@ -177,22 +247,22 @@ static std::map<fs::path, DecodeFunc> data_decoders {
|
||||
{fs::u8path(".toml"), toml::parse},
|
||||
};
|
||||
|
||||
bool io::is_data_file(const fs::path& file) {
|
||||
bool io::is_data_file(const io::path& file) {
|
||||
return is_data_interchange_format(file.extension());
|
||||
}
|
||||
|
||||
bool io::is_data_interchange_format(const fs::path& ext) {
|
||||
bool io::is_data_interchange_format(const std::string& ext) {
|
||||
return data_decoders.find(ext) != data_decoders.end();
|
||||
}
|
||||
|
||||
dv::value io::read_object(const fs::path& file) {
|
||||
dv::value io::read_object(const path& file) {
|
||||
const auto& found = data_decoders.find(file.extension());
|
||||
if (found == data_decoders.end()) {
|
||||
throw std::runtime_error("unknown file format");
|
||||
}
|
||||
auto text = read_string(file);
|
||||
try {
|
||||
return found->second(file.u8string(), text);
|
||||
return found->second(file.string(), text);
|
||||
} catch (const parsing_error& err) {
|
||||
throw std::runtime_error(err.errorLog());
|
||||
}
|
||||
|
||||
+38
-25
@@ -9,16 +9,25 @@
|
||||
#include "typedefs.hpp"
|
||||
#include "data/dv.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
#include "path.hpp"
|
||||
|
||||
namespace io {
|
||||
class Device;
|
||||
|
||||
void set_device(const std::string& name, std::shared_ptr<Device> device);
|
||||
std::shared_ptr<Device> get_device(const std::string& name);
|
||||
Device& require_device(const std::string& name);
|
||||
|
||||
void create_subdevice(
|
||||
const std::string& name, const std::string& parent, const path& root
|
||||
);
|
||||
|
||||
/// @brief Read-only random access file
|
||||
class rafile {
|
||||
std::ifstream file;
|
||||
size_t filelength;
|
||||
public:
|
||||
rafile(const fs::path& filename);
|
||||
rafile(const path& filename);
|
||||
|
||||
void seekg(std::streampos pos);
|
||||
void read(char* buffer, std::streamsize size);
|
||||
@@ -29,52 +38,56 @@ namespace io {
|
||||
/// @param file target file
|
||||
/// @param data data bytes array
|
||||
/// @param size size of data bytes array
|
||||
bool write_bytes(const fs::path& file, const ubyte* data, size_t size);
|
||||
|
||||
/// @brief Append bytes array to the file without any extra data
|
||||
/// @param file target file
|
||||
/// @param data data bytes array
|
||||
/// @param size size of data bytes array
|
||||
uint append_bytes(const fs::path& file, const ubyte* data, size_t size);
|
||||
bool write_bytes(const io::path& file, const ubyte* data, size_t size);
|
||||
|
||||
/// @brief Write string to the file
|
||||
bool write_string(const fs::path& filename, std::string_view content);
|
||||
bool write_string(const io::path& file, std::string_view content);
|
||||
|
||||
/// @brief Write dynamic data to the JSON file
|
||||
/// @param nice if true, human readable format will be used, otherwise
|
||||
/// minimal
|
||||
bool write_json(
|
||||
const fs::path& filename, const dv::value& obj, bool nice = true
|
||||
const io::path& file, const dv::value& obj, bool nice = true
|
||||
);
|
||||
|
||||
/// @brief Write dynamic data to the binary JSON file
|
||||
/// (see src/coders/binary_json_spec.md)
|
||||
/// @param compressed use gzip compression
|
||||
bool write_binary_json(
|
||||
const fs::path& filename,
|
||||
const io::path& file,
|
||||
const dv::value& obj,
|
||||
bool compressed = false
|
||||
);
|
||||
|
||||
bool read(const fs::path&, char* data, size_t size);
|
||||
util::Buffer<ubyte> read_bytes_buffer(const fs::path&);
|
||||
std::unique_ptr<ubyte[]> read_bytes(const fs::path&, size_t& length);
|
||||
std::vector<ubyte> read_bytes(const fs::path&);
|
||||
std::string read_string(const fs::path& filename);
|
||||
bool read(const io::path& file, char* data, size_t size);
|
||||
util::Buffer<ubyte> read_bytes_buffer(const path& file);
|
||||
std::unique_ptr<ubyte[]> read_bytes(const path& file, size_t& length);
|
||||
std::vector<ubyte> read_bytes(const path& file);
|
||||
std::string read_string(const path& file);
|
||||
|
||||
/// @brief Read JSON or BJSON file
|
||||
/// @param file *.json or *.bjson file
|
||||
dv::value read_json(const fs::path& file);
|
||||
dv::value read_json(const path& file);
|
||||
|
||||
dv::value read_binary_json(const fs::path& file);
|
||||
dv::value read_binary_json(const path& file);
|
||||
|
||||
/// @brief Read TOML file
|
||||
/// @param file *.toml file
|
||||
dv::value read_toml(const fs::path& file);
|
||||
dv::value read_toml(const path& file);
|
||||
|
||||
std::vector<std::string> read_list(const fs::path& file);
|
||||
std::vector<std::string> read_list(const io::path& file);
|
||||
|
||||
bool is_data_file(const fs::path& file);
|
||||
bool is_data_interchange_format(const fs::path& ext);
|
||||
dv::value read_object(const fs::path& file);
|
||||
bool is_regular_file(const io::path& file);
|
||||
bool is_directory(const io::path& file);
|
||||
bool exists(const io::path& file);
|
||||
bool create_directories(const io::path& file);
|
||||
bool remove(const io::path& file);
|
||||
uint64_t remove_all(const io::path& file);
|
||||
size_t file_size(const io::path& file);
|
||||
|
||||
std::filesystem::path resolve(const io::path& file);
|
||||
|
||||
bool is_data_file(const io::path& file);
|
||||
bool is_data_interchange_format(const std::string& ext);
|
||||
dv::value read_object(const path& file);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "path.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace io;
|
||||
|
||||
void path::checkValid() const {
|
||||
if (colonPos == std::string::npos) {
|
||||
throw std::runtime_error("path entry point is not specified: " + str);
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <filesystem>
|
||||
|
||||
namespace io {
|
||||
/// @brief std::filesystem::path project-specific alternative having
|
||||
/// `entry_point:path` scheme and solving std::filesystem::path problems:
|
||||
/// - implicit std::string conversions depending on compiler
|
||||
/// - unicode path construction must be done with std::filesystem::u8path
|
||||
class path {
|
||||
public:
|
||||
path() = default;
|
||||
|
||||
path(std::string str) : str(std::move(str)) {
|
||||
colonPos = this->str.find(':');
|
||||
|
||||
size_t len = this->str.length();
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
if (this->str[i] == '\\') {
|
||||
this->str[i] = '/';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
path(const char* str) : path(std::string(str)) {}
|
||||
|
||||
bool operator==(const std::string& other) const {
|
||||
return str == other;
|
||||
}
|
||||
|
||||
bool operator<(const path& other) const {
|
||||
return str < other.str;
|
||||
}
|
||||
|
||||
bool operator==(const path& other) const {
|
||||
return str == other.str;
|
||||
}
|
||||
|
||||
bool operator==(const char* other) const {
|
||||
return str == other;
|
||||
}
|
||||
|
||||
path operator/(const char* child) const {
|
||||
if (str.empty() || str[str.length()-1] == ':') {
|
||||
return str + std::string(child);
|
||||
}
|
||||
return str + "/" + std::string(child);
|
||||
}
|
||||
|
||||
path operator/(const std::string& child) const {
|
||||
if (str.empty() || str[str.length()-1] == ':') {
|
||||
return str + child;
|
||||
}
|
||||
return str + "/" + child;
|
||||
}
|
||||
|
||||
path operator/(std::string_view child) const {
|
||||
if (str.empty() || str[str.length()-1] == ':') {
|
||||
return str + std::string(child);
|
||||
}
|
||||
return str + "/" + std::string(child);
|
||||
}
|
||||
|
||||
path operator/(const path& child) const {
|
||||
if (str.empty() || str[str.length()-1] == ':') {
|
||||
return str + child.pathPart();
|
||||
}
|
||||
return str + "/" + child.pathPart();
|
||||
}
|
||||
|
||||
std::string pathPart() const {
|
||||
if (colonPos == std::string::npos) {
|
||||
return str;
|
||||
}
|
||||
return str.substr(colonPos + 1);
|
||||
}
|
||||
|
||||
std::string name() const {
|
||||
size_t slashpos = str.rfind('/');
|
||||
if (slashpos == std::string::npos) {
|
||||
return colonPos == std::string::npos ? str
|
||||
: str.substr(colonPos + 1);
|
||||
}
|
||||
return str.substr(slashpos + 1);
|
||||
}
|
||||
|
||||
std::string stem() const {
|
||||
return name().substr(0, name().rfind('.'));
|
||||
}
|
||||
|
||||
/// @brief Get extension
|
||||
std::string extension() const {
|
||||
size_t slashpos = str.rfind('/');
|
||||
size_t dotpos = str.rfind('.');
|
||||
if (dotpos == std::string::npos ||
|
||||
(slashpos != std::string::npos && dotpos < slashpos)) {
|
||||
return "";
|
||||
}
|
||||
return str.substr(dotpos);
|
||||
}
|
||||
|
||||
/// @brief Get entry point
|
||||
std::string entryPoint() const {
|
||||
checkValid();
|
||||
return str.substr(0, colonPos);
|
||||
}
|
||||
|
||||
/// @brief Get parent path
|
||||
path parent() const {
|
||||
size_t slashpos = str.rfind('/');
|
||||
if (slashpos == std::string::npos) {
|
||||
return colonPos == std::string::npos
|
||||
? path()
|
||||
: path(str.substr(0, colonPos));
|
||||
}
|
||||
return colonPos == std::string::npos
|
||||
? path(str.substr(0, slashpos))
|
||||
: path(str.substr(0, colonPos) + str.substr(slashpos));
|
||||
}
|
||||
|
||||
std::string string() const {
|
||||
return str;
|
||||
}
|
||||
|
||||
/// @brief Check if path is not initialized with 'entry_point:path'
|
||||
bool empty() const {
|
||||
return str.empty();
|
||||
}
|
||||
private:
|
||||
/// @brief UTF-8 string contains entry_point:path or empty string
|
||||
std::string str;
|
||||
/// @brief Precalculated position of colon character
|
||||
size_t colonPos = std::string::npos;
|
||||
|
||||
void checkValid() const;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user