aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Allen <axlecrusher@gmail.com>2017-05-06 22:05:32 -0400
committerJoshua Allen <axlecrusher@gmail.com>2017-05-06 22:05:32 -0400
commit92d291674e5a12c7eedd314ffc4cf955d575cbea (patch)
tree6d19b86d482e384e827ff53fb5deedca5e07f4e7
parent730bbd51f9aa0f0c6b57e9607d2b682ea1bf5ad2 (diff)
downloadlibsurvive-92d291674e5a12c7eedd314ffc4cf955d575cbea.tar.gz
libsurvive-92d291674e5a12c7eedd314ffc4cf955d575cbea.tar.bz2
add function to parse array of floats from json
-rw-r--r--redist/json_helpers.c29
-rw-r--r--redist/json_helpers.h4
2 files changed, 33 insertions, 0 deletions
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;i<count;++i) {
+ char* end = str + token->end;
+ 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 <stdint.h>
+#include <jsmn.h>
+#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)();