|
NAMEparse_ranges - function to parse a string formatted like 'min:max:mult,...'SYNOPSISint parse_ranges(char *str, int defmin, int defmax, int defmult, int (*parse_func)(), char **rangeptr, char **errptr); int range_min(char *rbuf, int r); int range_max(char *rbuf, int r); int range_mult(char *rbuf, int r); DESCRIPTIONparse_ranges() is a function to parse a comma-separated list of range tokens each having the following form:num or min:max[:mult] any of the values may be blank (ie. min::mult, :max, etc.) and default values for missing arguments may be supplied by the caller. The special first form is short hand for 'num:num'. After parsing the string, the ranges are put into an array of integers, which is malloc'd by the routine. The min, max, and mult entries of each range can be extracted from the array using the range_min(), range_max(), and range_mult() functions. If range_ptr is not NULL, and parse_ranges() successfully parses the range string (ie. does not return -1), *range_ptr will point to space malloc'd by parse_ranges(). The user may free this space by calling free(). parse_ranges() parameters are:
EXAMPLES/* * simple example to take a list of ranges on the cmdline (in argv[1]), and * print a random number from within that range. */ #include <stdio.h> main() { extern int parse_ranges(), range_min(), range_max(), range_mult(); extern long random_range(), random_range_seed(); int min, max, mult, nranges; char *ep, *rp; random_range_seed(getpid()); if ((nranges = parse_ranges(argv[1], 0, INT_MAX, 1, NULL, &rp, &ep)) < 0) { fprintf(stderr, "parse_ranges() failed: %s0, ep); exit(1); } range = random_range(0, nranges-1, 1); min = range_min(rp, range); max = range_max(rp, range); mult = range_mult(rp, range); fprintf("%d\n", random_range(min, max-1, mult)); exit(0); } SEE ALSOrandom_range(3), random_range_seed(3), bytes_by_prefix(3).DIAGNOSTICSparse_ranges() returns -1 on error or the number of ranges parsed. No space will be malloc'd if parse_ranges() fails. Error messages are passed back through the errptr parameter. There are no error conditions for range_min(), range_max(), or range_mult().
Visit the GSP FreeBSD Man Page Interface. |