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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 begin_args_(1) { | 161 begin_args_(1) { |
174 InitFromArgv(argc, argv); | 162 InitFromArgv(argc, argv); |
175 } | 163 } |
176 | 164 |
177 CommandLine::CommandLine(const StringVector& argv) | 165 CommandLine::CommandLine(const StringVector& argv) |
178 : argv_(1), | 166 : argv_(1), |
179 begin_args_(1) { | 167 begin_args_(1) { |
180 InitFromArgv(argv); | 168 InitFromArgv(argv); |
181 } | 169 } |
182 | 170 |
| 171 CommandLine::CommandLine(const CommandLine& other) |
| 172 : argv_(other.argv_), |
| 173 switches_(other.switches_), |
| 174 begin_args_(other.begin_args_) { |
| 175 ResetStringPieces(); |
| 176 } |
| 177 |
| 178 CommandLine& CommandLine::operator=(const CommandLine& other) { |
| 179 argv_ = other.argv_; |
| 180 switches_ = other.switches_; |
| 181 begin_args_ = other.begin_args_; |
| 182 ResetStringPieces(); |
| 183 return *this; |
| 184 } |
| 185 |
183 CommandLine::~CommandLine() { | 186 CommandLine::~CommandLine() { |
184 } | 187 } |
185 | 188 |
186 #if defined(OS_WIN) | 189 #if defined(OS_WIN) |
187 // static | 190 // static |
188 void CommandLine::set_slash_is_not_a_switch() { | 191 void CommandLine::set_slash_is_not_a_switch() { |
189 // The last switch prefix should be slash, so adjust the size to skip it. | 192 // The last switch prefix should be slash, so adjust the size to skip it. |
190 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); | 193 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); |
191 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; | 194 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
192 } | 195 } |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 const CommandLine::CharType* const* argv) { | 245 const CommandLine::CharType* const* argv) { |
243 StringVector new_argv; | 246 StringVector new_argv; |
244 for (int i = 0; i < argc; ++i) | 247 for (int i = 0; i < argc; ++i) |
245 new_argv.push_back(argv[i]); | 248 new_argv.push_back(argv[i]); |
246 InitFromArgv(new_argv); | 249 InitFromArgv(new_argv); |
247 } | 250 } |
248 | 251 |
249 void CommandLine::InitFromArgv(const StringVector& argv) { | 252 void CommandLine::InitFromArgv(const StringVector& argv) { |
250 argv_ = StringVector(1); | 253 argv_ = StringVector(1); |
251 switches_.clear(); | 254 switches_.clear(); |
| 255 switches_by_stringpiece_.clear(); |
252 begin_args_ = 1; | 256 begin_args_ = 1; |
253 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); | 257 SetProgram(argv.empty() ? FilePath() : FilePath(argv[0])); |
254 AppendSwitchesAndArguments(this, argv); | 258 AppendSwitchesAndArguments(this, argv); |
255 } | 259 } |
256 | 260 |
257 FilePath CommandLine::GetProgram() const { | 261 FilePath CommandLine::GetProgram() const { |
258 return FilePath(argv_[0]); | 262 return FilePath(argv_[0]); |
259 } | 263 } |
260 | 264 |
261 void CommandLine::SetProgram(const FilePath& program) { | 265 void CommandLine::SetProgram(const FilePath& program) { |
262 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); | 266 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); |
263 } | 267 } |
264 | 268 |
265 bool CommandLine::HasSwitch(const std::string& switch_string) const { | 269 bool CommandLine::HasSwitch(const base::StringPiece& switch_string) const { |
266 DCHECK_EQ(StringToLowerASCII(switch_string), switch_string); | 270 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
267 return switches_.find(switch_string) != switches_.end(); | 271 return switches_by_stringpiece_.find(switch_string) != |
| 272 switches_by_stringpiece_.end(); |
268 } | 273 } |
269 | 274 |
270 bool CommandLine::HasSwitch(const char string_constant[]) const { | 275 bool CommandLine::HasSwitch(const char switch_constant[]) const { |
271 return HasSwitch(std::string(string_constant)); | 276 return HasSwitch(base::StringPiece(switch_constant)); |
272 } | 277 } |
273 | 278 |
274 std::string CommandLine::GetSwitchValueASCII( | 279 std::string CommandLine::GetSwitchValueASCII( |
275 const std::string& switch_string) const { | 280 const base::StringPiece& switch_string) const { |
276 StringType value = GetSwitchValueNative(switch_string); | 281 StringType value = GetSwitchValueNative(switch_string); |
277 if (!IsStringASCII(value)) { | 282 if (!IsStringASCII(value)) { |
278 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; | 283 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; |
279 return std::string(); | 284 return std::string(); |
280 } | 285 } |
281 #if defined(OS_WIN) | 286 #if defined(OS_WIN) |
282 return UTF16ToASCII(value); | 287 return UTF16ToASCII(value); |
283 #else | 288 #else |
284 return value; | 289 return value; |
285 #endif | 290 #endif |
286 } | 291 } |
287 | 292 |
288 FilePath CommandLine::GetSwitchValuePath( | 293 FilePath CommandLine::GetSwitchValuePath( |
289 const std::string& switch_string) const { | 294 const base::StringPiece& switch_string) const { |
290 return FilePath(GetSwitchValueNative(switch_string)); | 295 return FilePath(GetSwitchValueNative(switch_string)); |
291 } | 296 } |
292 | 297 |
293 CommandLine::StringType CommandLine::GetSwitchValueNative( | 298 CommandLine::StringType CommandLine::GetSwitchValueNative( |
294 const std::string& switch_string) const { | 299 const base::StringPiece& switch_string) const { |
295 SwitchMap::const_iterator result = | 300 DCHECK_EQ(StringToLowerASCII(switch_string.as_string()), switch_string); |
296 switches_.find(LowerASCIIOnWindows(switch_string)); | 301 auto result = switches_by_stringpiece_.find(switch_string); |
297 return result == switches_.end() ? StringType() : result->second; | 302 return result == switches_by_stringpiece_.end() ? StringType() |
| 303 : *(result->second); |
298 } | 304 } |
299 | 305 |
300 void CommandLine::AppendSwitch(const std::string& switch_string) { | 306 void CommandLine::AppendSwitch(const std::string& switch_string) { |
301 AppendSwitchNative(switch_string, StringType()); | 307 AppendSwitchNative(switch_string, StringType()); |
302 } | 308 } |
303 | 309 |
304 void CommandLine::AppendSwitchPath(const std::string& switch_string, | 310 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
305 const FilePath& path) { | 311 const FilePath& path) { |
306 AppendSwitchNative(switch_string, path.value()); | 312 AppendSwitchNative(switch_string, path.value()); |
307 } | 313 } |
308 | 314 |
309 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 315 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
310 const CommandLine::StringType& value) { | 316 const CommandLine::StringType& value) { |
311 std::string switch_key(LowerASCIIOnWindows(switch_string)); | |
312 #if defined(OS_WIN) | 317 #if defined(OS_WIN) |
| 318 const std::string switch_key = StringToLowerASCII(switch_string); |
313 StringType combined_switch_string(ASCIIToUTF16(switch_key)); | 319 StringType combined_switch_string(ASCIIToUTF16(switch_key)); |
314 #elif defined(OS_POSIX) | 320 #elif defined(OS_POSIX) |
315 StringType combined_switch_string(switch_string); | 321 const std::string& switch_key = switch_string; |
| 322 StringType combined_switch_string(switch_key); |
316 #endif | 323 #endif |
317 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | 324 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
318 switches_[switch_key.substr(prefix_length)] = value; | 325 auto insertion = |
| 326 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); |
| 327 if (!insertion.second) |
| 328 insertion.first->second = value; |
| 329 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); |
319 // Preserve existing switch prefixes in |argv_|; only append one if necessary. | 330 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
320 if (prefix_length == 0) | 331 if (prefix_length == 0) |
321 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; | 332 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
322 if (!value.empty()) | 333 if (!value.empty()) |
323 combined_switch_string += kSwitchValueSeparator + value; | 334 combined_switch_string += kSwitchValueSeparator + value; |
324 // Append the switch and update the switches/arguments divider |begin_args_|. | 335 // Append the switch and update the switches/arguments divider |begin_args_|. |
325 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); | 336 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
326 } | 337 } |
327 | 338 |
328 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 339 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 } else { | 457 } else { |
447 #if defined(OS_WIN) | 458 #if defined(OS_WIN) |
448 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); | 459 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); |
449 #endif | 460 #endif |
450 params.append(arg); | 461 params.append(arg); |
451 } | 462 } |
452 } | 463 } |
453 return params; | 464 return params; |
454 } | 465 } |
455 | 466 |
| 467 void CommandLine::ResetStringPieces() { |
| 468 switches_by_stringpiece_.clear(); |
| 469 for (const auto& entry : switches_) |
| 470 switches_by_stringpiece_[entry.first] = &(entry.second); |
| 471 } |
| 472 |
456 } // namespace base | 473 } // namespace base |
OLD | NEW |