| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shellapi.h> | 9 #include <shellapi.h> |
| 10 #elif defined(OS_POSIX) | 10 #elif defined(OS_POSIX) |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 return HasSwitch(WideToASCII(switch_string)); | 284 return HasSwitch(WideToASCII(switch_string)); |
| 285 } | 285 } |
| 286 | 286 |
| 287 std::string CommandLine::GetSwitchValueASCII( | 287 std::string CommandLine::GetSwitchValueASCII( |
| 288 const std::string& switch_string) const { | 288 const std::string& switch_string) const { |
| 289 return WideToASCII(GetSwitchValue(switch_string)); | 289 return WideToASCII(GetSwitchValue(switch_string)); |
| 290 } | 290 } |
| 291 | 291 |
| 292 FilePath CommandLine::GetSwitchValuePath( | 292 FilePath CommandLine::GetSwitchValuePath( |
| 293 const std::string& switch_string) const { | 293 const std::string& switch_string) const { |
| 294 return FilePath::FromWStringHack(GetSwitchValue(switch_string)); | 294 return FilePath(GetSwitchValueNative(switch_string)); |
| 295 } | 295 } |
| 296 | 296 |
| 297 std::wstring CommandLine::GetSwitchValue( | 297 CommandLine::StringType CommandLine::GetSwitchValueNative( |
| 298 const std::string& switch_string) const { | 298 const std::string& switch_string) const { |
| 299 std::string lowercased_switch(switch_string); | 299 std::string lowercased_switch(switch_string); |
| 300 #if defined(OS_WIN) | 300 #if defined(OS_WIN) |
| 301 Lowercase(&lowercased_switch); | 301 Lowercase(&lowercased_switch); |
| 302 #endif | 302 #endif |
| 303 | 303 |
| 304 std::map<std::string, StringType>::const_iterator result = | 304 std::map<std::string, StringType>::const_iterator result = |
| 305 switches_.find(lowercased_switch); | 305 switches_.find(lowercased_switch); |
| 306 | 306 |
| 307 if (result == switches_.end()) { | 307 if (result == switches_.end()) { |
| 308 return L""; | 308 return CommandLine::StringType(); |
| 309 } else { | 309 } else { |
| 310 #if defined(OS_WIN) | |
| 311 return result->second; | 310 return result->second; |
| 312 #else | |
| 313 return base::SysNativeMBToWide(result->second); | |
| 314 #endif | |
| 315 } | 311 } |
| 316 } | 312 } |
| 317 | 313 |
| 318 std::wstring CommandLine::GetSwitchValue( | 314 std::wstring CommandLine::GetSwitchValue( |
| 315 const std::string& switch_string) const { |
| 316 // TODO(evanm): deprecate. |
| 317 CommandLine::StringType value = GetSwitchValueNative(switch_string); |
| 318 #if defined(OS_WIN) |
| 319 return value; |
| 320 #else |
| 321 return base::SysNativeMBToWide(value); |
| 322 #endif |
| 323 } |
| 324 |
| 325 std::wstring CommandLine::GetSwitchValue( |
| 319 const std::wstring& switch_string) const { | 326 const std::wstring& switch_string) const { |
| 327 // TODO(evanm): deprecate. |
| 320 return GetSwitchValue(WideToASCII(switch_string)); | 328 return GetSwitchValue(WideToASCII(switch_string)); |
| 321 } | 329 } |
| 322 | 330 |
| 323 FilePath CommandLine::GetProgram() const { | 331 FilePath CommandLine::GetProgram() const { |
| 324 return FilePath::FromWStringHack(program()); | 332 return FilePath::FromWStringHack(program()); |
| 325 } | 333 } |
| 326 | 334 |
| 327 #if defined(OS_WIN) | 335 #if defined(OS_WIN) |
| 328 std::wstring CommandLine::program() const { | 336 std::wstring CommandLine::program() const { |
| 329 return program_; | 337 return program_; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 #if defined(OS_WIN) | 378 #if defined(OS_WIN) |
| 371 void CommandLine::AppendSwitch(const std::string& switch_string) { | 379 void CommandLine::AppendSwitch(const std::string& switch_string) { |
| 372 std::wstring prefixed_switch_string = PrefixedSwitchString(switch_string); | 380 std::wstring prefixed_switch_string = PrefixedSwitchString(switch_string); |
| 373 command_line_string_.append(L" "); | 381 command_line_string_.append(L" "); |
| 374 command_line_string_.append(prefixed_switch_string); | 382 command_line_string_.append(prefixed_switch_string); |
| 375 switches_[switch_string] = L""; | 383 switches_[switch_string] = L""; |
| 376 } | 384 } |
| 377 | 385 |
| 378 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, | 386 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, |
| 379 const std::wstring& value_string) { | 387 const std::wstring& value_string) { |
| 388 AppendSwitchNative(switch_string, value_string); |
| 389 } |
| 390 |
| 391 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 392 const std::wstring& value_string) { |
| 380 std::wstring value_string_edit; | 393 std::wstring value_string_edit; |
| 381 | 394 |
| 382 // NOTE(jhughes): If the value contains a quotation mark at one | 395 // NOTE(jhughes): If the value contains a quotation mark at one |
| 383 // end but not both, you may get unusable output. | 396 // end but not both, you may get unusable output. |
| 384 if (!value_string.empty() && | 397 if (!value_string.empty() && |
| 385 (value_string.find(L" ") != std::wstring::npos) && | 398 (value_string.find(L" ") != std::wstring::npos) && |
| 386 (value_string[0] != L'"') && | 399 (value_string[0] != L'"') && |
| 387 (value_string[value_string.length() - 1] != L'"')) { | 400 (value_string[value_string.length() - 1] != L'"')) { |
| 388 // need to provide quotes | 401 // need to provide quotes |
| 389 value_string_edit = StringPrintf(L"\"%ls\"", value_string.c_str()); | 402 value_string_edit = StringPrintf(L"\"%ls\"", value_string.c_str()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 program_ = wrapper_and_args[0]; | 439 program_ = wrapper_and_args[0]; |
| 427 command_line_string_ = wrapper + L" " + command_line_string_; | 440 command_line_string_ = wrapper + L" " + command_line_string_; |
| 428 } | 441 } |
| 429 | 442 |
| 430 #elif defined(OS_POSIX) | 443 #elif defined(OS_POSIX) |
| 431 void CommandLine::AppendSwitch(const std::string& switch_string) { | 444 void CommandLine::AppendSwitch(const std::string& switch_string) { |
| 432 argv_.push_back(kSwitchPrefixes[0] + switch_string); | 445 argv_.push_back(kSwitchPrefixes[0] + switch_string); |
| 433 switches_[switch_string] = ""; | 446 switches_[switch_string] = ""; |
| 434 } | 447 } |
| 435 | 448 |
| 449 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 450 const std::string& value) { |
| 451 argv_.push_back(kSwitchPrefixes[0] + switch_string + |
| 452 kSwitchValueSeparator + value); |
| 453 switches_[switch_string] = value; |
| 454 } |
| 455 |
| 436 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, | 456 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, |
| 437 const std::wstring& value_string) { | 457 const std::wstring& value_string) { |
| 458 // TODO(evanm): deprecate. |
| 438 std::string mb_value = base::SysWideToNativeMB(value_string); | 459 std::string mb_value = base::SysWideToNativeMB(value_string); |
| 439 | 460 AppendSwitchNative(switch_string, mb_value); |
| 440 argv_.push_back(kSwitchPrefixes[0] + switch_string + | |
| 441 kSwitchValueSeparator + mb_value); | |
| 442 switches_[switch_string] = mb_value; | |
| 443 } | 461 } |
| 444 | 462 |
| 445 void CommandLine::AppendLooseValue(const std::wstring& value) { | 463 void CommandLine::AppendLooseValue(const std::wstring& value) { |
| 446 argv_.push_back(base::SysWideToNativeMB(value)); | 464 argv_.push_back(base::SysWideToNativeMB(value)); |
| 447 } | 465 } |
| 448 | 466 |
| 449 void CommandLine::AppendArguments(const CommandLine& other, | 467 void CommandLine::AppendArguments(const CommandLine& other, |
| 450 bool include_program) { | 468 bool include_program) { |
| 451 // Verify include_program is used correctly. | 469 // Verify include_program is used correctly. |
| 452 // Logic could be shorter but this is clearer. | 470 // Logic could be shorter but this is clearer. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 463 // The wrapper may have embedded arguments (like "gdb --args"). In this case, | 481 // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| 464 // we don't pretend to do anything fancy, we just split on spaces. | 482 // we don't pretend to do anything fancy, we just split on spaces. |
| 465 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); | 483 const std::string wrapper = base::SysWideToNativeMB(wrapper_wide); |
| 466 std::vector<std::string> wrapper_and_args; | 484 std::vector<std::string> wrapper_and_args; |
| 467 SplitString(wrapper, ' ', &wrapper_and_args); | 485 SplitString(wrapper, ' ', &wrapper_and_args); |
| 468 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); | 486 argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
| 469 } | 487 } |
| 470 | 488 |
| 471 #endif | 489 #endif |
| 472 | 490 |
| 491 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 492 const FilePath& path) { |
| 493 AppendSwitchNative(switch_string, path.value()); |
| 494 } |
| 495 |
| 473 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, | 496 void CommandLine::AppendSwitchWithValue(const std::string& switch_string, |
| 474 const std::string& value_string) { | 497 const std::string& value_string) { |
| 475 AppendSwitchWithValue(switch_string, ASCIIToWide(value_string)); | 498 AppendSwitchWithValue(switch_string, ASCIIToWide(value_string)); |
| 476 } | 499 } |
| 477 | 500 |
| 501 void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| 502 const char* const switches[], |
| 503 size_t count) { |
| 504 for (size_t i = 0; i < count; ++i) { |
| 505 if (source.HasSwitch(switches[i])) { |
| 506 StringType value = source.GetSwitchValueNative(switches[i]); |
| 507 AppendSwitchNative(switches[i], value); |
| 508 } |
| 509 } |
| 510 } |
| 511 |
| 478 // private | 512 // private |
| 479 CommandLine::CommandLine() { | 513 CommandLine::CommandLine() { |
| 480 } | 514 } |
| OLD | NEW |