aboutsummaryrefslogtreecommitdiff
path: root/redist/json_helpers.c
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 /redist/json_helpers.c
parent730bbd51f9aa0f0c6b57e9607d2b682ea1bf5ad2 (diff)
downloadlibsurvive-92d291674e5a12c7eedd314ffc4cf955d575cbea.tar.gz
libsurvive-92d291674e5a12c7eedd314ffc4cf955d575cbea.tar.bz2
add function to parse array of floats from json
Diffstat (limited to 'redist/json_helpers.c')
-rw-r--r--redist/json_helpers.c29
1 files changed, 29 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