| 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" |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 out.push_back('\\'); | 138 out.push_back('\\'); |
| 139 out.push_back('"'); | 139 out.push_back('"'); |
| 140 } else { | 140 } else { |
| 141 out.push_back(arg[i]); | 141 out.push_back(arg[i]); |
| 142 } | 142 } |
| 143 } | 143 } |
| 144 out.push_back('"'); | 144 out.push_back('"'); |
| 145 | 145 |
| 146 return out; | 146 return out; |
| 147 } | 147 } |
| 148 |
| 149 // Controls whether we honor the argc and argv parameters passed in to the main |
| 150 // function for console apps or use the shell32 API's to parse the command |
| 151 // line string and retrieve argc and argv. |
| 152 bool g_honor_argv_for_testing = false; |
| 148 #endif | 153 #endif |
| 149 | 154 |
| 150 } // namespace | 155 } // namespace |
| 151 | 156 |
| 152 CommandLine::CommandLine(NoProgram no_program) | 157 CommandLine::CommandLine(NoProgram no_program) |
| 153 : argv_(1), | 158 : argv_(1), |
| 154 begin_args_(1) { | 159 begin_args_(1) { |
| 155 } | 160 } |
| 156 | 161 |
| 157 CommandLine::CommandLine(const FilePath& program) | 162 CommandLine::CommandLine(const FilePath& program) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 CommandLine::~CommandLine() { | 195 CommandLine::~CommandLine() { |
| 191 } | 196 } |
| 192 | 197 |
| 193 #if defined(OS_WIN) | 198 #if defined(OS_WIN) |
| 194 // static | 199 // static |
| 195 void CommandLine::set_slash_is_not_a_switch() { | 200 void CommandLine::set_slash_is_not_a_switch() { |
| 196 // The last switch prefix should be slash, so adjust the size to skip it. | 201 // The last switch prefix should be slash, so adjust the size to skip it. |
| 197 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); | 202 DCHECK_EQ(wcscmp(kSwitchPrefixes[arraysize(kSwitchPrefixes) - 1], L"/"), 0); |
| 198 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; | 203 switch_prefix_count = arraysize(kSwitchPrefixes) - 1; |
| 199 } | 204 } |
| 205 |
| 206 void CommandLine::set_honor_argv_for_testing(bool honor_argv_for_testing) { |
| 207 g_honor_argv_for_testing = honor_argv_for_testing; |
| 208 } |
| 200 #endif | 209 #endif |
| 201 | 210 |
| 202 // static | 211 // static |
| 203 bool CommandLine::Init(int argc, const char* const* argv) { | 212 bool CommandLine::Init(int argc, const char* const* argv) { |
| 204 if (current_process_commandline_) { | 213 if (current_process_commandline_) { |
| 205 // If this is intentional, Reset() must be called first. If we are using | 214 // If this is intentional, Reset() must be called first. If we are using |
| 206 // the shared build mode, we have to share a single object across multiple | 215 // the shared build mode, we have to share a single object across multiple |
| 207 // shared libraries. | 216 // shared libraries. |
| 208 return false; | 217 return false; |
| 209 } | 218 } |
| 210 | 219 |
| 211 current_process_commandline_ = new CommandLine(NO_PROGRAM); | 220 current_process_commandline_ = new CommandLine(NO_PROGRAM); |
| 221 |
| 212 #if defined(OS_WIN) | 222 #if defined(OS_WIN) |
| 213 current_process_commandline_->ParseFromString(::GetCommandLineW()); | 223 if (g_honor_argv_for_testing) { |
| 224 // On Windows we need to convert the command line arguments to string16 |
| 225 base::CommandLine::StringVector argv_vector; |
| 226 for (int i = 0; i < argc; ++i) |
| 227 argv_vector.push_back(UTF8ToUTF16(argv[i])); |
| 228 current_process_commandline_->InitFromArgv(argv_vector); |
| 229 } else { |
| 230 current_process_commandline_->ParseFromString(::GetCommandLineW()); |
| 231 } |
| 214 #elif defined(OS_POSIX) | 232 #elif defined(OS_POSIX) |
| 215 current_process_commandline_->InitFromArgv(argc, argv); | 233 current_process_commandline_->InitFromArgv(argc, argv); |
| 216 #endif | 234 #endif |
| 217 | |
| 218 return true; | 235 return true; |
| 219 } | 236 } |
| 220 | 237 |
| 221 // static | 238 // static |
| 222 void CommandLine::Reset() { | 239 void CommandLine::Reset() { |
| 223 DCHECK(current_process_commandline_); | 240 DCHECK(current_process_commandline_); |
| 224 delete current_process_commandline_; | 241 delete current_process_commandline_; |
| 225 current_process_commandline_ = NULL; | 242 current_process_commandline_ = NULL; |
| 226 } | 243 } |
| 227 | 244 |
| (...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 return params; | 490 return params; |
| 474 } | 491 } |
| 475 | 492 |
| 476 void CommandLine::ResetStringPieces() { | 493 void CommandLine::ResetStringPieces() { |
| 477 switches_by_stringpiece_.clear(); | 494 switches_by_stringpiece_.clear(); |
| 478 for (const auto& entry : switches_) | 495 for (const auto& entry : switches_) |
| 479 switches_by_stringpiece_[entry.first] = &(entry.second); | 496 switches_by_stringpiece_[entry.first] = &(entry.second); |
| 480 } | 497 } |
| 481 | 498 |
| 482 } // namespace base | 499 } // namespace base |
| OLD | NEW |