diff --git a/include/paths.hpp b/include/paths.hpp new file mode 100644 index 0000000..2eb1e3d --- /dev/null +++ b/include/paths.hpp @@ -0,0 +1,9 @@ +#pragma once + +#include + +namespace cellar { + namespace paths { + extern std::string translate(std::string in_path, bool lazy = false); + } +} diff --git a/src/core/paths.cpp b/src/core/paths.cpp new file mode 100644 index 0000000..072e112 --- /dev/null +++ b/src/core/paths.cpp @@ -0,0 +1,50 @@ +#include +#include +#include + +#include + +#include "output.hpp" +#include "paths.hpp" + +using namespace cellar; + +std::string cellar::paths::translate(std::string in_path, bool lazy) { + bool posix_input; + + static std::regex drive_letter(R"([a-zA-Z]:\\)"); + + posix_input = ! std::regex_match(in_path.substr(0, 3), drive_letter); + + if (!lazy) { + output::warning("non-lazy path translation is not implemented yet"); + } + + if (posix_input) { + // lazy + if (boost::algorithm::to_lower_copy(in_path.substr(0,1)) != "z") { + throw std::invalid_argument("lazy path translation isn't possible for drive letters other than Z:"); + } + std::string out_path = in_path.substr(2, in_path.size() - 2); + + size_t slashpos = out_path.find("\\"); + while (slashpos != std::string::npos) { + out_path.replace(slashpos, 1, "/"); + slashpos = out_path.find("\\"); + } + + return out_path; + } else { + // lazy + std::string out_path = "Z:"; + out_path.append(in_path); + + size_t slashpos = out_path.find("/"); + while (slashpos != std::string::npos) { + out_path.replace(slashpos, 1, "\\"); + slashpos = out_path.find("/"); + } + + return out_path; + } +}