Skip to content

Commit

Permalink
sys/config: Add CONF_FLOAT to config
Browse files Browse the repository at this point in the history
CONF_FLOAT was defined as a type but not implemented. This change
adds CONF_FLOAT.
  • Loading branch information
wes3 committed Jul 28, 2023
1 parent acdc81f commit 5f58e32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc/baselibc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions sys/config/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <float.h>

#include "os/mynewt.h"
#include "base64/base64.h"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 5f58e32

Please sign in to comment.