1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
// (C) 2017 <>< Joshua Allen, Under MIT/x11 License.
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json_helpers.h"
void json_write_float_array(FILE* f, const char* tag, float* v, uint8_t count) {
uint8_t i = 0;
char * str1 = NULL;
char * str2 = NULL;
asprintf(&str1,"\"%s\":[", tag);
for (i=0;i<count;++i) {
if (i<(count-1)) {
asprintf(&str2, "%s\"%f\",", str1,v[i]);
} else {
asprintf(&str2, "%s\"%f\"", str1,v[i]);
}
free(str1);
str1=str2;
str2=NULL;
}
asprintf(&str2, "%s]", str1);
fputs(str2,f);
free(str1);
free(str2);
}
void json_write_double_array(FILE* f, const char* tag, double* v, uint8_t count) {
uint8_t i = 0;
char * str1 = NULL;
char * str2 = NULL;
asprintf(&str1,"\"%s\":[", tag);
for (i=0;i<count;++i) {
if (i<(count-1)) {
asprintf(&str2, "%s\"%f\",", str1,v[i]);
} else {
asprintf(&str2, "%s\"%f\"", str1,v[i]);
}
free(str1);
str1=str2;
str2=NULL;
}
asprintf(&str2, "%s]", str1);
fputs(str2,f);
free(str1);
free(str2);
}
void json_write_uint32(FILE* f, const char* tag, uint32_t v) {
fprintf(f, "\"%s\":\"%d\"", tag, v);
}
void json_write_float(FILE* f, const char* tag, float v) {
fprintf(f, "\"%s\":\"%f\"", tag, v);
}
void json_write_str(FILE* f, const char* tag, const char* v) {
fprintf(f, "\"%s\":\"%s\"", tag, v);
}
|