'lib/helper.c', 'include/ipmitool/helper.h' - add str2double()

Commit adds function to handle conversion from string to double. Function has
the same concept as other str2*() functions.
This commit is contained in:
Zdenek Styblik 2012-12-12 05:39:35 +00:00
parent bd851bab8f
commit 9b3000e329
2 changed files with 28 additions and 0 deletions

View File

@ -65,6 +65,7 @@ struct oemvalstr {
const char * val2str(uint16_t val, const struct valstr * vs);
const char * oemval2str(uint32_t oem,uint16_t val, const struct oemvalstr * vs);
int str2double(const char * str, double * double_ptr);
int str2long(const char * str, int64_t * lng_ptr);
int str2ulong(const char * str, uint64_t * ulng_ptr);
int str2int(const char * str, int32_t * int_ptr);

View File

@ -145,6 +145,33 @@ const char * oemval2str(uint32_t oem, uint16_t val,
return un_str;
}
/* str2double - safely convert string to double
*
* @str: source string to convert from
* @double_ptr: pointer where to store result
*
* returns zero on success
* returns (-1) if one of args is NULL, (-2) invalid input, (-3) for *flow
*/
int str2double(const char * str, double * double_ptr)
{
char * end_ptr = 0;
if (!str || !double_ptr)
return (-1);
*double_ptr = 0;
errno = 0;
*double_ptr = strtod(str, &end_ptr);
if (*end_ptr != '\0')
return (-2);
if (errno != 0)
return (-3);
return 0;
} /* str2double(...) */
/* str2long - safely convert string to int64_t
*
* @str: source string to convert from