parses a comma separated list of words after a pattern in one of the arguments in argv
| 3411 | |
| 3412 | // parses a comma separated list of words after a pattern in one of the arguments in argv |
| 3413 | bool parseCommaSepArgs(int argc, const char* const* argv, const char* pattern, |
| 3414 | std::vector<String>& res) { |
| 3415 | String filtersString; |
| 3416 | if(parseOption(argc, argv, pattern, &filtersString)) { |
| 3417 | // tokenize with "," as a separator, unless escaped with backslash |
| 3418 | std::ostringstream s; |
| 3419 | auto flush = [&s, &res]() { |
| 3420 | auto string = s.str(); |
| 3421 | if(string.size() > 0) { |
| 3422 | res.push_back(string.c_str()); |
| 3423 | } |
| 3424 | s.str(""); |
| 3425 | }; |
| 3426 | |
| 3427 | bool seenBackslash = false; |
| 3428 | const char* current = filtersString.c_str(); |
| 3429 | const char* end = current + strlen(current); |
| 3430 | while(current != end) { |
| 3431 | char character = *current++; |
| 3432 | if(seenBackslash) { |
| 3433 | seenBackslash = false; |
| 3434 | if(character == ',' || character == '\\') { |
| 3435 | s.put(character); |
| 3436 | continue; |
| 3437 | } |
| 3438 | s.put('\\'); |
| 3439 | } |
| 3440 | if(character == '\\') { |
| 3441 | seenBackslash = true; |
| 3442 | } else if(character == ',') { |
| 3443 | flush(); |
| 3444 | } else { |
| 3445 | s.put(character); |
| 3446 | } |
| 3447 | } |
| 3448 | |
| 3449 | if(seenBackslash) { |
| 3450 | s.put('\\'); |
| 3451 | } |
| 3452 | flush(); |
| 3453 | return true; |
| 3454 | } |
| 3455 | return false; |
| 3456 | } |
| 3457 | |
| 3458 | enum optionType |
| 3459 | { |