| 10 | #include <vector> |
| 11 | |
| 12 | std::string escape_arg(const std::string& arg) { |
| 13 | if (arg.empty() == false && |
| 14 | arg.find_first_of(" \t\n\v\"") == arg.npos) { |
| 15 | return arg; |
| 16 | } |
| 17 | |
| 18 | std::string escaped; |
| 19 | escaped.push_back('"'); |
| 20 | for (auto it = arg.begin(); ; ++it) { |
| 21 | int num_backslashes = 0; |
| 22 | |
| 23 | while (it != arg.end() && *it == '\\') { |
| 24 | ++it; |
| 25 | ++num_backslashes; |
| 26 | } |
| 27 | |
| 28 | if (it == arg.end()) { |
| 29 | escaped.append(num_backslashes * 2, '\\'); |
| 30 | break; |
| 31 | } else if (*it == '"') { |
| 32 | escaped.append((num_backslashes + 1) * 2, '\\'); |
| 33 | escaped.push_back('"'); |
| 34 | escaped.push_back(*it); |
| 35 | } else { |
| 36 | escaped.append(num_backslashes, '\\'); |
| 37 | escaped.push_back(*it); |
| 38 | } |
| 39 | } |
| 40 | escaped.push_back('"'); |
| 41 | |
| 42 | return escaped; |
| 43 | } |
| 44 | |
| 45 | |
| 46 | void create_empty_file(std::string const& path) { |