2019-05-15 11:53:18 -07:00
|
|
|
#include <exception>
|
2019-05-15 12:08:33 -07:00
|
|
|
#include <iostream>
|
2019-05-15 11:53:18 -07:00
|
|
|
#include <string>
|
2019-05-15 12:08:33 -07:00
|
|
|
#include <vector>
|
2019-05-15 11:53:18 -07:00
|
|
|
#include <regex>
|
|
|
|
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
|
|
|
#include "output.hpp"
|
|
|
|
#include "paths.hpp"
|
2019-05-15 12:08:33 -07:00
|
|
|
#include "internal/core.hpp"
|
2019-05-15 11:53:18 -07:00
|
|
|
|
2019-05-15 12:08:33 -07:00
|
|
|
using namespace std;
|
2019-05-15 11:53:18 -07:00
|
|
|
using namespace cellar;
|
|
|
|
|
2019-05-15 12:08:33 -07:00
|
|
|
string cellar::paths::translate(std::string in_path, bool lazy) {
|
|
|
|
bool windows_input;
|
2019-05-15 11:53:18 -07:00
|
|
|
|
2019-05-15 12:08:33 -07:00
|
|
|
static regex drive_letter(R"([a-zA-Z]:\\)");
|
2019-05-15 11:53:18 -07:00
|
|
|
|
2019-05-15 12:08:33 -07:00
|
|
|
windows_input = regex_match(in_path.substr(0, 3), drive_letter);
|
2019-05-15 11:53:18 -07:00
|
|
|
|
|
|
|
if (!lazy) {
|
|
|
|
output::warning("non-lazy path translation is not implemented yet");
|
|
|
|
}
|
|
|
|
|
2019-05-15 12:08:33 -07:00
|
|
|
if (windows_input) {
|
2019-05-15 11:53:18 -07:00
|
|
|
// lazy
|
|
|
|
if (boost::algorithm::to_lower_copy(in_path.substr(0,1)) != "z") {
|
2019-05-15 12:08:33 -07:00
|
|
|
throw invalid_argument("lazy path translation isn't possible for drive letters other than Z:");
|
2019-05-15 11:53:18 -07:00
|
|
|
}
|
2019-05-15 12:08:33 -07:00
|
|
|
string out_path = in_path.substr(2, in_path.size() - 2);
|
2019-05-15 11:53:18 -07:00
|
|
|
|
|
|
|
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
|
2019-05-15 12:08:33 -07:00
|
|
|
string out_path = "Z:";
|
2019-05-15 11:53:18 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-05-15 12:08:33 -07:00
|
|
|
|
|
|
|
void cellar::core::translate_command(int argc, vector<string> argv) {
|
|
|
|
if (argv.size() < 2) {
|
|
|
|
output::error("pass an argument");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: tclap me later
|
|
|
|
vector<string> pathargs(argv.cbegin() + 1, argv.cend());
|
|
|
|
|
|
|
|
for (auto arg : pathargs) {
|
|
|
|
cout << paths::translate(arg) << endl;
|
|
|
|
}
|
|
|
|
}
|