(62 lines)
1 // This file defines some functions to make it easy to parse simply formatted2 // strings **correctly** without memory bugs.3 //4 // strparse(input_str, format...) - parse a string5 //6 // Examples:7 //8 // const char *filename; long line_num;9 // const char *err = NULL;10 // if ((err=strparse("foo.c:12", &filename, ":", &line_num)))11 // errx(1, "Failed to parse file:line at: ", err);12 //13 // const char *item1, *item2;14 // if ((err=strparse("one, two", &item1, ",", PARSE_WHITESPACE, &item2)))15 // errx(1, "Failed to parse items at: ", err);27 } some_char_t;30 typedef enum { PARSE_LITERAL, PARSE_LONG, PARSE_DOUBLE, PARSE_BOOL, PARSE_STRING, PARSE_SOME_OF } parse_type_e;33 parse_type_e type;35 } parse_element_t;39 some_char_t *: PARSE_SOME_OF, \51 (parse_type_e[]){MAP_LIST(_parse_type, __VA_ARGS__)}, (void *[]){MAP_LIST(as_void_star, __VA_ARGS__)})53 ({ \55 size_t _capacity; \57 const char *_parse_err = _just_read > 0 ? strparse(_file_contents, __VA_ARGS__) : "No such file"; \59 _parse_err; \60 })