OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkFlags.h" | 8 #include "SkFlags.h" |
9 | 9 |
| 10 static bool string_is_in(const char* target, const char* set[], size_t len) { |
| 11 for (size_t i = 0; i < len; i++) { |
| 12 if (0 == strcmp(target, set[i])) { |
| 13 return true; |
| 14 } |
| 15 } |
| 16 return false; |
| 17 } |
| 18 |
| 19 /** |
| 20 * Check to see whether string represents a boolean value. |
| 21 * @param string C style string to parse. |
| 22 * @param result Pointer to a boolean which will be set to the value in the str
ing, if the |
| 23 * string represents a boolean. |
| 24 * @param boolean True if the string represents a boolean, false otherwise. |
| 25 */ |
| 26 static bool parse_bool_arg(const char* string, bool* result) { |
| 27 static const char* trueValues[] = { "1", "TRUE", "true" }; |
| 28 if (string_is_in(string, trueValues, SK_ARRAY_COUNT(trueValues))) { |
| 29 *result = true; |
| 30 return true; |
| 31 } |
| 32 static const char* falseValues[] = { "0", "FALSE", "false" }; |
| 33 if (string_is_in(string, falseValues, SK_ARRAY_COUNT(falseValues))) { |
| 34 *result = false; |
| 35 return true; |
| 36 } |
| 37 SkDebugf("Parameter \"%s\" not supported.\n", string); |
| 38 return false; |
| 39 } |
| 40 |
| 41 bool SkFlagInfo::match(const char* string) { |
| 42 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
| 43 string++; |
| 44 // Allow one or two dashes |
| 45 if (SkStrStartsWith(string, '-') && strlen(string) > 1) { |
| 46 string++; |
| 47 } |
| 48 if (kBool_FlagType == fFlagType) { |
| 49 // In this case, go ahead and set the value. |
| 50 if (fName.equals(string) || fShortName.equals(string)) { |
| 51 *fBoolValue = true; |
| 52 return true; |
| 53 } |
| 54 if (SkStrStartsWith(string, "no") && strlen(string) > 2) { |
| 55 string += 2; |
| 56 if (fName.equals(string) || fShortName.equals(string)) { |
| 57 *fBoolValue = false; |
| 58 return true; |
| 59 } |
| 60 return false; |
| 61 } |
| 62 int equalIndex = SkStrFind(string, "="); |
| 63 if (equalIndex > 0) { |
| 64 // The string has an equal sign. Check to see if the string matc
hes. |
| 65 SkString flag(string, equalIndex); |
| 66 if (flag.equals(fName) || flag.equals(fShortName)) { |
| 67 // Check to see if the remainder beyond the equal sign is tr
ue or false: |
| 68 string += equalIndex + 1; |
| 69 parse_bool_arg(string, fBoolValue); |
| 70 return true; |
| 71 } |
| 72 } |
| 73 } |
| 74 return fName.equals(string) || fShortName.equals(string); |
| 75 } else { |
| 76 // Has no dash |
| 77 return false; |
| 78 } |
| 79 return false; |
| 80 } |
| 81 |
10 SkFlagInfo* SkFlags::gHead; | 82 SkFlagInfo* SkFlags::gHead; |
11 SkString SkFlags::gUsage; | 83 SkString SkFlags::gUsage; |
12 | 84 |
13 void SkFlags::SetUsage(const char* usage) { | 85 void SkFlags::SetUsage(const char* usage) { |
14 gUsage.set(usage); | 86 gUsage.set(usage); |
15 } | 87 } |
16 | 88 |
17 // Maximum line length for the help message. | 89 // Maximum line length for the help message. |
18 #define LINE_LENGTH 80 | 90 #define LINE_LENGTH 80 |
19 | 91 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 helpPrinted = true; | 196 helpPrinted = true; |
125 } | 197 } |
126 if (!helpPrinted) { | 198 if (!helpPrinted) { |
127 bool flagMatched = false; | 199 bool flagMatched = false; |
128 SkFlagInfo* flag = gHead; | 200 SkFlagInfo* flag = gHead; |
129 while (flag != NULL) { | 201 while (flag != NULL) { |
130 if (flag->match(argv[i])) { | 202 if (flag->match(argv[i])) { |
131 flagMatched = true; | 203 flagMatched = true; |
132 switch (flag->getFlagType()) { | 204 switch (flag->getFlagType()) { |
133 case SkFlagInfo::kBool_FlagType: | 205 case SkFlagInfo::kBool_FlagType: |
134 // Handled by match, above | 206 // Can be handled by match, above, but can also be s
et by the next |
| 207 // string. |
| 208 if (i+1 < argc && !SkStrStartsWith(argv[i+1], '-'))
{ |
| 209 i++; |
| 210 bool value; |
| 211 if (parse_bool_arg(argv[i], &value)) { |
| 212 flag->setBool(value); |
| 213 } |
| 214 } |
135 break; | 215 break; |
136 case SkFlagInfo::kString_FlagType: | 216 case SkFlagInfo::kString_FlagType: |
137 flag->resetStrings(); | 217 flag->resetStrings(); |
138 // Add all arguments until another flag is reached. | 218 // Add all arguments until another flag is reached. |
139 while (i+1 < argc && !SkStrStartsWith(argv[i+1], '-'
)) { | 219 while (i+1 < argc && !SkStrStartsWith(argv[i+1], '-'
)) { |
140 i++; | 220 i++; |
141 flag->append(argv[i]); | 221 flag->append(argv[i]); |
142 } | 222 } |
143 break; | 223 break; |
144 case SkFlagInfo::kInt_FlagType: | 224 case SkFlagInfo::kInt_FlagType: |
(...skipping 23 matching lines...) Expand all Loading... |
168 gHead = NULL; | 248 gHead = NULL; |
169 while (flag != NULL) { | 249 while (flag != NULL) { |
170 SkFlagInfo* next = flag->next(); | 250 SkFlagInfo* next = flag->next(); |
171 SkDELETE(flag); | 251 SkDELETE(flag); |
172 flag = next; | 252 flag = next; |
173 } | 253 } |
174 if (helpPrinted) { | 254 if (helpPrinted) { |
175 exit(0); | 255 exit(0); |
176 } | 256 } |
177 } | 257 } |
OLD | NEW |