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( | |
| 150 const CommandLine::SwitchMap& switches, | |
| 151 std::map<base::StringPiece, const CommandLine::StringType*>* | |
|
tapted
2015/04/17 01:14:33
nit: this should have a typedef (I know there's di
jackhou1
2015/04/22 04:09:42
Done.
| |
| 152 switches_by_stringpiece) { | |
| 153 switches_by_stringpiece->clear(); | |
| 154 for (auto it = switches.begin(); it != switches.end(); ++it) | |
| 155 (*switches_by_stringpiece)[it->first] = &(it->second); | |
| 156 } | |
| 157 | |
| 158 } // namespace | 158 } // namespace |
| 159 | 159 |
| 160 CommandLine::CommandLine(NoProgram no_program) | 160 CommandLine::CommandLine(NoProgram no_program) |
| 161 : argv_(1), | 161 : argv_(1), |
| 162 begin_args_(1) { | 162 begin_args_(1) { |
| 163 } | 163 } |
| 164 | 164 |
| 165 CommandLine::CommandLine(const FilePath& program) | 165 CommandLine::CommandLine(const FilePath& program) |
| 166 : argv_(1), | 166 : argv_(1), |
| 167 begin_args_(1) { | 167 begin_args_(1) { |
| 168 SetProgram(program); | 168 SetProgram(program); |
| 169 } | 169 } |
| 170 | 170 |
| 171 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) | 171 CommandLine::CommandLine(int argc, const CommandLine::CharType* const* argv) |
| 172 : argv_(1), | 172 : argv_(1), |
| 173 begin_args_(1) { | 173 begin_args_(1) { |
| 174 InitFromArgv(argc, argv); | 174 InitFromArgv(argc, argv); |
| 175 } | 175 } |
| 176 | 176 |
| 177 CommandLine::CommandLine(const StringVector& argv) | 177 CommandLine::CommandLine(const StringVector& argv) |
| 178 : argv_(1), | 178 : argv_(1), |
| 179 begin_args_(1) { | 179 begin_args_(1) { |
| 180 InitFromArgv(argv); | 180 InitFromArgv(argv); |
| 181 } | 181 } |
| 182 | 182 |
| 183 CommandLine::CommandLine(const CommandLine& other) | |
| 184 : argv_(other.argv_), | |
| 185 switches_(other.switches_), | |
| 186 begin_args_(other.begin_args_) { | |
| 187 ResetStringPieces(switches_, &switches_by_stringpiece_); | |
| 188 } | |
| 189 | |
| 190 CommandLine& CommandLine::operator=(const CommandLine& other) { | |
| 191 argv_ = other.argv_; | |
| 192 switches_ = other.switches_; | |
| 193 begin_args_ = other.begin_args_; | |
| 194 ResetStringPieces(switches_, &switches_by_stringpiece_); | |
| 195 return *this; | |
| 196 } | |
| 197 | |
| 183 CommandLine::~CommandLine() { | 198 CommandLine::~CommandLine() { |
| 184 } | 199 } |
| 185 | 200 |
| 186 #if defined(OS_WIN) | 201 #if defined(OS_WIN) |
| 187 // static | 202 // static |
| 188 void CommandLine::set_slash_is_not_a_switch() { | 203 void CommandLine::set_slash_is_not_a_switch() { |
| 189 // The last switch prefix should be slash, so adjust the size to skip it. | 204 // The last switch prefix should be slash, so adjust the size to skip it. |
| 190 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); | 205 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); |
| 191 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; | 206 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
| 192 } | 207 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 const CommandLine::CharType* const* argv) { | 257 const CommandLine::CharType* const* argv) { |
| 243 StringVector new_argv; | 258 StringVector new_argv; |
| 244 for (int i = 0; i < argc; ++i) | 259 for (int i = 0; i < argc; ++i) |
| 245 new_argv.push_back(argv[i]); | 260 new_argv.push_back(argv[i]); |
| 246 InitFromArgv(new_argv); | 261 InitFromArgv(new_argv); |
| 247 } | 262 } |
| 248 | 263 |
| 249 void CommandLine::InitFromArgv(const StringVector& argv) { | 264 void CommandLine::InitFromArgv(const StringVector& argv) { |
| 250 argv_ = StringVector(1); | 265 argv_ = StringVector(1); |
| 251 switches_.clear(); | 266 switches_.clear(); |
| 267 switches_by_stringpiece_.clear(); | |
| 252 begin_args_ = 1; | 268 begin_args_ = 1; |
| 253 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); | 269 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
| 254 AppendSwitchesAndArguments(this, argv); | 270 AppendSwitchesAndArguments(this, argv); |
| 255 } | 271 } |
| 256 | 272 |
| 257 FilePath CommandLine::GetProgram() const { | 273 FilePath CommandLine::GetProgram() const { |
| 258 return FilePath(argv_[0]); | 274 return FilePath(argv_[0]); |
| 259 } | 275 } |
| 260 | 276 |
| 261 void CommandLine::SetProgram(const FilePath& program) { | 277 void CommandLine::SetProgram(const FilePath& program) { |
| 262 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); | 278 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
| 263 } | 279 } |
| 264 | 280 |
| 265 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 281 bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const { |
| 266 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end(); | 282 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
|
tapted
2015/04/17 01:14:33
rebase?
jackhou1
2015/04/22 04:09:42
Done.
| |
| 283 return switches_by_stringpiece_.find(switch_string) != | |
| 284 switches_by_stringpiece_.end(); | |
| 267 } | 285 } |
| 268 | 286 |
| 269 bool CommandLine::HasSwitch(const char string_constant[]) const { | 287 bool CommandLine::HasSwitch(const char switch_constant[]) const { |
| 270 return HasSwitch(std::string(string_constant)); | 288 return HasSwitch(base::StringPiece(switch_constant)); |
| 271 } | 289 } |
| 272 | 290 |
| 273 std::string CommandLine::GetSwitchValueASCII( | 291 std::string CommandLine::GetSwitchValueASCII( |
| 274 const std::string& switch_string) const { | 292 const base::StringPiece& switch_string) const { |
| 275 StringType value = GetSwitchValueNative(switch_string); | 293 StringType value = GetSwitchValueNative(switch_string); |
| 276 if (!IsStringASCII(value)) { | 294 if (!IsStringASCII(value)) { |
| 277 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; | 295 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
| 278 return std::string(); | 296 return std::string(); |
| 279 } | 297 } |
| 280 #if defined(OS_WIN) | 298 #if defined(OS_WIN) |
| 281 return UTF16ToASCII(value); | 299 return UTF16ToASCII(value); |
| 282 #else | 300 #else |
| 283 return value; | 301 return value; |
| 284 #endif | 302 #endif |
| 285 } | 303 } |
| 286 | 304 |
| 287 FilePath CommandLine::GetSwitchValuePath( | 305 FilePath CommandLine::GetSwitchValuePath( |
| 288 const std::string& switch_string) const { | 306 const base::StringPiece& switch_string) const { |
| 289 return FilePath(GetSwitchValueNative(switch_string)); | 307 return FilePath(GetSwitchValueNative(switch_string)); |
| 290 } | 308 } |
| 291 | 309 |
| 292 CommandLine::StringType CommandLine::GetSwitchValueNative( | 310 CommandLine::StringType CommandLine::GetSwitchValueNative( |
| 293 const std::string& switch_string) const { | 311 const base::StringPiece& switch_string) const { |
| 294 SwitchMap::const_iterator result = | 312 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
| 295 switches_.find(LowerASCIIOnWindows(switch_string)); | 313 auto result = switches_by_stringpiece_.find(switch_string); |
| 296 return result == switches_.end() ? StringType() : result->second; | 314 return result == switches_by_stringpiece_.end() ? StringType() |
| 315 : *(result->second); | |
| 297 } | 316 } |
| 298 | 317 |
| 299 void CommandLine::AppendSwitch(const std::string& switch_string) { | 318 void CommandLine::AppendSwitch(const std::string& switch_string) { |
| 300 AppendSwitchNative(switch_string, StringType()); | 319 AppendSwitchNative(switch_string, StringType()); |
| 301 } | 320 } |
| 302 | 321 |
| 303 void CommandLine::AppendSwitchPath(const std::string& switch_string, | 322 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 304 const FilePath& path) { | 323 const FilePath& path) { |
| 305 AppendSwitchNative(switch_string, path.value()); | 324 AppendSwitchNative(switch_string, path.value()); |
| 306 } | 325 } |
| 307 | 326 |
| 308 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 327 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 309 const CommandLine::StringType& value) { | 328 const CommandLine::StringType& value) { |
| 310 std::string switch_key(LowerASCIIOnWindows(switch_string)); | |
| 311 #if defined(OS_WIN) | 329 #if defined(OS_WIN) |
| 330 const std::string switch_key = StringToLowerASCII(switch_string); | |
| 312 StringType combined_switch_string(ASCIIToUTF16(switch_key)); | 331 StringType combined_switch_string(ASCIIToUTF16(switch_key)); |
| 313 #elif defined(OS_POSIX) | 332 #elif defined(OS_POSIX) |
| 314 StringType combined_switch_string(switch_string); | 333 const std::string& switch_key = switch_string; |
| 334 StringType combined_switch_string(switch_key); | |
| 315 #endif | 335 #endif |
| 316 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | 336 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| 317 switches_[switch_key.substr(prefix_length)] = value; | 337 auto insertion = |
| 338 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); | |
| 339 if (!insertion.second) | |
| 340 insertion.first->second = value; | |
| 341 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); | |
| 318 // Preserve existing switch prefixes in |argv_|; only append one if necessary. | 342 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 319 if (prefix_length == 0) | 343 if (prefix_length == 0) |
| 320 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; | 344 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
| 321 if (!value.empty()) | 345 if (!value.empty()) |
| 322 combined_switch_string += kSwitchValueSeparator + value; | 346 combined_switch_string += kSwitchValueSeparator + value; |
| 323 // Append the switch and update the switches/arguments divider |begin_args_|. | 347 // Append the switch and update the switches/arguments divider |begin_args_|. |
| 324 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); | 348 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
| 325 } | 349 } |
| 326 | 350 |
| 327 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 351 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 446 #if defined(OS_WIN) | 470 #if defined(OS_WIN) |
| 447 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); | 471 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); |
| 448 #endif | 472 #endif |
| 449 params.append(arg); | 473 params.append(arg); |
| 450 } | 474 } |
| 451 } | 475 } |
| 452 return params; | 476 return params; |
| 453 } | 477 } |
| 454 | 478 |
| 455 } // namespace base | 479 } // namespace base |
| OLD | NEW |