From: poljar (Damir Jelić) Date: Fri, 1 Nov 2013 17:40:06 +0000 (+0100) Subject: strutil: Add helper functions: string to number. X-Git-Tag: libsigrok-0.3.0~515 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=9e4f8cf93b465fb34b35b083935f45ba5427045e;p=libsigrok.git strutil: Add helper functions: string to number. This patch adds helper functions for converting a string to different number formats (double, long, float, int). These functions are exposed in the public API. --- diff --git a/proto.h b/proto.h index 7ec6ae4f..5b516eca 100644 --- a/proto.h +++ b/proto.h @@ -141,6 +141,10 @@ SR_API uint64_t sr_parse_timestring(const char *timestring); SR_API gboolean sr_parse_boolstring(const char *boolstring); SR_API int sr_parse_period(const char *periodstr, uint64_t *p, uint64_t *q); SR_API int sr_parse_voltage(const char *voltstr, uint64_t *p, uint64_t *q); +SR_API int sr_atol(const char *str, long *ret); +SR_API int sr_atoi(const char *str, int *ret); +SR_API int sr_atod(const char *str, double *ret); +SR_API int sr_atof(const char *str, float *ret); /*--- version.c -------------------------------------------------------------*/ diff --git a/strutil.c b/strutil.c index f6cdb73b..6c653844 100644 --- a/strutil.c +++ b/strutil.c @@ -21,6 +21,7 @@ #include #include #include +#include #include "libsigrok.h" #include "libsigrok-internal.h" @@ -47,6 +48,126 @@ * @{ */ +/** + * Convert a string representation of a numeric value to a long integer. The + * conversion is strict and will fail if the complete string does not represent + * a valid long integer. The function sets errno according to the details of the + * failure. + * + * @param str The string representation to convert. + * @param ret Pointer to long where the result of the conversion will be stored. + * + * @return SR_OK if conversion is successful, otherwise SR_ERR. + * + * @since 0.3.0 + */ +SR_API int sr_atol(const char *str, long *ret) +{ + long tmp; + char *endptr = NULL; + + errno = 0; + tmp = strtol(str, &endptr, 0); + + if (!endptr || *endptr || errno) { + if (!errno) + errno = EINVAL; + return SR_ERR; + } + + *ret = tmp; + return SR_OK; +} + +/** + * Convert a string representation of a numeric value to an integer. The + * conversion is strict and will fail if the complete string does not represent + * a valid integer. The function sets errno according to the details of the + * failure. + * + * @param str The string representation to convert. + * @param ret Pointer to int where the result of the conversion will be stored. + * + * @return SR_OK if conversion is successful, otherwise SR_ERR. + * + * @since 0.3.0 + */ +SR_API int sr_atoi(const char *str, int *ret) +{ + long tmp; + + if (sr_atol(str, &tmp) != SR_OK) + return SR_ERR; + + if ((int) tmp != tmp) { + errno = ERANGE; + return SR_ERR; + } + + *ret = (int) tmp; + return SR_OK; +} + +/** + * Convert a string representation of a numeric value to a double. The + * conversion is strict and will fail if the complete string does not represent + * a valid double. The function sets errno according to the details of the + * failure. + * + * @param str The string representation to convert. + * @param ret Pointer to double where the result of the conversion will be stored. + * + * @return SR_OK if conversion is successful, otherwise SR_ERR. + * + * @since 0.3.0 + */ +SR_API int sr_atod(const char *str, double *ret) +{ + double tmp; + char *endptr = NULL; + + errno = 0; + tmp = strtof(str, &endptr); + + if (!endptr || *endptr || errno) { + if (!errno) + errno = EINVAL; + return SR_ERR; + } + + *ret = tmp; + return SR_OK; +} + +/** + * Convert a string representation of a numeric value to a float. The + * conversion is strict and will fail if the complete string does not represent + * a valid float. The function sets errno according to the details of the + * failure. + * + * @param str The string representation to convert. + * @param ret Pointer to float where the result of the conversion will be stored. + * + * @return SR_OK if conversion is successful, otherwise SR_ERR. + * + * @since 0.3.0 + */ +SR_API int sr_atof(const char *str, float *ret) +{ + double tmp; + + if (sr_atod(str, &tmp) != SR_OK) + return SR_ERR; + + if ((float) tmp != tmp) { + errno = ERANGE; + return SR_ERR; + } + + *ret = (float) tmp; + return SR_OK; +} + /** * Convert a numeric value value to its "natural" string representation * in SI units.