From 64ef5e60ff2c9838bc31761d875bb655f85c9b20 Mon Sep 17 00:00:00 2001 From: Username404 Date: Sun, 4 Dec 2022 22:24:37 +0100 Subject: [PATCH] main.cpp: Support Windows paths on node.js Signed-off-by: Username404 --- src/main.cpp | 54 ++++++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 56fdfa0..b5597c6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,41 +42,41 @@ int main(int argc, char* argv[]) { #ifdef _OPENMP if (not parallel) omp_set_num_threads(1); #endif + optional file_path = make_optional(); + if (not text_provided) { + file_path = filesystem::path(currentArgument); #ifdef EMSCRIPTEN - const string directory (currentArgument.substr(0, currentArgument.find_last_of(filesystem::path::preferred_separator) + 1)); - for (unsigned int i = 1; i < directory.size(); ++i) { - const string sub_directory = directory.substr(0, i); - if (directory[i] == '/' and not filesystem::is_directory(sub_directory)) { - MAIN_THREAD_EM_ASM({ - if ((typeof process !== 'undefined') && (process.release.name === 'node')) { - const sub_directory = UTF8ToString($0); - if (!FS.isMountpoint(sub_directory)) { - FS.mkdir(sub_directory); - FS.mount(NODEFS, {root: sub_directory}, sub_directory); - }; - } - }, sub_directory.c_str()); + if (currentArgument.size() >= 2 and currentArgument[1] == ':') { + auto path = string(currentArgument).erase(0, 2); + replace(path.begin(), path.end(), '\\', '/'); + file_path = path; + } + const string directory = filesystem::path(file_path.value()).remove_filename(); + for (unsigned int character = 1; character < directory.size(); ++character) { + const string sub_directory = directory.substr(0, character); + if ((directory[character] == '/') and not filesystem::is_directory(sub_directory)) { + MAIN_THREAD_EM_ASM({ + if ((typeof process !== 'undefined') && (process.release.name === 'node')) { + const sub_directory = UTF8ToString($0); + if (!FS.isMountpoint(sub_directory)) { + FS.mkdir(sub_directory); + FS.mount(NODEFS, {root: sub_directory}, sub_directory); + }; + } + }, sub_directory.c_str()); + } } - } #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; try { 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) { - size_t lastSlash = 0; - 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.first = file_path.has_value() ? file_path->filename().string() : string(); resultingPair.second.emplace(error); } return resultingPair;