From 92d291674e5a12c7eedd314ffc4cf955d575cbea Mon Sep 17 00:00:00 2001 From: Joshua Allen Date: Sat, 6 May 2017 22:05:32 -0400 Subject: add function to parse array of floats from json --- redist/json_helpers.c | 29 +++++++++++++++++++++++++++++ redist/json_helpers.h | 4 ++++ 2 files changed, 33 insertions(+) diff --git a/redist/json_helpers.c b/redist/json_helpers.c index c52301d..af7ddda 100644 --- a/redist/json_helpers.c +++ b/redist/json_helpers.c @@ -216,3 +216,32 @@ void json_load_file(const char* path) { free(JSON_STRING); } +int parse_float_array(char* str, jsmntok_t* token, FLT** f, uint8_t count) { + uint16_t i = 0; + + if (count==0) return 0; + + if (*f!=NULL) free(*f); + *f = malloc(sizeof(FLT) * count); + + for(i=0;iend; + char* s = str+token->start; + + #ifdef USE_DOUBLE + (*f)[i] = strtod(s, &end); + #else + (*f)[i] = strtof(s, &end); + #endif + + if (s == end) { + free(*f); + *f=NULL; + return 0; //not a float + } + token++; + } + + + return count; +} \ No newline at end of file diff --git a/redist/json_helpers.h b/redist/json_helpers.h index 1cccfe3..3ebf66b 100644 --- a/redist/json_helpers.h +++ b/redist/json_helpers.h @@ -4,6 +4,8 @@ #define JSON_HELPERS_H #include +#include +#include "survive_types.h" void json_write_float_array(FILE* f, const char* tag, float* v, uint8_t count); void json_write_double_array(FILE* f, const char* tag, double* v, uint8_t count); @@ -11,6 +13,8 @@ void json_write_uint32(FILE* f, const char* tag, uint32_t v); void json_write_float(FILE* f, const char* tag, float v); void json_write_str(FILE* f, const char* tag, const char* v); +int parse_float_array(char* str, jsmntok_t* token, FLT** values, uint8_t count); + void json_load_file(const char* path); extern void (*json_begin_object)(char* tag); extern void (*json_end_object)(); -- cgit v1.2.3