Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <ostream> | 8 #include <ostream> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 86 switch_value); | 86 switch_value); |
| 87 #elif defined(OS_POSIX) | 87 #elif defined(OS_POSIX) |
| 88 command_line->AppendSwitchNative(switch_string, switch_value); | 88 command_line->AppendSwitchNative(switch_string, switch_value); |
| 89 #endif | 89 #endif |
| 90 } else { | 90 } else { |
| 91 command_line->AppendArgNative(arg); | 91 command_line->AppendArgNative(arg); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 | 95 |
| 96 // Lowercase switches for backwards compatiblity *on Windows*. | |
| 97 #if defined(OS_WIN) | |
| 98 std::string LowerASCIIOnWindows(const std::string& string) { | |
| 99 return StringToLowerASCII(string); | |
| 100 } | |
| 101 #elif defined(OS_POSIX) | |
| 102 const std::string& LowerASCIIOnWindows(const std::string& string) { | |
| 103 return string; | |
| 104 } | |
| 105 #endif | |
| 106 | |
| 107 | |
| 108 #if defined(OS_WIN) | 96 #if defined(OS_WIN) |
| 109 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. | 97 // Quote a string as necessary for CommandLineToArgvW compatiblity *on Windows*. |
| 110 string16 QuoteForCommandLineToArgvW(const string16& arg, | 98 string16 QuoteForCommandLineToArgvW(const string16& arg, |
| 111 bool quote_placeholders) { | 99 bool quote_placeholders) { |
| 112 // We follow the quoting rules of CommandLineToArgvW. | 100 // We follow the quoting rules of CommandLineToArgvW. |
| 113 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx | 101 // http://msdn.microsoft.com/en-us/library/17w5ykft.aspx |
| 114 string16 quotable_chars(L" \\\""); | 102 string16 quotable_chars(L" \\\""); |
| 115 // We may also be required to quote '%', which is commonly used in a command | 103 // We may also be required to quote '%', which is commonly used in a command |
| 116 // line as a placeholder. (It may be substituted for a string with spaces.) | 104 // line as a placeholder. (It may be substituted for a string with spaces.) |
| 117 if (quote_placeholders) | 105 if (quote_placeholders) |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 148 } else { | 136 } else { |
| 149 out.push_back(arg[i]); | 137 out.push_back(arg[i]); |
| 150 } | 138 } |
| 151 } | 139 } |
| 152 out.push_back('"'); | 140 out.push_back('"'); |
| 153 | 141 |
| 154 return out; | 142 return out; |
| 155 } | 143 } |
| 156 #endif | 144 #endif |
| 157 | 145 |
| 146 // Reconstruct |switches_by_stringpiece| to be a mirror of |switches|. | |
| 147 // |switches_by_stringpiece| only contains pointers to objects owned by | |
| 148 // |switches|. | |
| 149 void ResetStringPieces( | |
|
brettw
2015/04/24 19:50:49
Why not make this a private member function? You c
jackhou1
2015/05/11 13:51:42
Done.
| |
| 150 const CommandLine::SwitchMap& switches, | |
| 151 CommandLine::StringPieceSwitchMap* switches_by_stringpiece) { | |
| 152 switches_by_stringpiece->clear(); | |
| 153 for (auto it = switches.begin(); it != switches.end(); ++it) | |
|
brettw
2015/04/24 19:50:50
auto -> const auto&
Always use const refs when app
jackhou1
2015/05/11 13:51:42
Done.
| |
| 154 (*switches_by_stringpiece)[it->first] = &(it->second); | |
| 155 } | |
| 156 | |
| 158 } // namespace | 157 } // namespace |
| 159 | 158 |
| 160 CommandLine::CommandLine(NoProgram no_program) | 159 CommandLine::CommandLine(NoProgram no_program) |
| 161 : argv_(1), | 160 : argv_(1), |
| 162 begin_args_(1) { | 161 begin_args_(1) { |
| 163 } | 162 } |
| 164 | 163 |
| 165 CommandLine::CommandLine(const FilePath& program) | 164 CommandLine::CommandLine(const FilePath& program) |
| 166 : argv_(1), | 165 : argv_(1), |
| 167 begin_args_(1) { | 166 begin_args_(1) { |
| 168 SetProgram(program); | 167 SetProgram(program); |
| 169 } | 168 } |
| 170 | 169 |
| 171 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) | 170 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
| 172 : argv_(1), | 171 : argv_(1), |
| 173 begin_args_(1) { | 172 begin_args_(1) { |
| 174 InitFromArgv(argc, argv); | 173 InitFromArgv(argc, argv); |
| 175 } | 174 } |
| 176 | 175 |
| 177 CommandLine::CommandLine(const StringVector& argv) | 176 CommandLine::CommandLine(const StringVector& argv) |
| 178 : argv_(1), | 177 : argv_(1), |
| 179 begin_args_(1) { | 178 begin_args_(1) { |
| 180 InitFromArgv(argv); | 179 InitFromArgv(argv); |
| 181 } | 180 } |
| 182 | 181 |
| 182 CommandLine::CommandLine(const CommandLine& other) | |
| 183 : argv_(other.argv_), | |
| 184 switches_(other.switches_), | |
| 185 begin_args_(other.begin_args_) { | |
| 186 ResetStringPieces(switches_, &switches_by_stringpiece_); | |
| 187 } | |
| 188 | |
| 189 CommandLine& CommandLine::operator=(const CommandLine& other) { | |
| 190 argv_ = other.argv_; | |
| 191 switches_ = other.switches_; | |
| 192 begin_args_ = other.begin_args_; | |
| 193 ResetStringPieces(switches_, &switches_by_stringpiece_); | |
| 194 return *this; | |
| 195 } | |
| 196 | |
| 183 CommandLine::~CommandLine() { | 197 CommandLine::~CommandLine() { |
| 184 } | 198 } |
| 185 | 199 |
| 186 #if defined(OS_WIN) | 200 #if defined(OS_WIN) |
| 187 // static | 201 // static |
| 188 void CommandLine::set_slash_is_not_a_switch() { | 202 void CommandLine::set_slash_is_not_a_switch() { |
| 189 // The last switch prefix should be slash, so adjust the size to skip it. | 203 // The last switch prefix should be slash, so adjust the size to skip it. |
| 190 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); | 204 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); |
| 191 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; | 205 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
| 192 } | 206 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 const CommandLine::CharType* const* argv) { | 256 const CommandLine::CharType* const* argv) { |
| 243 StringVector new_argv; | 257 StringVector new_argv; |
| 244 for (int i = 0; i < argc; ++i) | 258 for (int i = 0; i < argc; ++i) |
| 245 new_argv.push_back(argv[i]); | 259 new_argv.push_back(argv[i]); |
| 246 InitFromArgv(new_argv); | 260 InitFromArgv(new_argv); |
| 247 } | 261 } |
| 248 | 262 |
| 249 void CommandLine::InitFromArgv(const StringVector& argv) { | 263 void CommandLine::InitFromArgv(const StringVector& argv) { |
| 250 argv_ = StringVector(1); | 264 argv_ = StringVector(1); |
| 251 switches_.clear(); | 265 switches_.clear(); |
| 266 switches_by_stringpiece_.clear(); | |
| 252 begin_args_ = 1; | 267 begin_args_ = 1; |
| 253 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); | 268 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
| 254 AppendSwitchesAndArguments(this, argv); | 269 AppendSwitchesAndArguments(this, argv); |
| 255 } | 270 } |
| 256 | 271 |
| 257 FilePath CommandLine::GetProgram() const { | 272 FilePath CommandLine::GetProgram() const { |
| 258 return FilePath(argv_[0]); | 273 return FilePath(argv_[0]); |
| 259 } | 274 } |
| 260 | 275 |
| 261 void CommandLine::SetProgram(const FilePath& program) { | 276 void CommandLine::SetProgram(const FilePath& program) { |
| 262 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); | 277 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
| 263 } | 278 } |
| 264 | 279 |
| 265 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 280 bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const { |
| 266 DCHECK_EQ(StringToLowerASCII(switch_string), switch_string); | 281 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
| 267 return switches_.find(switch_string) != switches_.end(); | 282 return switches_by_stringpiece_.find(switch_string) != |
| 283 switches_by_stringpiece_.end(); | |
| 268 } | 284 } |
| 269 | 285 |
| 270 bool CommandLine::HasSwitch(const char string_constant[]) const { | 286 bool CommandLine::HasSwitch(const char switch_constant[]) const { |
| 271 return HasSwitch(std::string(string_constant)); | 287 return HasSwitch(base::StringPiece(switch_constant)); |
| 272 } | 288 } |
| 273 | 289 |
| 274 std::string CommandLine::GetSwitchValueASCII( | 290 std::string CommandLine::GetSwitchValueASCII( |
| 275 const std::string& switch_string) const { | 291 const base::StringPiece& switch_string) const { |
| 276 StringType value = GetSwitchValueNative(switch_string); | 292 StringType value = GetSwitchValueNative(switch_string); |
| 277 if (!IsStringASCII(value)) { | 293 if (!IsStringASCII(value)) { |
| 278 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; | 294 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| 279 return std::string(); | 295 return std::string(); |
| 280 } | 296 } |
| 281 #if defined(OS_WIN) | 297 #if defined(OS_WIN) |
| 282 return UTF16ToASCII(value); | 298 return UTF16ToASCII(value); |
| 283 #else | 299 #else |
| 284 return value; | 300 return value; |
| 285 #endif | 301 #endif |
| 286 } | 302 } |
| 287 | 303 |
| 288 FilePath CommandLine::GetSwitchValuePath( | 304 FilePath CommandLine::GetSwitchValuePath( |
| 289 const std::string& switch_string) const { | 305 const base::StringPiece& switch_string) const { |
| 290 return FilePath(GetSwitchValueNative(switch_string)); | 306 return FilePath(GetSwitchValueNative(switch_string)); |
| 291 } | 307 } |
| 292 | 308 |
| 293 CommandLine::StringType CommandLine::GetSwitchValueNative( | 309 CommandLine::StringType CommandLine::GetSwitchValueNative( |
| 294 const std::string& switch_string) const { | 310 const base::StringPiece& switch_string) const { |
| 295 SwitchMap::const_iterator result = | 311 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
|
brettw
2015/04/24 19:50:49
Shouldn't this function require that the switches
jackhou1
2015/05/11 13:51:42
It has the same DCHECK as HasSwitch. AppendSwitch*
| |
| 296 switches_.find(LowerASCIIOnWindows(switch_string)); | 312 auto result = switches_by_stringpiece_.find(switch_string); |
| 297 return result == switches_.end() ? StringType() : result->second; | 313 return result == switches_by_stringpiece_.end() ? StringType() |
| 314 : *(result->second); | |
| 298 } | 315 } |
| 299 | 316 |
| 300 void CommandLine::AppendSwitch(const std::string& switch_string) { | 317 void CommandLine::AppendSwitch(const std::string& switch_string) { |
| 301 AppendSwitchNative(switch_string, StringType()); | 318 AppendSwitchNative(switch_string, StringType()); |
| 302 } | 319 } |
| 303 | 320 |
| 304 void CommandLine::AppendSwitchPath(const std::string& switch_string, | 321 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 305 const FilePath& path) { | 322 const FilePath& path) { |
| 306 AppendSwitchNative(switch_string, path.value()); | 323 AppendSwitchNative(switch_string, path.value()); |
| 307 } | 324 } |
| 308 | 325 |
| 309 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 326 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 310 const CommandLine::StringType& value) { | 327 const CommandLine::StringType& value) { |
| 311 std::string switch_key(LowerASCIIOnWindows(switch_string)); | |
| 312 #if defined(OS_WIN) | 328 #if defined(OS_WIN) |
| 329 const std::string switch_key = StringToLowerASCII(switch_string); | |
|
brettw
2015/04/24 19:50:50
Ditto for lower-case.
jackhou1
2015/05/11 13:51:42
With AppendSwitch* the string may have come from a
| |
| 313 StringType combined_switch_string(ASCIIToUTF16(switch_key)); | 330 StringType combined_switch_string(ASCIIToUTF16(switch_key)); |
| 314 #elif defined(OS_POSIX) | 331 #elif defined(OS_POSIX) |
| 315 StringType combined_switch_string(switch_string); | 332 const std::string& switch_key = switch_string; |
| 333 StringType combined_switch_string(switch_key); | |
| 316 #endif | 334 #endif |
| 317 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | 335 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| 318 switches_[switch_key.substr(prefix_length)] = value; | 336 auto insertion = |
| 337 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); | |
| 338 if (!insertion.second) | |
| 339 insertion.first->second = value; | |
| 340 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); | |
| 319 // Preserve existing switch prefixes in |argv_|; only append one if necessary. | 341 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 320 if (prefix_length == 0) | 342 if (prefix_length == 0) |
| 321 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; | 343 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
| 322 if (!value.empty()) | 344 if (!value.empty()) |
| 323 combined_switch_string += kSwitchValueSeparator + value; | 345 combined_switch_string += kSwitchValueSeparator + value; |
| 324 // Append the switch and update the switches/arguments divider |begin_args_|. | 346 // Append the switch and update the switches/arguments divider |begin_args_|. |
| 325 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); | 347 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
| 326 } | 348 } |
| 327 | 349 |
| 328 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 350 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 447 #if defined(OS_WIN) | 469 #if defined(OS_WIN) |
| 448 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); | 470 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); |
| 449 #endif | 471 #endif |
| 450 params.append(arg); | 472 params.append(arg); |
| 451 } | 473 } |
| 452 } | 474 } |
| 453 return params; | 475 return params; |
| 454 } | 476 } |
| 455 | 477 |
| 456 } // namespace base | 478 } // namespace base |
| OLD | NEW |