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/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/stl_util.h" | |
| 13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_tokenizer.h" | 15 #include "base/strings/string_tokenizer.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 18 | 19 |
| 19 #if defined(OS_WIN) | 20 #if defined(OS_WIN) |
| 20 #include <windows.h> | 21 #include <windows.h> |
| 21 #include <shellapi.h> | 22 #include <shellapi.h> |
| 22 #endif | 23 #endif |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 } else { | 142 } else { |
| 142 out.push_back(arg[i]); | 143 out.push_back(arg[i]); |
| 143 } | 144 } |
| 144 } | 145 } |
| 145 out.push_back('"'); | 146 out.push_back('"'); |
| 146 | 147 |
| 147 return out; | 148 return out; |
| 148 } | 149 } |
| 149 #endif | 150 #endif |
| 150 | 151 |
| 152 std::string SwitchKey(const std::string& switch_string) { | |
|
Ilya Sherman
2017/06/09 20:19:28
nit: Mebbe "GetSwitchKey"?
| |
| 153 #if defined(OS_WIN) | |
| 154 return ToLowerASCII(switch_string); | |
| 155 #elif defined(OS_POSIX) | |
| 156 return switch_string; | |
| 157 #endif | |
| 158 } | |
| 159 | |
| 160 CommandLine::StringType CombinedSwitchString(const std::string switch_key) { | |
|
Ilya Sherman
2017/06/09 20:19:28
nit: Mebbe "GetCombinedSwitchString"?
| |
| 161 #if defined(OS_WIN) | |
| 162 return ASCIIToUTF16(switch_key); | |
| 163 #elif defined(OS_POSIX) | |
| 164 return switch_key; | |
| 165 #endif | |
| 166 } | |
| 167 | |
| 151 } // namespace | 168 } // namespace |
| 152 | 169 |
| 153 CommandLine::CommandLine(NoProgram no_program) | 170 CommandLine::CommandLine(NoProgram no_program) |
| 154 : argv_(1), | 171 : argv_(1), |
| 155 begin_args_(1) { | 172 begin_args_(1) { |
| 156 } | 173 } |
| 157 | 174 |
| 158 CommandLine::CommandLine(const FilePath& program) | 175 CommandLine::CommandLine(const FilePath& program) |
| 159 : argv_(1), | 176 : argv_(1), |
| 160 begin_args_(1) { | 177 begin_args_(1) { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 327 AppendSwitchNative(switch_string, StringType()); | 344 AppendSwitchNative(switch_string, StringType()); |
| 328 } | 345 } |
| 329 | 346 |
| 330 void CommandLine::AppendSwitchPath(const std::string& switch_string, | 347 void CommandLine::AppendSwitchPath(const std::string& switch_string, |
| 331 const FilePath& path) { | 348 const FilePath& path) { |
| 332 AppendSwitchNative(switch_string, path.value()); | 349 AppendSwitchNative(switch_string, path.value()); |
| 333 } | 350 } |
| 334 | 351 |
| 335 void CommandLine::AppendSwitchNative(const std::string& switch_string, | 352 void CommandLine::AppendSwitchNative(const std::string& switch_string, |
| 336 const CommandLine::StringType& value) { | 353 const CommandLine::StringType& value) { |
| 337 #if defined(OS_WIN) | 354 const std::string switch_key = SwitchKey(switch_string); |
| 338 const std::string switch_key = ToLowerASCII(switch_string); | 355 StringType combined_switch_string = CombinedSwitchString(switch_key); |
| 339 StringType combined_switch_string(ASCIIToUTF16(switch_key)); | |
| 340 #elif defined(OS_POSIX) | |
| 341 const std::string& switch_key = switch_string; | |
| 342 StringType combined_switch_string(switch_key); | |
| 343 #endif | |
| 344 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | 356 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); |
| 345 auto insertion = | 357 auto insertion = |
| 346 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); | 358 switches_.insert(make_pair(switch_key.substr(prefix_length), value)); |
| 347 if (!insertion.second) | 359 if (!insertion.second) |
| 348 insertion.first->second = value; | 360 insertion.first->second = value; |
| 349 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); | 361 switches_by_stringpiece_[insertion.first->first] = &(insertion.first->second); |
| 350 // Preserve existing switch prefixes in |argv_|; only append one if necessary. | 362 // Preserve existing switch prefixes in |argv_|; only append one if necessary. |
| 351 if (prefix_length == 0) | 363 if (prefix_length == 0) |
| 352 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; | 364 combined_switch_string = kSwitchPrefixes[0] + combined_switch_string; |
| 353 if (!value.empty()) | 365 if (!value.empty()) |
| 354 combined_switch_string += kSwitchValueSeparator + value; | 366 combined_switch_string += kSwitchValueSeparator + value; |
| 355 // Append the switch and update the switches/arguments divider |begin_args_|. | 367 // Append the switch and update the switches/arguments divider |begin_args_|. |
| 356 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); | 368 argv_.insert(argv_.begin() + begin_args_++, combined_switch_string); |
| 357 } | 369 } |
| 358 | 370 |
| 359 void CommandLine::AppendSwitchASCII(const std::string& switch_string, | 371 void CommandLine::AppendSwitchASCII(const std::string& switch_string, |
| 360 const std::string& value_string) { | 372 const std::string& value_string) { |
| 361 #if defined(OS_WIN) | 373 #if defined(OS_WIN) |
| 362 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string)); | 374 AppendSwitchNative(switch_string, ASCIIToUTF16(value_string)); |
| 363 #elif defined(OS_POSIX) | 375 #elif defined(OS_POSIX) |
| 364 AppendSwitchNative(switch_string, value_string); | 376 AppendSwitchNative(switch_string, value_string); |
| 365 #endif | 377 #endif |
| 366 } | 378 } |
| 367 | 379 |
| 380 void CommandLine::RemoveSwitchForTesting(const std::string& switch_string) { | |
| 381 const std::string switch_key = SwitchKey(switch_string); | |
| 382 StringType combined_switch_string = CombinedSwitchString(switch_key); | |
| 383 size_t prefix_length = GetSwitchPrefixLength(combined_switch_string); | |
| 384 switches_by_stringpiece_.erase(switch_key.substr(prefix_length)); | |
| 385 switches_.erase(switch_string.substr(prefix_length)); | |
| 386 | |
| 387 auto find_switch = [&combined_switch_string](const std::string& s) { | |
|
Ilya Sherman
2017/06/09 20:19:28
nit: What does "s" represent? Please give it a cl
| |
| 388 return s.find(combined_switch_string) != std::string::npos; | |
| 389 }; | |
| 390 | |
| 391 base::EraseIf(argv_, find_switch); | |
| 392 } | |
| 393 | |
| 368 void CommandLine::CopySwitchesFrom(const CommandLine& source, | 394 void CommandLine::CopySwitchesFrom(const CommandLine& source, |
| 369 const char* const switches[], | 395 const char* const switches[], |
| 370 size_t count) { | 396 size_t count) { |
| 371 for (size_t i = 0; i < count; ++i) { | 397 for (size_t i = 0; i < count; ++i) { |
| 372 if (source.HasSwitch(switches[i])) | 398 if (source.HasSwitch(switches[i])) |
| 373 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); | 399 AppendSwitchNative(switches[i], source.GetSwitchValueNative(switches[i])); |
| 374 } | 400 } |
| 375 } | 401 } |
| 376 | 402 |
| 377 CommandLine::StringVector CommandLine::GetArgs() const { | 403 CommandLine::StringVector CommandLine::GetArgs() const { |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 489 return params; | 515 return params; |
| 490 } | 516 } |
| 491 | 517 |
| 492 void CommandLine::ResetStringPieces() { | 518 void CommandLine::ResetStringPieces() { |
| 493 switches_by_stringpiece_.clear(); | 519 switches_by_stringpiece_.clear(); |
| 494 for (const auto& entry : switches_) | 520 for (const auto& entry : switches_) |
| 495 switches_by_stringpiece_[entry.first] = &(entry.second); | 521 switches_by_stringpiece_[entry.first] = &(entry.second); |
| 496 } | 522 } |
| 497 | 523 |
| 498 } // namespace base | 524 } // namespace base |
| OLD | NEW |