util/cutils: New qemu_strtosz()

Most callers of qemu_strtosz_suffix() pass QEMU_STRTOSZ_DEFSUFFIX_B.
Capture the pattern in new qemu_strtosz().

Inline qemu_strtosz_suffix() into its only remaining caller.

Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <1487708048-2131-17-git-send-email-armbru@redhat.com>
This commit is contained in:
Markus Armbruster 2017-02-21 21:14:00 +01:00
parent e591591b32
commit 466dea14e6
6 changed files with 30 additions and 41 deletions

View File

@ -139,22 +139,7 @@ int parse_uint(const char *s, unsigned long long *value, char **endptr,
int base);
int parse_uint_full(const char *s, unsigned long long *value, int base);
/*
* qemu_strtosz() suffixes used to specify the default treatment of an
* argument passed to qemu_strtosz() without an explicit suffix.
* These should be defined using upper case characters in the range
* A-Z, as qemu_strtosz() will use qemu_toupper() on the given argument
* prior to comparison.
*/
#define QEMU_STRTOSZ_DEFSUFFIX_EB 'E'
#define QEMU_STRTOSZ_DEFSUFFIX_PB 'P'
#define QEMU_STRTOSZ_DEFSUFFIX_TB 'T'
#define QEMU_STRTOSZ_DEFSUFFIX_GB 'G'
#define QEMU_STRTOSZ_DEFSUFFIX_MB 'M'
#define QEMU_STRTOSZ_DEFSUFFIX_KB 'K'
#define QEMU_STRTOSZ_DEFSUFFIX_B 'B'
int64_t qemu_strtosz_suffix(const char *nptr, char **end,
const char default_suffix);
int64_t qemu_strtosz(const char *nptr, char **end);
int64_t qemu_strtosz_MiB(const char *nptr, char **end);
int64_t qemu_strtosz_metric(const char *nptr, char **end);

View File

@ -489,8 +489,7 @@ opts_type_size(Visitor *v, const char *name, uint64_t *obj, Error **errp)
return;
}
val = qemu_strtosz_suffix(opt->str ? opt->str : "", &endptr,
QEMU_STRTOSZ_DEFSUFFIX_B);
val = qemu_strtosz(opt->str ? opt->str : "", &endptr);
if (val < 0 || *endptr) {
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, opt->name,
"a size value representible as a non-negative int64");

View File

