| 305 | { |
| 306 | #ifdef __USING_WINDOWS__ |
| 307 | inline void quote_argument(const std::wstring &argument, std::wstring &command_line, |
| 308 | bool force) |
| 309 | { |
| 310 | // |
| 311 | // Unless we're told otherwise, don't quote unless we actually |
| 312 | // need to do so --- hopefully avoid problems if programs won't |
| 313 | // parse quotes properly |
| 314 | // |
| 315 | |
| 316 | if (force == false && argument.empty() == false && |
| 317 | argument.find_first_of(L" \t\n\v") == argument.npos) { |
| 318 | command_line.append(argument); |
| 319 | } |
| 320 | else { |
| 321 | command_line.push_back(L'"'); |
| 322 | |
| 323 | for (auto it = argument.begin();; ++it) { |
| 324 | unsigned number_backslashes = 0; |
| 325 | |
| 326 | while (it != argument.end() && *it == L'\\') { |
| 327 | ++it; |
| 328 | ++number_backslashes; |
| 329 | } |
| 330 | |
| 331 | if (it == argument.end()) { |
| 332 | |
| 333 | // |
| 334 | // Escape all backslashes, but let the terminating |
| 335 | // double quotation mark we add below be interpreted |
| 336 | // as a metacharacter. |
| 337 | // |
| 338 | |
| 339 | command_line.append(number_backslashes * 2, L'\\'); |
| 340 | break; |
| 341 | } |
| 342 | else if (*it == L'"') { |
| 343 | |
| 344 | // |
| 345 | // Escape all backslashes and the following |
| 346 | // double quotation mark. |
| 347 | // |
| 348 | |
| 349 | command_line.append(number_backslashes * 2 + 1, L'\\'); |
| 350 | command_line.push_back(*it); |
| 351 | } |
| 352 | else { |
| 353 | |
| 354 | // |
| 355 | // Backslashes aren't special here. |
| 356 | // |
| 357 | |
| 358 | command_line.append(number_backslashes, L'\\'); |
| 359 | command_line.push_back(*it); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | command_line.push_back(L'"'); |
| 364 | } |