From e1dab512c25765f57a41da311f1b4209bcb859c7 Mon Sep 17 00:00:00 2001 From: Conghui Chen Date: Wed, 17 Oct 2018 02:50:04 +0800 Subject: [PATCH] dm: add string convert API As function sscanf is banned, to get value from parameter buffer,strto* is recommended. To reduce the inspection code when using strto*, it's better to use a string convert API. Usage: For virtio-blk, it has parameters: range=/ sscanf: if (sscanf(cp, "range=%ld/%ld", &sub_file_start_lba, &sub_file_size) == 2) sub_file_assign = 1; string API: if (strsep(&cp, "=") && !dm_strtol(cp, &cp, 10, &sub_file_start_lba) && *cp == '/' && !dm_strtol(cp + 1, &cp, 10, &sub_file_size)) sub_file_assign = 1; Tracked-on: #1496 Signed-off-by: Conghui Chen Reviewed-by: Shuo Liu Acked-by: Yin Fengwei --- devicemodel/Makefile | 3 ++ devicemodel/include/dm_string.h | 67 ++++++++++++++++++++++++++++++++ devicemodel/lib/dm_string.c | 69 +++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 devicemodel/include/dm_string.h create mode 100644 devicemodel/lib/dm_string.c diff --git a/devicemodel/Makefile b/devicemodel/Makefile index 134e27ea7..9aa229e58 100644 --- a/devicemodel/Makefile +++ b/devicemodel/Makefile @@ -56,6 +56,9 @@ LIBS += -luuid LIBS += -lusb-1.0 LIBS += -lacrn-mngr +# lib +SRCS += lib/dm_string.c + # hw SRCS += hw/block_if.c SRCS += hw/usb_core.c diff --git a/devicemodel/include/dm_string.h b/devicemodel/include/dm_string.h new file mode 100644 index 000000000..bfb8a487d --- /dev/null +++ b/devicemodel/include/dm_string.h @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + * + */ + +#ifndef _DM_STRING_H_ +#define _DM_STRING_H_ + +/** + * @brief Convert string to a long integer. + * + * @param s Pointer to original string. + * @param end Pointer to end string. + * @param base Base: 8, 10, 16... + * @param val Long integer convert from string. + * + * @return -1 error. + * @return 0 no error. + */ + +int dm_strtol(char *s, char **end, unsigned int base, long *val); + +/** + * @brief Convert string to an integer. + * + * @param s Pointer to original string. + * @param end Pointer to end string. + * @param base Base: 8, 10, 16... + * @param val Integer convert from string. + * + * @return -1 error. + * @return 0 no error. + */ + +int dm_strtoi(char *s, char **end, unsigned int base, int *val); + +/** + * @brief Convert string to an unsigned long integer. + * + * @param s Pointer to original string. + * @param end Pointer to end string. + * @param base Base: 8, 10, 16... + * @param val Unsigned long integer convert from string. + * + * @return -1 error. + * @return 0 no error. + */ + +int dm_strtoul(char *s, char **end, unsigned int base, unsigned long *val); + +/** + * @brief Convert string to an unsigned integer. + * + * @param s Pointer to original string. + * @param end Pointer to end string after strtol. + * @param base Base: 8, 10, 16... + * @param val Unsigned integer convert from string. + * + * @return -1 error. + * @return 0 no error. + */ + +int dm_strtoui(char *s, char **end, unsigned int base, unsigned int *val); + +#endif diff --git a/devicemodel/lib/dm_string.c b/devicemodel/lib/dm_string.c new file mode 100644 index 000000000..4192b3c34 --- /dev/null +++ b/devicemodel/lib/dm_string.c @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2018 Intel Corporation. All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + * + */ + +#include +#include +#include +#include "dm_string.h" + +int +dm_strtol(char *s, char **end, unsigned int base, long *val) +{ + if (!s) + goto err; + + *val = strtol(s, end, base); + if (*end == s) { + printf("ERROR! nothing covert for: %s!\n", s); + goto err; + } + return 0; + +err: + return -1; +} + +int +dm_strtoi(char *s, char **end, unsigned int base, int *val) +{ + long l_val; + int ret; + + l_val = 0; + ret = dm_strtol(s, end, base, &l_val); + *val = (int)l_val; + return ret; +} + +int +dm_strtoul(char *s, char **end, unsigned int base, unsigned long *val) +{ + if (!s) + goto err; + + *val = strtoul(s, end, base); + if (*end == s) { + printf("ERROR! nothing covert for: %s!\n", s); + goto err; + } + return 0; + +err: + return -1; +} + +int +dm_strtoui(char *s, char **end, unsigned int base, unsigned int *val) +{ + unsigned long l_val; + int ret; + + l_val = 0; + ret = dm_strtoul(s, end, base, &l_val); + *val = (unsigned int)l_val; + return ret; +}