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