From 9b3000e3296d2eefd2339b57f65085d16e905046 Mon Sep 17 00:00:00 2001 From: Zdenek Styblik Date: Wed, 12 Dec 2012 05:39:35 +0000 Subject: [PATCH] '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. --- ipmitool/include/ipmitool/helper.h | 1 + ipmitool/lib/helper.c | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/ipmitool/include/ipmitool/helper.h b/ipmitool/include/ipmitool/helper.h index bc2bc02..1388d27 100644 --- a/ipmitool/include/ipmitool/helper.h +++ b/ipmitool/include/ipmitool/helper.h @@ -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); diff --git a/ipmitool/lib/helper.c b/ipmitool/lib/helper.c index 73df753..49d5890 100644 --- a/ipmitool/lib/helper.c +++ b/ipmitool/lib/helper.c @@ -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