mirror of
https://github.com/projectacrn/acrn-hypervisor.git
synced 2025-09-09 12:49:24 +00:00
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=<start lba>/<subfile size> 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 <conghui.chen@intel.com> Reviewed-by: Shuo Liu <shuo.a.liu@intel.com> Acked-by: Yin Fengwei <fengwei.yin@intel.com>
This commit is contained in:
67
devicemodel/include/dm_string.h
Normal file
67
devicemodel/include/dm_string.h
Normal file
@@ -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
|
Reference in New Issue
Block a user