@ -462,8 +462,7 @@ static int img_create(int argc, char **argv)
if (optind < argc) {
int64_t sval;
char *end;
sval = qemu_strtosz_suffix(argv[optind++], &end,
QEMU_STRTOSZ_DEFSUFFIX_B);
sval = qemu_strtosz(argv[optind++], &end);
if (sval < 0 || *end) {
if (sval == -ERANGE) {
error_report("Image size must be less than 8 EiB!");
@ -1865,7 +1864,7 @@ static int img_convert(int argc, char **argv)
{
int64_t sval;
char *end;
sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
sval = qemu_strtosz(optarg, &end);
if (sval < 0 || *end) {
error_report("Invalid minimum zero buffer size for sparse output specified");
ret = -1;
@ -3653,8 +3652,7 @@ static int img_bench(int argc, char **argv)
{
char *end;
errno = 0;
offset = qemu_strtosz_suffix(optarg, &end,
QEMU_STRTOSZ_DEFSUFFIX_B);
offset = qemu_strtosz(optarg, &end);
if (offset < 0|| *end) {
error_report("Invalid offset specified");
return 1;
@ -3670,7 +3668,7 @@ static int img_bench(int argc, char **argv)
int64_t sval;
char *end;
sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
sval = qemu_strtosz(optarg, &end);
if (sval < 0 || sval > INT_MAX || *end) {
error_report("Invalid buffer size specified");
return 1;
@ -3684,7 +3682,7 @@ static int img_bench(int argc, char **argv)
int64_t sval;
char *end;
sval = qemu_strtosz_suffix(optarg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
sval = qemu_strtosz(optarg, &end);
if (sval < 0 || sval > INT_MAX || *end) {
error_report("Invalid step size specified");
return 1;
@ -3847,7 +3845,7 @@ static int img_dd_bs(const char *arg,
char *end;
int64_t res;
res = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(arg, &end);
if (res <= 0 || res > INT_MAX || *end) {
error_report("invalid number: '%s'", arg);
@ -3864,7 +3862,7 @@ static int img_dd_count(const char *arg,
{
char *end;
dd->count = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
dd->count = qemu_strtosz(arg, &end);
if (dd->count < 0 || *end) {
error_report("invalid number: '%s'", arg);
@ -3898,7 +3896,7 @@ static int img_dd_skip(const char *arg,
{
char *end;
in->offset = qemu_strtosz_suffix(arg, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
in->offset = qemu_strtosz(arg, &end);
if (in->offset < 0 || *end) {
error_report("invalid number: '%s'", arg);

View File

@ -140,7 +140,7 @@ static int64_t cvtnum(const char *s)
char *end;
int64_t ret;
ret = qemu_strtosz_suffix(s, &end, QEMU_STRTOSZ_DEFSUFFIX_B);
ret = qemu_strtosz(s, &end);
if (*end != '\0') {
/* Detritus at the end of the string */
return -EINVAL;

View File

@ -1391,27 +1391,27 @@ static void test_qemu_strtosz_simple(void)
/* Note: precision is 53 bits since we're parsing with strtod() */
str = "9007199254740991"; /* 2^53-1 */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, 0x1fffffffffffff);
g_assert(endptr == str + 16);
str = "9007199254740992"; /* 2^53 */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, 0x20000000000000);
g_assert(endptr == str + 16);
str = "9007199254740993"; /* 2^53+1 */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
g_assert(endptr == str + 16);
str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, 0x7ffffffffffffc00);
g_assert(endptr == str + 19);
str = "9223372036854775295"; /* 0x7ffffffffffffdff */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */
g_assert(endptr == str + 19);
@ -1528,17 +1528,17 @@ static void test_qemu_strtosz_erange(void)
g_assert(endptr == str + 2);
str = "9223372036854775296"; /* 0x7ffffffffffffe00 */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, -ERANGE);
g_assert(endptr == str + 19);
str = "9223372036854775807"; /* 2^63-1 */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, -ERANGE);
g_assert(endptr == str + 19);
str = "9223372036854775808"; /* 2^63 */
res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, -ERANGE);
g_assert(endptr == str + 19);

View File

@ -178,6 +178,14 @@ int fcntl_setfl(int fd, int flag)
}
#endif
#define QEMU_STRTOSZ_DEFSUFFIX_EB 'E'
#define QEMU_STRTOSZ_DEFSUFFIX_PB 'P'
#define QEMU_STRTOSZ_DEFSUFFIX_TB 'T'
#define QEMU_STRTOSZ_DEFSUFFIX_GB 'G'
#define QEMU_STRTOSZ_DEFSUFFIX_MB 'M'
#define QEMU_STRTOSZ_DEFSUFFIX_KB 'K'
#define QEMU_STRTOSZ_DEFSUFFIX_B 'B'
static int64_t suffix_mul(char suffix, int64_t unit)
{
switch (qemu_toupper(suffix)) {
@ -248,15 +256,14 @@ fail:
return retval;
}
int64_t qemu_strtosz_suffix(const char *nptr, char **end,
const char default_suffix)
int64_t qemu_strtosz(const char *nptr, char **end)
{
return do_strtosz(nptr, end, default_suffix, 1024);
return do_strtosz(nptr, end, QEMU_STRTOSZ_DEFSUFFIX_B, 1024);
}
int64_t qemu_strtosz_MiB(const char *nptr, char **end)
{
return qemu_strtosz_suffix(nptr, end, QEMU_STRTOSZ_DEFSUFFIX_MB);
return do_strtosz(nptr, end, QEMU_STRTOSZ_DEFSUFFIX_MB, 1024);
}
int64_t qemu_strtosz_metric(const char *nptr, char **end)