OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 #include "debugger/base/debug_command_line.h" | |
5 #include <stdio.h> | |
6 #include <stdlib.h> | |
7 #include <string.h> | |
8 | |
9 #ifdef _WIN32 | |
10 #pragma warning(disable : 4996) // Disable sscanf warning. | |
11 #endif | |
12 | |
13 namespace { | |
14 /// Prepends '-' if |name| has one '-' at the front. | |
15 std::string NormalizeSwitchName(const std::string& name) { | |
16 std::string result = name; | |
17 if (name.size() > 1) | |
18 if ((name[0] == '-') && (name[1] != '-')) | |
19 result = std::string("-") + result; | |
20 return result; | |
21 } | |
22 | |
23 /// Prepends '-' one at a time until the name has two -- at the front. | |
24 std::string CreateSwitchName(const std::string& name) { | |
25 std::string result = name; | |
26 while (0 != strncmp(result.c_str(), "--", 2)) { | |
27 result = std::string("-") + result; | |
28 } | |
29 return result; | |
30 } | |
31 | |
32 bool HasWhiteSpace(const std::string& str) { | |
33 for (size_t i = 0; i < str.size(); i++) { | |
34 if (isspace(str[i])) | |
35 return true; | |
36 } | |
37 return false; | |
38 } | |
39 } // namespace | |
40 | |
41 namespace debug { | |
42 CommandLine::CommandLine(int argc, char* argv[]) { | |
43 for (int i = 0; i < argc; i++) { | |
44 std::string value; | |
45 if (NULL != argv[i]) | |
46 value = argv[i]; | |
47 argv_.push_back(value); | |
48 } | |
49 } | |
50 | |
51 CommandLine::CommandLine(const std::string& command_line) { | |
52 std::string token; | |
53 bool in_quote = false; | |
54 for (size_t i = 0; i < command_line.size(); i++) { | |
55 char c = command_line[i]; | |
56 if (in_quote) { | |
57 if ('"' == c) { | |
58 in_quote = false; | |
59 argv_.push_back(token); | |
60 token.clear(); | |
61 } else { | |
62 token.push_back(c); | |
63 } | |
64 } else if (isspace(c)) { | |
65 if (token.size() > 0) | |
66 argv_.push_back(token); | |
67 token.clear(); | |
68 } else if (('"' == c) && (token.size() == 0)) { | |
69 in_quote = true; | |
70 } else { | |
71 token.push_back(c); | |
72 } | |
73 } | |
74 if (token.size() > 0) | |
75 argv_.push_back(token); | |
76 } | |
77 | |
78 CommandLine::~CommandLine() {} | |
79 | |
80 size_t CommandLine::GetParametersNum() const { | |
81 return argv_.size() > 0 ? argv_.size() - 1 : 0; | |
82 } | |
83 | |
84 std::string CommandLine::GetParameter(size_t pos) const { | |
85 if (argv_.size() > (pos + 1)) | |
86 return argv_[pos + 1]; | |
87 return ""; | |
88 } | |
89 | |
90 std::string CommandLine::GetProgramName() const { | |
91 if (argv_.size() > 0) | |
92 return argv_[0]; | |
93 return ""; | |
94 } | |
95 | |
96 std::string CommandLine::ToString() const { | |
97 std::string str; | |
98 for (size_t i = 0; i < argv_.size(); i++) { | |
99 if (i > 0) | |
100 str += " "; | |
101 if (HasWhiteSpace(argv_[i])) { | |
102 str += "\""; | |
103 str += argv_[i]; | |
104 str += "\""; | |
105 } else { | |
106 str += argv_[i]; | |
107 } | |
108 } | |
109 return str; | |
110 } | |
111 | |
112 std::string CommandLine::GetStringSwitch( | |
113 const std::string& name, | |
114 const std::string& default_value) const { | |
115 for (size_t i = 1; (i + 1) < argv_.size(); i++) | |
116 if (NormalizeSwitchName(argv_[i]) == CreateSwitchName(name)) | |
117 return argv_[i + 1]; | |
118 return default_value; | |
119 } | |
120 | |
121 int CommandLine::GetIntSwitch(const std::string& name, | |
122 int default_value) const { | |
123 std::string str = GetStringSwitch(name, ""); | |
124 if (0 != str.size()) | |
125 return atoi(str.c_str()); | |
126 return default_value; | |
127 } | |
128 | |
129 void* CommandLine::GetAddrSwitch(const std::string& name) const { | |
130 void* ptr = NULL; | |
131 std::string str = GetStringSwitch(name, ""); | |
132 sscanf(str.c_str(), "%p", &ptr); // NOLINT | |
133 return ptr; | |
134 } | |
135 | |
136 bool CommandLine::HasSwitch(const std::string& name) const { | |
137 for (size_t i = 1; i < argv_.size(); i++) | |
138 if (NormalizeSwitchName(argv_[i]) == CreateSwitchName(name)) | |
139 return true; | |
140 return false; | |
141 } | |
142 } // namespace debug | |
143 | |
144 | |
OLD | NEW |