Enhance C89 compatibility by replacing strtof and strtold with strtod in conversion functions (#1207)
This commit is contained in:
+4
-1
@@ -77,7 +77,10 @@ The git repositories are hosted at the following sites:
|
|||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
|
||||||
* Changed RGB color clamp function to avoid Zephyr RTOS name collisions.(#1201)
|
* Changed bacnet_strtof and bacnet_strtold functions to use strtod to
|
||||||
|
improve compatibility with C89 standards while ensuring proper type
|
||||||
|
casting and range checking. (#1207)
|
||||||
|
* Changed RGB color clamp function to avoid Zephyr RTOS name collisions. (#1201)
|
||||||
* Changed the load control object AbleToMeetShed to only check for immediate
|
* Changed the load control object AbleToMeetShed to only check for immediate
|
||||||
shed ability and added CanNowComplyWithShed function to attempt to meet the
|
shed ability and added CanNowComplyWithShed function to attempt to meet the
|
||||||
shed request while in the non-compliant state. (#1191)
|
shed request while in the non-compliant state. (#1191)
|
||||||
|
|||||||
+15
-14
@@ -13,6 +13,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <float.h>
|
||||||
|
#include <math.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
/* BACnet Stack defines - first */
|
/* BACnet Stack defines - first */
|
||||||
@@ -1565,14 +1567,15 @@ bool bacnet_strtol(const char *str, long *long_value)
|
|||||||
* @param float_value - where to put the converted value
|
* @param float_value - where to put the converted value
|
||||||
* @return true if converted and finite value is set
|
* @return true if converted and finite value is set
|
||||||
* @return false if not converted and finite value is not set
|
* @return false if not converted and finite value is not set
|
||||||
|
* @note Uses strtod() for C89 compatibility and casts to float
|
||||||
*/
|
*/
|
||||||
bool bacnet_strtof(const char *str, float *float_value)
|
bool bacnet_strtof(const char *str, float *float_value)
|
||||||
{
|
{
|
||||||
char *endptr;
|
char *endptr;
|
||||||
float value;
|
double value;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
value = strtof(str, &endptr);
|
value = strtod(str, &endptr);
|
||||||
if (endptr == str) {
|
if (endptr == str) {
|
||||||
/* No digits found */
|
/* No digits found */
|
||||||
return false;
|
return false;
|
||||||
@@ -1586,8 +1589,12 @@ bool bacnet_strtof(const char *str, float *float_value)
|
|||||||
/* Extra text found */
|
/* Extra text found */
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
if (fabs(value) > FLT_MAX) {
|
||||||
|
/* Value exceeds float range */
|
||||||
|
return false;
|
||||||
|
}
|
||||||
if (float_value) {
|
if (float_value) {
|
||||||
*float_value = value;
|
*float_value = (float)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -1635,14 +1642,15 @@ bool bacnet_strtod(const char *str, double *double_value)
|
|||||||
* @param long_double_value - where to put the converted value
|
* @param long_double_value - where to put the converted value
|
||||||
* @return true if converted and finite value is set
|
* @return true if converted and finite value is set
|
||||||
* @return false if not converted and finite value is not set
|
* @return false if not converted and finite value is not set
|
||||||
|
* @note Uses strtod() for C89 compatibility and casts to long double
|
||||||
*/
|
*/
|
||||||
bool bacnet_strtold(const char *str, long double *long_double_value)
|
bool bacnet_strtold(const char *str, long double *long_double_value)
|
||||||
{
|
{
|
||||||
char *endptr;
|
char *endptr;
|
||||||
long double value;
|
double value;
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
value = strtold(str, &endptr);
|
value = strtod(str, &endptr);
|
||||||
if (endptr == str) {
|
if (endptr == str) {
|
||||||
/* No digits found */
|
/* No digits found */
|
||||||
return false;
|
return false;
|
||||||
@@ -1657,7 +1665,7 @@ bool bacnet_strtold(const char *str, long double *long_double_value)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (long_double_value) {
|
if (long_double_value) {
|
||||||
*long_double_value = value;
|
*long_double_value = (long double)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -1862,23 +1870,16 @@ bool bacnet_string_to_bool(const char *str, bool *bool_value)
|
|||||||
* @param unsigned_int - where to put the converted value
|
* @param unsigned_int - where to put the converted value
|
||||||
* @return true if converted and value is set
|
* @return true if converted and value is set
|
||||||
* @return false if not converted and value is not set
|
* @return false if not converted and value is not set
|
||||||
|
* @note the conversion is limited to the strtoul() range for C89 compatibility
|
||||||
*/
|
*/
|
||||||
bool bacnet_string_to_unsigned(
|
bool bacnet_string_to_unsigned(
|
||||||
const char *str, BACNET_UNSIGNED_INTEGER *unsigned_int)
|
const char *str, BACNET_UNSIGNED_INTEGER *unsigned_int)
|
||||||
{
|
{
|
||||||
char *endptr;
|
char *endptr;
|
||||||
#ifdef UINT64_MAX
|
|
||||||
unsigned long long value;
|
|
||||||
#else
|
|
||||||
unsigned long value;
|
unsigned long value;
|
||||||
#endif
|
|
||||||
|
|
||||||
errno = 0;
|
errno = 0;
|
||||||
#ifdef UINT64_MAX
|
|
||||||
value = strtoull(str, &endptr, 0);
|
|
||||||
#else
|
|
||||||
value = strtoul(str, &endptr, 0);
|
value = strtoul(str, &endptr, 0);
|
||||||
#endif
|
|
||||||
if (endptr == str) {
|
if (endptr == str) {
|
||||||
/* No digits found */
|
/* No digits found */
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user