1 /* SPDX-License-Identifier: GPL-2.0-or-later */ 2 /* 3 * Copyright (c) 2015-2024 Cyril Hrubis <[email protected]> 4 */ 5 6 /** 7 * DOC: Option parsing functions 8 * 9 * Implements simple helpers on the top of the strtol() and strtod() for 10 * command line option parsing. 11 */ 12 13 #ifndef TST_PARSE_H__ 14 #define TST_PARSE_H__ 15 16 /** 17 * tst_parse_int() - Parse an integer from a string. 18 * 19 * @str: A string with an integer number. 20 * @val: A pointer to integer to store the result to. 21 * @min: A lower bound, pass INT_MIN for full range. 22 * @max: An upper bound, pass INT_MAX for full range. 23 * return: A zero if whole string was consumed and the value was within bounds, 24 * an errno otherwise. 25 */ 26 int tst_parse_int(const char *str, int *val, int min, int max); 27 28 /** 29 * tst_parse_long() - Parse a long integer from a string. 30 * 31 * @str: A string with an integer number. 32 * @val: A pointer to long integer to store the result to. 33 * @min: A lower bound, pass LONG_MIN for full range. 34 * @max: An upper bound, pass LONG_MAX for full range. 35 * return: A zero if whole string was consumed and the value was within bounds, 36 * an errno otherwise. 37 */ 38 int tst_parse_long(const char *str, long *val, long min, long max); 39 40 /** 41 * tst_parse_float() - Parse a floating point number from a string. 42 * 43 * @str: A string with a floating point number. 44 * @val: A pointer to float to store the result to. 45 * @min: A lower bound. 46 * @max: An upper bound. 47 * return: A zero if whole string was consumed and the value was within bounds, 48 * an errno otherwise. 49 */ 50 int tst_parse_float(const char *str, float *val, float min, float max); 51 52 /** 53 * tst_parse_filesize() - Parse a file size from a string. 54 * 55 * @str: A string a positive number optionally followed by an unit, i.e. K, M, 56 * or G for kilobytes, megabytes and gigabytes. 57 * @val: A pointer to long long integer to store the size in bytes to. 58 * @min: A lower bound. 59 * @max: An upper bound. 60 * return: A zero if whole string was consumed and the value was within bounds, 61 * an errno otherwise. 62 */ 63 int tst_parse_filesize(const char *str, long long *val, long long min, long long max); 64 65 #endif /* TST_PARSE_H__ */ 66