Chromium Code Reviews| Index: base/command_line.cc |
| diff --git a/base/command_line.cc b/base/command_line.cc |
| index 66b4437dcf171c62d86257de04b2c3996923f620..f34ed9d491cc3e418c3b833154f98e8c21000618 100644 |
| --- a/base/command_line.cc |
| +++ b/base/command_line.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -27,179 +27,142 @@ |
| CommandLine* CommandLine::current_process_commandline_ = NULL; |
| -// Since we use a lazy match, make sure that longer versions (like L"--") |
| -// are listed before shorter versions (like L"-") of similar prefixes. |
| +namespace { |
| +const CommandLine::CharType kSwitchTerminator[] = FILE_PATH_LITERAL("--"); |
| +const CommandLine::CharType kSwitchValueSeparator[] = FILE_PATH_LITERAL("="); |
| +// Since we use a lazy match, make sure that longer versions (like "--") |
| +// are listed before shorter versions (like "-") of similar prefixes. |
| #if defined(OS_WIN) |
| -const wchar_t* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
| -const wchar_t kSwitchTerminator[] = L"--"; |
| -const wchar_t kSwitchValueSeparator[] = L"="; |
| +const CommandLine::CharType* const kSwitchPrefixes[] = {L"--", L"-", L"/"}; |
| #elif defined(OS_POSIX) |
| // Unixes don't use slash as a switch. |
| -const char* const kSwitchPrefixes[] = {"--", "-"}; |
| -const char kSwitchTerminator[] = "--"; |
| -const char kSwitchValueSeparator[] = "="; |
| +const CommandLine::CharType* const kSwitchPrefixes[] = {"--", "-"}; |
| #endif |
| -#if defined(OS_WIN) |
| -// Lowercase a string. This is used to lowercase switch names. |
| -// Is this what we really want? It seems crazy to me. I've left it in |
| -// for backwards compatibility on Windows. |
| -static void Lowercase(std::string* parameter) { |
| - transform(parameter->begin(), parameter->end(), parameter->begin(), |
| - tolower); |
| -} |
| -#endif |
| - |
| -CommandLine::~CommandLine() { |
| -} |
| - |
| -#if defined(OS_WIN) |
| -CommandLine::CommandLine(NoProgram no_program) { |
| -} |
| +void Lowercase(string* arg); |
| -void CommandLine::ParseFromString(const std::wstring& command_line) { |
| - TrimWhitespace(command_line, TRIM_ALL, &command_line_string_); |
| - |
| - if (command_line_string_.empty()) |
| - return; |
| - |
| - int num_args = 0; |
| - wchar_t** args = NULL; |
| - |
| - args = CommandLineToArgvW(command_line_string_.c_str(), &num_args); |
| - |
| - // Populate program_ with the trimmed version of the first arg. |
| - TrimWhitespace(args[0], TRIM_ALL, &program_); |
| - |
| - bool parse_switches = true; |
| - for (int i = 1; i < num_args; ++i) { |
| - std::wstring arg; |
| - TrimWhitespace(args[i], TRIM_ALL, &arg); |
| - |
| - if (!parse_switches) { |
| - args_.push_back(arg); |
| - continue; |
| - } |
| +// Returns true and fills in |switch_string| and |switch_value| if |
| +// |parameter_string| represents a switch. |
| +bool IsSwitch(const CommandLine::StringType& parameter_string, |
| + string* switch_string, |
| + CommandLine::StringType* switch_value) { |
| + switch_string->clear(); |
| + switch_value->clear(); |
|
evanm
2011/02/24 01:01:05
It's hard to follow what's gone on in this file.
msw
2011/05/10 23:18:43
Done (move was r76339, this change is cleaner).
|
| - if (arg == kSwitchTerminator) { |
| - parse_switches = false; |
| + for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { |
| + CommandLine::StringType prefix(kSwitchPrefixes[i]); |
| + if (parameter_string.find(prefix) != 0) |
| continue; |
| - } |
| - std::string switch_string; |
| - std::wstring switch_value; |
| - if (IsSwitch(arg, &switch_string, &switch_value)) { |
| - switches_[switch_string] = switch_value; |
| + const size_t switch_start = prefix.length(); |
| + const size_t equals_position = parameter_string.find( |
| + kSwitchValueSeparator, switch_start); |
| + CommandLine::StringType switch_native; |
| + if (equals_position == CommandLine::StringType::npos) { |
| + switch_native = parameter_string.substr(switch_start); |
| } else { |
| - args_.push_back(arg); |
| + switch_native = parameter_string.substr( |
| + switch_start, equals_position - switch_start); |
| + *switch_value = parameter_string.substr(equals_position + 1); |
| } |
| - } |
| - |
| - if (args) |
| - LocalFree(args); |
| -} |
| - |
| -// static |
| -CommandLine CommandLine::FromString(const std::wstring& command_line) { |
| - CommandLine cmd; |
| - cmd.ParseFromString(command_line); |
| - return cmd; |
| -} |
| +#if defined(OS_WIN) |
| + *switch_string = WideToASCII(switch_native); |
| +#else |
| + *switch_string = switch_native; |
| +#endif |
| + Lowercase(switch_string); |
| -CommandLine::CommandLine(const FilePath& program) { |
| - if (!program.empty()) { |
| - program_ = program.value(); |
| - // TODO(evanm): proper quoting here. |
| - command_line_string_ = L'"' + program.value() + L'"'; |
| + return true; |
| } |
| -} |
| -#elif defined(OS_POSIX) |
| -CommandLine::CommandLine(NoProgram no_program) { |
| - // Push an empty argument, because we always assume argv_[0] is a program. |
| - argv_.push_back(""); |
| + return false; |
| } |
| -CommandLine::CommandLine(int argc, const char* const* argv) { |
| - InitFromArgv(argc, argv); |
| +#if defined(OS_WIN) |
| +// Lowercase a string for case-insensitivity of switches *on Windows*. |
| +// Is this desirable? It exists for backwards compatibility on Windows. |
| +void Lowercase(string* arg) { |
| + transform(arg->begin(), arg->end(), arg->begin(), tolower); |
| } |
| -CommandLine::CommandLine(const std::vector<std::string>& argv) { |
| - InitFromArgv(argv); |
| +CommandLine::StringType Native(const string& string) { |
| + return ASCIIToWide(string); |
| } |
| -void CommandLine::InitFromArgv(int argc, const char* const* argv) { |
| - for (int i = 0; i < argc; ++i) |
| - argv_.push_back(argv[i]); |
| - InitFromArgv(argv_); |
| -} |
| +CommandLine::StringType Native(const std::wstring& string) { return string; } |
| -void CommandLine::InitFromArgv(const std::vector<std::string>& argv) { |
| - argv_ = argv; |
| - bool parse_switches = true; |
| - for (size_t i = 1; i < argv_.size(); ++i) { |
| - const std::string& arg = argv_[i]; |
| +// Quote a string if necessary *on Windows*, such that CommandLineToArgvW() will |
| +// always process it as a single argument. |
| +CommandLine::StringType Quote(const CommandLine::StringType& arg) { |
| + // We follow the quoting rules of CommandLineToArgvW. |
| + // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
| + if (arg.find_first_of(L" \\\"") == CommandLine::StringType::npos) { |
| + // No quoting necessary. |
| + return arg; |
| + } |
| - if (!parse_switches) { |
| - args_.push_back(arg); |
| - continue; |
| - } |
| + std::wstring out; |
| + out.push_back(L'"'); |
| + for (size_t i = 0; i < arg.size(); ++i) { |
| + if (arg[i] == '\\') { |
| + // Find the extent of this run of backslashes. |
| + size_t start = i, end = start + 1; |
| + for (; end < arg.size() && arg[end] == '\\'; ++end) |
| + /* empty */; |
| + size_t backslash_count = end - start; |
| - if (arg == kSwitchTerminator) { |
| - parse_switches = false; |
| - continue; |
| - } |
| + // Backslashes are escapes only if the run is followed by a double quote. |
| + // Since we also will end the string with a double quote, we escape for |
| + // either a double quote or the end of the string. |
| + if (end == arg.size() || arg[end] == '"') { |
| + // To quote, we need to output 2x as many backslashes. |
| + backslash_count *= 2; |
| + } |
| + for (size_t j = 0; j < backslash_count; ++j) |
| + out.push_back('\\'); |
| - std::string switch_string; |
| - std::string switch_value; |
| - if (IsSwitch(arg, &switch_string, &switch_value)) { |
| - switches_[switch_string] = switch_value; |
| + // Advance i to one before the end to balance i++ in loop. |
| + i = end - 1; |
| + } else if (arg[i] == '"') { |
| + out.push_back('\\'); |
| + out.push_back('"'); |
| } else { |
| - args_.push_back(arg); |
| + out.push_back(arg[i]); |
| } |
| } |
| + out.push_back('"'); |
| + |
| + return out; |
| } |
| +#elif defined(OS_POSIX) |
| +void Lowercase(string* arg) {} |
| -CommandLine::CommandLine(const FilePath& program) { |
| - argv_.push_back(program.value()); |
| +CommandLine::StringType Native(const string& string) { return string; } |
| + |
| +CommandLine::StringType Native(const std::wstring& string) { |
| + return WideToASCII(string); |
| } |
| +CommandLine::StringType Quote(const CommandLine::StringType& string) { |
| + return string; |
| +} |
| #endif |
| +} // namespace |
| -// static |
| -bool CommandLine::IsSwitch(const StringType& parameter_string, |
| - std::string* switch_string, |
| - StringType* switch_value) { |
| - switch_string->clear(); |
| - switch_value->clear(); |
| - |
| - for (size_t i = 0; i < arraysize(kSwitchPrefixes); ++i) { |
| - StringType prefix(kSwitchPrefixes[i]); |
| - if (parameter_string.find(prefix) != 0) |
| - continue; |
| +CommandLine::CommandLine(NoProgram no_program) { |
| + SetProgram(StringType()); |
| +} |
| - const size_t switch_start = prefix.length(); |
| - const size_t equals_position = parameter_string.find( |
| - kSwitchValueSeparator, switch_start); |
| - StringType switch_native; |
| - if (equals_position == StringType::npos) { |
| - switch_native = parameter_string.substr(switch_start); |
| - } else { |
| - switch_native = parameter_string.substr( |
| - switch_start, equals_position - switch_start); |
| - *switch_value = parameter_string.substr(equals_position + 1); |
| - } |
| -#if defined(OS_WIN) |
| - *switch_string = WideToASCII(switch_native); |
| - Lowercase(switch_string); |
| -#else |
| - *switch_string = switch_native; |
| -#endif |
| +CommandLine::CommandLine(const FilePath& program) { |
| + SetProgram(program.value()); |
| +} |
| - return true; |
| - } |
| +CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) { |
| + InitFromArgv(argc, argv); |
| +} |
| - return false; |
| +CommandLine::CommandLine(const StringVector& argv) { |
| + InitFromArgv(argv); |
| } |
| // static |
| @@ -213,8 +176,9 @@ void CommandLine::Init(int argc, const char* const* argv) { |
| #endif |
| } |
| +// static |
| void CommandLine::Reset() { |
| - DCHECK(current_process_commandline_ != NULL); |
| + DCHECK(current_process_commandline_); |
| delete current_process_commandline_; |
| current_process_commandline_ = NULL; |
| } |
| @@ -225,17 +189,57 @@ CommandLine* CommandLine::ForCurrentProcess() { |
| return current_process_commandline_; |
| } |
| -bool CommandLine::HasSwitch(const std::string& switch_string) const { |
| - std::string lowercased_switch(switch_string); |
| #if defined(OS_WIN) |
| - Lowercase(&lowercased_switch); |
| +// static |
| +CommandLine CommandLine::FromString( |
| + const CommandLine::StringType& command_line) { |
| + CommandLine cmd; |
| + cmd.ParseFromString(command_line); |
| + return cmd; |
| +} |
| #endif |
| + |
| +void CommandLine::InitFromArgv(int argc, |
| + const CommandLine::CharType* const* argv) { |
| + StringVector new_argv; |
| + for (int i = 0; i < argc; ++i) |
| + new_argv.push_back(Native(argv[i])); |
| + InitFromArgv(new_argv); |
| +} |
| + |
| +void CommandLine::InitFromArgv(const StringVector argv) { |
| + argv_.clear(); |
| + divider_ = 0; |
| + if (argv.size() <= 0) |
| + return; |
| + |
| + // Initialize the first program argument. |
| + SetProgram(argv[0]); |
| + |
| + // Copy switches/arguments to our vector, keeping switches before arguments. |
| + bool seen_kSwitchTerminator = false; |
| + for (size_t i = 1; i < argv.size(); ++i) { |
| + StringType arg = argv[i]; |
| + TrimWhitespace(arg, TRIM_ALL, &arg); |
| + |
| + string switch_string; |
| + StringType switch_value; |
| + seen_kSwitchTerminator |= arg == kSwitchTerminator; |
| + if (!seen_kSwitchTerminator && IsSwitch(arg, &switch_string, &switch_value)) |
| + AppendSwitchNative(switch_string, switch_value); |
| + else |
| + AppendArgNative(arg); |
| + } |
| +} |
| + |
| +bool CommandLine::HasSwitch(const string& switch_string) const { |
| + std::string lowercased_switch(switch_string); |
| + Lowercase(&lowercased_switch); |
| return switches_.find(lowercased_switch) != switches_.end(); |
| } |
| -std::string CommandLine::GetSwitchValueASCII( |
| - const std::string& switch_string) const { |
| - CommandLine::StringType value = GetSwitchValueNative(switch_string); |
| +string CommandLine::GetSwitchValueASCII(const string& switch_string) const { |
| + StringType value = GetSwitchValueNative(switch_string); |
| if (!IsStringASCII(value)) { |
| LOG(WARNING) << "Value of --" << switch_string << " must be ASCII."; |
| return ""; |
| @@ -247,217 +251,151 @@ std::string CommandLine::GetSwitchValueASCII( |
| #endif |
| } |
| -FilePath CommandLine::GetSwitchValuePath( |
| - const std::string& switch_string) const { |
| +FilePath CommandLine::GetSwitchValuePath(const string& switch_string) const { |
| return FilePath(GetSwitchValueNative(switch_string)); |
| } |
| CommandLine::StringType CommandLine::GetSwitchValueNative( |
| - const std::string& switch_string) const { |
| + const string& switch_string) const { |
| std::string lowercased_switch(switch_string); |
| -#if defined(OS_WIN) |
| Lowercase(&lowercased_switch); |
| -#endif |
| + SwitchMap::const_iterator result = switches_.find(lowercased_switch); |
| + return result == switches_.end() ? StringType() : result->second; |
| +} |
| - std::map<std::string, StringType>::const_iterator result = |
| - switches_.find(lowercased_switch); |
| +size_t CommandLine::GetSwitchCount() const { |
| + return switches_.size(); |
| +} |
| - if (result == switches_.end()) { |
| - return CommandLine::StringType(); |
| - } else { |
| - return result->second; |
| - } |
| +CommandLine::StringVector CommandLine::args() const { |
| + // Gather all arguments after the last switch (may include kSwitchTerminator). |
| + StringVector args(argv_.begin() + divider_, argv_.end()); |
| + |
| + // Erase only the first kSwitchTerminator (maybe "--" is a legitimate page?) |
| + StringVector::iterator it = args.begin(); |
| + for (; it < args.end() && *it != kSwitchTerminator; ++it) |
| + /* empty */; |
| + if (it != args.end()) |
| + args.erase(it); |
| + |
| + return args; |
| +} |
| + |
| +CommandLine::StringType CommandLine::command_line_string() const { |
| + return JoinString(argv_, FILE_PATH_LITERAL(' ')); |
| } |
| FilePath CommandLine::GetProgram() const { |
| #if defined(OS_WIN) |
| - return FilePath(program_); |
| -#else |
| - DCHECK_GT(argv_.size(), 0U); |
| - return FilePath(argv_[0]); |
| + // Ensure that quotes are not returned from GetProgram(). |
| + // TODO(evanm): proper quoting/handling here. |
| + return FilePath(argv_.size() > 0 ? argv_[0].substr(1, argv_[0].size() - 2) : |
| + StringType()); |
| #endif |
| + return FilePath(argv_.size() > 0 ? argv_[0] : StringType()); |
| } |
| -#if defined(OS_POSIX) |
| -std::string CommandLine::command_line_string() const { |
| - return JoinString(argv_, ' '); |
| -} |
| -#endif |
| +void CommandLine::SetProgram(const CommandLine::StringType& program) { |
| + CommandLine::StringType program_string(program); |
| + TrimWhitespace(program_string, TRIM_ALL, &program_string); |
| #if defined(OS_WIN) |
| -void CommandLine::AppendSwitch(const std::string& switch_string) { |
| - command_line_string_.append(L" "); |
| - command_line_string_.append(kSwitchPrefixes[0] + ASCIIToWide(switch_string)); |
| - switches_[switch_string] = L""; |
| -} |
| - |
| -void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| - const std::string& value_string) { |
| - AppendSwitchNative(switch_string, ASCIIToWide(value_string)); |
| -} |
| + // TODO(evanm): proper quoting here. |
| + if (!program_string.empty()) |
| + program_string = L'"' + program + L'"'; |
| +#endif |
| -// Quote a string if necessary, such that CommandLineToArgvW() will |
| -// always process it as a single argument. |
| -static std::wstring WindowsStyleQuote(const std::wstring& arg) { |
| - // We follow the quoting rules of CommandLineToArgvW. |
| - // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
| - if (arg.find_first_of(L" \\\"") == std::wstring::npos) { |
| - // No quoting necessary. |
| - return arg; |
| + if (argv_.size() > 0){ |
| + argv_[0] = program_string; |
| + } else { |
| + argv_.push_back(program_string); |
| + divider_ = 1; |
| } |
| +} |
| - std::wstring out; |
| - out.push_back(L'"'); |
| - for (size_t i = 0; i < arg.size(); ++i) { |
| - if (arg[i] == '\\') { |
| - // Find the extent of this run of backslashes. |
| - size_t start = i, end = start + 1; |
| - for (; end < arg.size() && arg[end] == '\\'; ++end) |
| - /* empty */; |
| - size_t backslash_count = end - start; |
| - |
| - // Backslashes are escapes only if the run is followed by a double quote. |
| - // Since we also will end the string with a double quote, we escape for |
| - // either a double quote or the end of the string. |
| - if (end == arg.size() || arg[end] == '"') { |
| - // To quote, we need to output 2x as many backslashes. |
| - backslash_count *= 2; |
| - } |
| - for (size_t j = 0; j < backslash_count; ++j) |
| - out.push_back('\\'); |
| - |
| - // Advance i to one before the end to balance i++ in loop. |
| - i = end - 1; |
| - } else if (arg[i] == '"') { |
| - out.push_back('\\'); |
| - out.push_back('"'); |
| - } else { |
| - out.push_back(arg[i]); |
| - } |
| - } |
| - out.push_back('"'); |
| +void CommandLine::AppendSwitch(const string& switch_string) { |
| + AppendSwitchNative(switch_string, CommandLine::StringType()); |
| +} |
| - return out; |
| +void CommandLine::AppendSwitchPath(const string& switch_string, |
| + const FilePath& path) { |
| + AppendSwitchNative(switch_string, path.value()); |
| } |
| -void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| - const std::wstring& value) { |
| - std::wstring combined_switch_string = |
| - kSwitchPrefixes[0] + ASCIIToWide(switch_string); |
| +void CommandLine::AppendSwitchNative(const string& switch_string, |
| + const CommandLine::StringType& value) { |
| + CommandLine::StringType combined = kSwitchPrefixes[0] + Native(switch_string); |
| if (!value.empty()) |
| - combined_switch_string += kSwitchValueSeparator + WindowsStyleQuote(value); |
| - |
| - command_line_string_.append(L" "); |
| - command_line_string_.append(combined_switch_string); |
| - |
| + combined += kSwitchValueSeparator + Quote(value); |
| + // Append the switch and update the switches/arguments |divider_|. |
| + argv_.insert(argv_.begin() + divider_++, combined); |
| switches_[switch_string] = value; |
| } |
| -void CommandLine::AppendArg(const std::string& value) { |
| - DCHECK(IsStringUTF8(value)); |
| - AppendArgNative(UTF8ToWide(value)); |
| -} |
| - |
| -void CommandLine::AppendArgNative(const std::wstring& value) { |
| - command_line_string_.append(L" "); |
| - command_line_string_.append(WindowsStyleQuote(value)); |
| - args_.push_back(value); |
| +void CommandLine::AppendSwitchASCII(const string& switch_string, |
| + const string& value_string) { |
| + AppendSwitchNative(switch_string, Native(value_string)); |
| } |
| -void CommandLine::AppendArguments(const CommandLine& other, |
| - bool include_program) { |
| - // Verify include_program is used correctly. |
| - // Logic could be shorter but this is clearer. |
| - DCHECK_EQ(include_program, !other.GetProgram().empty()); |
| - if (include_program) |
| - program_ = other.program_; |
| - |
| - if (!command_line_string_.empty()) |
| - command_line_string_ += L' '; |
| - |
| - command_line_string_ += other.command_line_string_; |
| - |
| - std::map<std::string, StringType>::const_iterator i; |
| +void CommandLine::AppendSwitches(const CommandLine& other) { |
| + SwitchMap::const_iterator i; |
| for (i = other.switches_.begin(); i != other.switches_.end(); ++i) |
| - switches_[i->first] = i->second; |
| -} |
| - |
| -void CommandLine::PrependWrapper(const std::wstring& wrapper) { |
| - if (wrapper.empty()) |
| - return; |
| - // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| - // we don't pretend to do anything fancy, we just split on spaces. |
| - std::vector<std::wstring> wrapper_and_args; |
| - base::SplitString(wrapper, ' ', &wrapper_and_args); |
| - program_ = wrapper_and_args[0]; |
| - command_line_string_ = wrapper + L" " + command_line_string_; |
| + AppendSwitchNative(i->first, i->second); |
| } |
| +void CommandLine::AppendArg(const string& value) { |
| +#if defined(OS_WIN) |
| + DCHECK(IsStringUTF8(value)); |
| + AppendArgNative(UTF8ToWide(value)); |
| #elif defined(OS_POSIX) |
| -void CommandLine::AppendSwitch(const std::string& switch_string) { |
| - argv_.push_back(kSwitchPrefixes[0] + switch_string); |
| - switches_[switch_string] = ""; |
| -} |
| - |
| -void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| - const std::string& value) { |
| - std::string combined_switch_string = kSwitchPrefixes[0] + switch_string; |
| - if (!value.empty()) |
| - combined_switch_string += kSwitchValueSeparator + value; |
| - argv_.push_back(combined_switch_string); |
| - switches_[switch_string] = value; |
| -} |
| - |
| -void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| - const std::string& value_string) { |
| - AppendSwitchNative(switch_string, value_string); |
| + AppendArgNative(value); |
| +#endif |
| } |
| -void CommandLine::AppendArg(const std::string& value) { |
| - AppendArgNative(value); |
| +void CommandLine::AppendArgPath(const FilePath& path) { |
| + AppendArgNative(path.value()); |
| } |
| -void CommandLine::AppendArgNative(const std::string& value) { |
| +void CommandLine::AppendArgNative(const CommandLine::StringType& value) { |
| +#if defined(OS_POSIX) |
| DCHECK(IsStringUTF8(value)); |
| - argv_.push_back(value); |
| +#endif |
| + argv_.push_back(Quote(value)); |
| } |
| void CommandLine::AppendArguments(const CommandLine& other, |
| bool include_program) { |
| // Verify include_program is used correctly. |
| - // Logic could be shorter but this is clearer. |
| - DCHECK_EQ(include_program, !other.GetProgram().empty()); |
| - |
| + DCHECK(!include_program || other.argv_.size() > 0); |
| if (include_program) |
| - argv_[0] = other.argv_[0]; |
| - |
| - // Skip the first arg when copying since it's the program but push all |
| - // arguments to our arg vector. |
| - for (size_t i = 1; i < other.argv_.size(); ++i) |
| - argv_.push_back(other.argv_[i]); |
| - |
| - std::map<std::string, StringType>::const_iterator i; |
| - for (i = other.switches_.begin(); i != other.switches_.end(); ++i) |
| - switches_[i->first] = i->second; |
| + SetProgram(other.GetProgram().value()); |
| + |
| + // Copy switches/arguments to our vector, keeping switches before arguments. |
| + bool seen_kSwitchTerminator = false; |
| + for (size_t i = 1; i < other.argv_.size(); ++i) { |
| + StringType arg = other.argv_[i]; |
| + TrimWhitespace(arg, TRIM_ALL, &arg); |
| + |
| + string switch_string; |
| + StringType switch_value; |
| + seen_kSwitchTerminator |= arg == kSwitchTerminator; |
| + if (!seen_kSwitchTerminator && IsSwitch(arg, &switch_string, &switch_value)) |
| + AppendSwitchNative(switch_string, switch_value); |
| + else |
| + AppendArgNative(arg); |
| + } |
| } |
| -void CommandLine::PrependWrapper(const std::string& wrapper) { |
| +void CommandLine::PrependWrapper(const StringType& wrapper) { |
| + if (wrapper.empty()) |
| + return; |
| // The wrapper may have embedded arguments (like "gdb --args"). In this case, |
| // we don't pretend to do anything fancy, we just split on spaces. |
| - std::vector<std::string> wrapper_and_args; |
| - base::SplitString(wrapper, ' ', &wrapper_and_args); |
| - argv_.insert(argv_.begin(), wrapper_and_args.begin(), wrapper_and_args.end()); |
| -} |
| - |
| -#endif |
| - |
| -void CommandLine::AppendArgPath(const FilePath& path) { |
| - AppendArgNative(path.value()); |
| -} |
| - |
| -void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| - const FilePath& path) { |
| - AppendSwitchNative(switch_string, path.value()); |
| + StringVector prefix; |
| + base::SplitString(wrapper, FILE_PATH_LITERAL(' '), &prefix); |
| + // Prepend the wrapper and update the switches/arguments |divider_|. |
| + argv_.insert(argv_.begin(), prefix.begin(), prefix.end()); |
| + divider_ += prefix.size(); |
| } |
| void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| @@ -471,12 +409,19 @@ void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| } |
| } |
| -// private |
| -CommandLine::CommandLine() { |
| -} |
| +#if defined(OS_WIN) |
| +void CommandLine::ParseFromString(const CommandLine::StringType& command_line) { |
| + StringType command_line_string; |
| + TrimWhitespace(command_line, TRIM_ALL, &command_line_string); |
| + if (command_line_string.empty()) |
| + return; |
| -// static |
| -CommandLine* CommandLine::ForCurrentProcessMutable() { |
| - DCHECK(current_process_commandline_); |
| - return current_process_commandline_; |
| + int argc = 0; |
| + wchar_t** argv = NULL; |
| + argv = ::CommandLineToArgvW(command_line_string.c_str(), &argc); |
| + |
| + CHECK(argv); |
| + InitFromArgv(argc, argv); |
| + LocalFree(argv); |
| } |
| +#endif |