diff --git a/util.c b/util.c index 45cf5ba..8e8d5a5 100644 --- a/util.c +++ b/util.c @@ -234,3 +234,57 @@ enum parser_result get_ranged_double(const char *str_val, double *result, double *result = parsed_val; return PARSED_OK; } + +int get_arg_val_i(int op, const char *optarg, int *val, int min, int max) +{ + enum parser_result r; + r = get_ranged_int(optarg, val, min, max); + if (r == MALFORMED) { + fprintf(stderr, + "-%c: %s is a malformed value\n", op, optarg); + return -1; + } + if (r == OUT_OF_RANGE) { + fprintf(stderr, + "-%c: %s is out of range. Must be in the range %d to %d\n", + op, optarg, min, max); + return -1; + } + return 0; +} + +int get_arg_val_ui(int op, const char *optarg, unsigned int *val, unsigned int min, unsigned int max) +{ + enum parser_result r; + r = get_ranged_uint(optarg, val, min, max); + if (r == MALFORMED) { + fprintf(stderr, + "-%c: %s is a malformed value\n", op, optarg); + return -1; + } + if (r == OUT_OF_RANGE) { + fprintf(stderr, + "-%c: %s is out of range. Must be in the range %u to %u\n", + op, optarg, min, max); + return -1; + } + return 0; +} + +int get_arg_val_d(int op, const char *optarg, double *val, double min, double max) +{ + enum parser_result r; + r = get_ranged_double(optarg, val, min, max); + if (r == MALFORMED) { + fprintf(stderr, + "-%c: %s is a malformed value\n", op, optarg); + return -1; + } + if (r == OUT_OF_RANGE) { + fprintf(stderr, + "-%c: %s is out of range. Must be in the range %e to %e\n", + op, optarg, min, max); + return -1; + } + return 0; +} diff --git a/util.h b/util.h index ea14d3b..62a56de 100644 --- a/util.h +++ b/util.h @@ -160,4 +160,41 @@ enum parser_result get_ranged_uint(const char *str_val, unsigned int *result, un * OUT_OF_RANGE if str_val is out of range. */ enum parser_result get_ranged_double(const char *str_val, double *result, double min, double max); + +/** + * Common procedure to get an int value from argument for ptp4l and phc2sys. + * + * @param op Character code of an option. + * @param optarg Option argument string. + * @param val Parsed value is stored in here. + * @param min Lower limit. Return -1 if parsed value is less than min. + * @param max Upper limit. Return -1 if parsed value is bigger than max. + * @return 0 on success, -1 if some error occurs. + */ +int get_arg_val_i(int op, const char *optarg, int *val, int min, int max); + +/** + * Common procedure to get an unsigned int value from argument for ptp4l + * and phc2sys. + * + * @param op Character code of an option. + * @param optarg Option argument string. + * @param val Parsed value is stored in here. + * @param min Lower limit. Return -1 if parsed value is less than min. + * @param max Upper limit. Return -1 if parsed value is bigger than max. + * @return 0 on success, -1 if some error occurs. + */ +int get_arg_val_ui(int op, const char *optarg, unsigned int *val, unsigned int min, unsigned int max); + +/** + * Common procedure to get a double value from argument for ptp4l and phc2sys. + * + * @param op Character code of an option. + * @param optarg Option argument string. + * @param val Parsed value is stored in here. + * @param min Lower limit. Return -1 if parsed value is less than min. + * @param max Upper limit. Return -1 if parsed value is bigger than max. + * @return 0 on success, -1 if some error occurs. + */ +int get_arg_val_d(int op, const char *optarg, double *val, double min, double max); #endif