Differences From Artifact [3c15f89aa9]:
- File src/OFOptionsParser.m — part of check-in [cfd374b906] at 2015-01-03 20:57:18 on branch trunk — Update copyright (user: js, size: 2703) [annotate] [blame] [check-ins using]
To Artifact [fbe9ab7d4c]:
- File
src/OFOptionsParser.m
— part of check-in
[bfa913aebe]
at
2015-11-28 18:56:19
on branch trunk
— OFOptionsParser: Reworked API
The new API allows long options and requires less manual parsing. (user: js, size: 6304) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
15 16 17 18 19 20 21 22 23 | */ #include "config.h" #import "OFOptionsParser.h" #import "OFApplication.h" #import "OFArray.h" @implementation OFOptionsParser | > > > > > > > > > > > > > > > | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > | > > > > > > > > | > > | > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | < | < < > > > > > > > > > > > | | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | */ #include "config.h" #import "OFOptionsParser.h" #import "OFApplication.h" #import "OFArray.h" #import "OFMapTable.h" #import "OFInvalidArgumentException.h" static uint32_t stringHash(void *value) { return [(OFString*)value hash]; } static bool stringEqual(void *value1, void *value2) { return [(OFString*)value1 isEqual: (OFString*)value2]; } @implementation OFOptionsParser + (instancetype)parserWithOptions: (const of_options_parser_option_t*)options { return [[[self alloc] initWithOptions: options] autorelease]; } - init { OF_INVALID_INIT_METHOD } - initWithOptions: (const of_options_parser_option_t*)options { self = [super init]; @try { size_t count = 0; const of_options_parser_option_t *iter; of_options_parser_option_t *iter2; const of_map_table_functions_t keyFunctions = { .hash = stringHash, .equal = stringEqual }; const of_map_table_functions_t valueFunctions = { NULL }; /* Count, sanity check and initialize pointers */ for (iter = options; iter->shortOption != '\0' || iter->longOption != nil; iter++) { if (iter->hasArgument < -1 || iter->hasArgument > 1) @throw [OFInvalidArgumentException exception]; if (iter->shortOption != '\0' && iter->hasArgument == -1) @throw [OFInvalidArgumentException exception]; if (iter->hasArgument == 0 && iter->argumentPtr != NULL) @throw [OFInvalidArgumentException exception]; if (iter->isSpecifiedPtr) *iter->isSpecifiedPtr = false; if (iter->argumentPtr) *iter->argumentPtr = nil; count++; } _longOptions = [[OFMapTable alloc] initWithKeyFunctions: keyFunctions valueFunctions: valueFunctions]; _options = [self allocMemoryWithSize: sizeof(*_options) count: count + 1]; for (iter = options, iter2 = _options; iter->shortOption != '\0' || iter->longOption != nil; iter++, iter2++) { iter2->shortOption = iter->shortOption; iter2->longOption = nil; iter2->hasArgument = iter->hasArgument; iter2->isSpecifiedPtr = iter->isSpecifiedPtr; iter2->argumentPtr = iter->argumentPtr; @try { iter2->longOption = [iter->longOption copy]; if ([_longOptions valueForKey: iter2->longOption] != NULL) @throw [OFInvalidArgumentException exception]; [_longOptions setValue: iter2 forKey: iter2->longOption]; } @catch (id e) { /* * Make sure we are in a consistent state where * dealloc works. */ [iter2->longOption release]; iter2->shortOption = '\0'; iter2->longOption = nil; @throw e; } } _arguments = [[OFApplication arguments] retain]; } @catch (id e) { [self release]; @throw e; } return self; } - (void)dealloc { of_options_parser_option_t *iter; [_longOptions release]; if (_options != NULL) for (iter = _options; iter->shortOption != '\0' || iter->longOption != nil; iter++) [iter->longOption release]; [_arguments release]; [_argument release]; [super dealloc]; } - (of_unichar_t)nextOption { of_options_parser_option_t *iter; OFString *argument; if (_done || _index >= [_arguments count]) return '\0'; [_lastLongOption release]; [_argument release]; _lastLongOption = nil; _argument = nil; argument = [_arguments objectAtIndex: _index]; if (_subIndex == 0) { if ([argument length] < 2 || [argument characterAtIndex: 0] != '-') { _done = true; return '\0'; } if ([argument isEqual: @"--"]) { _done = true; _index++; return '\0'; } if ([argument hasPrefix: @"--"]) { void *pool = objc_autoreleasePoolPush(); size_t pos; of_options_parser_option_t *option; _lastOption = '-'; _index++; if ((pos = [argument rangeOfString: @"="].location) != OF_NOT_FOUND) { of_range_t range = of_range(pos + 1, [argument length] - pos - 1); _argument = [[argument substringWithRange: range] copy]; } else pos = [argument length]; _lastLongOption = [[argument substringWithRange: of_range(2, pos - 2)] copy]; objc_autoreleasePoolPop(pool); option = [_longOptions valueForKey: _lastLongOption]; if (option == nil) return '?'; if (option->hasArgument == 1 && _argument == nil) return ':'; if (option->hasArgument == 0 && _argument != nil) return '='; if (option->isSpecifiedPtr != NULL) *option->isSpecifiedPtr = true; if (option->argumentPtr != NULL) *option->argumentPtr = [[_argument copy] autorelease]; if (option->shortOption != '\0') _lastOption = option->shortOption; return _lastOption; } _subIndex = 1; } _lastOption = [argument characterAtIndex: _subIndex++]; if (_subIndex >= [argument length]) { _index++; _subIndex = 0; } for (iter = _options; iter->shortOption != '\0' || iter->longOption != nil; iter++) { if (iter->shortOption == _lastOption) { if (iter->hasArgument == 0) return _lastOption; if (_index >= [_arguments count]) return ':'; argument = [_arguments objectAtIndex: _index]; argument = [argument substringWithRange: of_range(_subIndex, [argument length] - _subIndex)]; _argument = [argument copy]; if (iter->isSpecifiedPtr != NULL) *iter->isSpecifiedPtr = true; if (iter->argumentPtr != NULL) *iter->argumentPtr = [[_argument copy] autorelease]; _index++; _subIndex = 0; return _lastOption; } } return '?'; } - (of_unichar_t)lastOption { return _lastOption; } - (OFString*)lastLongOption { OF_GETTER(_lastLongOption, true) } - (OFString*)argument { OF_GETTER(_argument, true) } - (OFArray*)remainingArguments { return [_arguments objectsInRange: of_range(_index, [_arguments count] - _index)]; } @end |