Yerbacon/src/headers/emscripten_compatibility.h
Username404 ade66e8343
main.cpp: Add emscripten support to the "--output" argument
Signed-off-by: Username404 <w.iron.zombie@gmail.com>
2023-06-16 12:08:54 +02:00

36 lines
1.3 KiB
C++

#ifndef YERBACON_EMSCRIPTEN_COMPATIBILITY_H
#define YERBACON_EMSCRIPTEN_COMPATIBILITY_H
#include <string_view>
#include <string>
#include <filesystem>
filesystem::path mount(const std::string_view& element_path) {
filesystem::path file_path = element_path;
#ifdef EMSCRIPTEN
if (element_path.size() >= 2 and element_path[1] == ':') {
auto path = string(element_path.begin() + 2, element_path.end());
replace(path.begin(), path.end(), '\\', '/');
file_path = path;
}
const string directory = filesystem::path(file_path).remove_filename();
cout << "Directory: " << directory << endl;
for (unsigned int character = 1; character < directory.size(); ++character) {
const string sub_directory = directory.substr(0, character);
if (is_node and (directory[character] == '/') and not filesystem::is_directory(sub_directory)) {
MAIN_THREAD_EM_ASM({
const sub_directory = UTF8ToString($0);
if (!FS.isMountpoint(sub_directory)) {
try {
FS.mkdir(sub_directory);
FS.mount(NODEFS, {root: sub_directory}, sub_directory);
} catch (exception) {}
};
}, sub_directory.c_str());
}
}
#endif
return file_path;
}
#endif //YERBACON_EMSCRIPTEN_COMPATIBILITY_H