util: Add common procedures to get argument values for ptp4l and phc2sys

Signed-off-by: Ken ICHIKAWA <ichikawa.ken@jp.fujitsu.com>
master
Ken ICHIKAWA 2013-06-04 14:00:53 +09:00 committed by Richard Cochran
parent a92f42de38
commit 42cb449eaa
2 changed files with 91 additions and 0 deletions

54
util.c
View File

@ -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;
}

37
util.h
View File

@ -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