From e3f9fe2d404ca10153e95499ece111c077b6690a Mon Sep 17 00:00:00 2001 From: Eduardo Habkost Date: Mon, 4 Feb 2013 16:27:45 -0200 Subject: cutils: unsigned int parsing functions There are lots of duplicate parsing code using strto*() in QEMU, and most of that code is broken in one way or another. Even the visitors code have duplicate integer parsing code[1]. This introduces functions to help parsing unsigned int values: parse_uint() and parse_uint_full(). Parsing functions for signed ints and floats will be submitted later. parse_uint_full() has all the checks made by opts_type_uint64() at opts-visitor.c: - Check for NULL (returns -EINVAL) - Check for negative numbers (returns -EINVAL) - Check for empty string (returns -EINVAL) - Check for overflow or other errno values set by strtoll() (returns -errno) - Check for end of string (reject invalid characters after number) (returns -EINVAL) parse_uint() does everything above except checking for the end of the string, so callers can continue parsing the remainder of string after the number. Unit tests included. [1] string-input-visitor.c:parse_int() could use the same parsing code used by opts-visitor.c:opts_type_int(), instead of duplicating that logic. Signed-off-by: Eduardo Habkost Reviewed-by: Eric Blake Reviewed-by: Laszlo Ersek Signed-off-by: Anthony Liguori --- include/qemu-common.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'include/qemu-common.h') diff --git a/include/qemu-common.h b/include/qemu-common.h index af2379ff38..80016adae3 100644 --- a/include/qemu-common.h +++ b/include/qemu-common.h @@ -173,6 +173,10 @@ int qemu_fdatasync(int fd); int fcntl_setfl(int fd, int flag); int qemu_parse_fd(const char *param); +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); + /* * strtosz() suffixes used to specify the default treatment of an * argument passed to strtosz() without an explicit suffix. -- cgit v1.2.1