This is an automated email from the ASF dual-hosted git repository.
wes3 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
The following commit(s) were added to refs/heads/master by this push:
new ed7a54835 sys/config: Add CONF_FLOAT
ed7a54835 is described below
commit ed7a54835e4efae2937cb75ce9cb6e61dbc97955
Author: Will San Filippo <[email protected]>
AuthorDate: Fri Jul 28 12:18:32 2023 -0400
sys/config: Add CONF_FLOAT
CONF_FLOAT was defined as a type but not implemented. This change
adds CONF_FLOAT.
---
libc/baselibc/include/stdlib.h | 1 +
sys/config/src/config.c | 18 ++++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/libc/baselibc/include/stdlib.h b/libc/baselibc/include/stdlib.h
index d80a0f8a5..071342735 100644
--- a/libc/baselibc/include/stdlib.h
+++ b/libc/baselibc/include/stdlib.h
@@ -57,6 +57,7 @@ __extern long strtol(const char *, char **, int);
__extern long long strtoll(const char *, char **, int);
__extern unsigned long strtoul(const char *, char **, int);
__extern unsigned long long strtoull(const char *, char **, int);
+__extern float strtof(const char *, char **);
typedef int (*__comparefunc_t) (const void *, const void *);
__extern void *bsearch(const void *, const void *, size_t, size_t,
diff --git a/sys/config/src/config.c b/sys/config/src/config.c
index a1b08e07a..cb35d56b0 100644
--- a/sys/config/src/config.c
+++ b/sys/config/src/config.c
@@ -19,6 +19,8 @@
#include <string.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <float.h>
#include "os/mynewt.h"
#include "base64/base64.h"
@@ -154,6 +156,7 @@ conf_value_from_str(char *val_str, enum conf_type type,
void *vp, int maxlen)
{
int64_t val;
uint64_t uval;
+ float fval;
char *eptr;
if (!val_str) {
@@ -220,6 +223,16 @@ conf_value_from_str(char *val_str, enum conf_type type,
void *vp, int maxlen)
*(uint64_t *)vp = uval;
}
break;
+ case CONF_FLOAT:
+ fval = strtof(val_str, &eptr);
+ if (*eptr != '\0') {
+ goto err;
+ }
+ if ((fval < FLT_MIN) || (fval > FLT_MAX)) {
+ goto err;
+ }
+ *(float *)vp = fval;
+ break;
case CONF_STRING:
val = strlen(val_str);
if (val + 1 > maxlen) {
@@ -256,6 +269,7 @@ conf_str_from_value(enum conf_type type, void *vp, char
*buf, int buf_len)
{
int64_t val;
uint64_t uval;
+ float fval;
if (type == CONF_STRING) {
return vp;
@@ -294,6 +308,10 @@ conf_str_from_value(enum conf_type type, void *vp, char
*buf, int buf_len)
}
snprintf(buf, buf_len, "%llu", uval);
return buf;
+ case CONF_FLOAT:
+ fval = *(float *)vp;
+ snprintf(buf, buf_len, "%f", fval);
+ return buf;
default:
return NULL;
}