main.cpp: Support Windows paths on node.js

Signed-off-by: Username404 <w.iron.zombie@gmail.com>
This commit is contained in:
Username404 2022-12-04 22:24:37 +01:00
parent d1ebbda173
commit 64ef5e60ff
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 27 additions and 27 deletions

View File

@ -42,41 +42,41 @@ int main(int argc, char* argv[]) {
#ifdef _OPENMP #ifdef _OPENMP
if (not parallel) omp_set_num_threads(1); if (not parallel) omp_set_num_threads(1);
#endif #endif
optional file_path = make_optional<filesystem::path>();
if (not text_provided) {
file_path = filesystem::path(currentArgument);
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
const string directory (currentArgument.substr(0, currentArgument.find_last_of(filesystem::path::preferred_separator) + 1)); if (currentArgument.size() >= 2 and currentArgument[1] == ':') {
for (unsigned int i = 1; i < directory.size(); ++i) { auto path = string(currentArgument).erase(0, 2);
const string sub_directory = directory.substr(0, i); replace(path.begin(), path.end(), '\\', '/');
if (directory[i] == '/' and not filesystem::is_directory(sub_directory)) { file_path = path;
MAIN_THREAD_EM_ASM({ }
if ((typeof process !== 'undefined') && (process.release.name === 'node')) { const string directory = filesystem::path(file_path.value()).remove_filename();
const sub_directory = UTF8ToString($0); for (unsigned int character = 1; character < directory.size(); ++character) {
if (!FS.isMountpoint(sub_directory)) { const string sub_directory = directory.substr(0, character);
FS.mkdir(sub_directory); if ((directory[character] == '/') and not filesystem::is_directory(sub_directory)) {
FS.mount(NODEFS, {root: sub_directory}, sub_directory); MAIN_THREAD_EM_ASM({
}; if ((typeof process !== 'undefined') && (process.release.name === 'node')) {
} const sub_directory = UTF8ToString($0);
}, sub_directory.c_str()); if (!FS.isMountpoint(sub_directory)) {
FS.mkdir(sub_directory);
FS.mount(NODEFS, {root: sub_directory}, sub_directory);
};
}
}, sub_directory.c_str());
}
} }
}
#endif #endif
Units.insert_or_assign(currentArgument, async(not parallel ? launch::deferred : launch::async, [currentArgument, &text_provided, &target, &newLines]() { }
Units.insert_or_assign(currentArgument, async(not parallel ? launch::deferred : launch::async, [&, currentArgument, file_path]() {
unit_result resultingPair; unit_result resultingPair;
try { try {
resultingPair.first = Target::forName(target, newLines)->transpileWithTree( resultingPair.first = Target::forName(target, newLines)->transpileWithTree(
parseString(text_provided ? currentArgument : getFileContent(currentArgument.data())) parseString(text_provided ? currentArgument : getFileContent(file_path->string()))
); );
if (not text_provided) outputFileContent(string(currentArgument.substr(0, currentArgument.size() - 6)) + '.' + target, resultingPair.first); if (not text_provided) outputFileContent(filesystem::path(file_path.value()).replace_extension(target).string(), resultingPair.first);
} catch (const Yerbacon::Exception& error) { } catch (const Yerbacon::Exception& error) {
size_t lastSlash = 0; resultingPair.first = file_path.has_value() ? file_path->filename().string() : string();
const size_t position1 = currentArgument.find_last_of('/');
if (cmp_not_equal(position1, string_view::npos)) lastSlash = position1;
if constexpr(filesystem::path::preferred_separator != '/') {
const size_t position2 = currentArgument.find_last_of(filesystem::path::preferred_separator);
if (cmp_not_equal(position2, string_view::npos)) {
lastSlash = max(lastSlash, position2);
}
}
resultingPair.first = currentArgument.substr(lastSlash + 1);
resultingPair.second.emplace(error); resultingPair.second.emplace(error);
} }
return resultingPair; return resultingPair;