diff options
Diffstat (limited to 'src/porting.cpp')
-rw-r--r-- | src/porting.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/porting.cpp b/src/porting.cpp index 475292b97..629f00c37 100644 --- a/src/porting.cpp +++ b/src/porting.cpp @@ -729,6 +729,38 @@ void attachOrCreateConsole() #endif } +#ifdef _WIN32 +std::string QuoteArgv(const std::string &arg) +{ + // Quoting rules on Windows are batshit insane, can differ between applications + // and there isn't even a stdlib function to deal with it. + // Ref: https://learn.microsoft.com/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way + if (!arg.empty() && arg.find_first_of(" \t\n\v\"") == std::string::npos) + return arg; + + std::string ret; + ret.reserve(arg.size()+2); + ret.push_back('"'); + for (auto it = arg.begin(); it != arg.end(); ++it) { + u32 back = 0; + while (it != arg.end() && *it == '\\') + ++back, ++it; + + if (it == arg.end()) { + ret.append(2 * back, '\\'); + break; + } else if (*it == '"') { + ret.append(2 * back + 1, '\\'); + } else { + ret.append(back, '\\'); + } + ret.push_back(*it); + } + ret.push_back('"'); + return ret; +} +#endif + int mt_snprintf(char *buf, const size_t buf_size, const char *fmt, ...) { // https://msdn.microsoft.com/en-us/library/bt7tawza.aspx |