can now generate basic *_to_json functions
This commit is contained in:
parent
cc159c2244
commit
a8e2a1e28f
3 changed files with 282 additions and 0 deletions
107
test_files/header.kwil.h
Normal file
107
test_files/header.kwil.h
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
#include <string.h>
|
||||
#define KWIL_GEN_IMPL(...)\
|
||||
int struct_A_to_json(struct struct_A* src, char** out_json) {\
|
||||
int json_capacity = 2; \
|
||||
/* length of b */\
|
||||
json_capacity += 5+ snprintf(NULL, 0, "%d", src->b);\
|
||||
/* length of a */\
|
||||
json_capacity += 5 + snprintf(NULL, 0, "%f", src->a);\
|
||||
/* length of u */\
|
||||
json_capacity += 5 + snprintf(NULL, 0, "%du", src->u);\
|
||||
/* length of dyn_str */\
|
||||
json_capacity += 11 + strlen(src->dyn_str);\
|
||||
char* json = malloc(json_capacity);\
|
||||
int json_len = 1;\
|
||||
strcpy(json, "{");\
|
||||
*out_json = json;\
|
||||
/* field: b */\
|
||||
json_len += sprintf(json + json_len, "\"b\":");\
|
||||
json_len += sprintf(json + json_len, "%d", src->b);\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: a */\
|
||||
json_len += sprintf(json + json_len, "\"a\":");\
|
||||
json_len += sprintf(json + json_len, "%f", src->a);\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: u */\
|
||||
json_len += sprintf(json + json_len, "\"u\":");\
|
||||
json_len += sprintf(json + json_len, "%u", src->u);\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: dyn_str */\
|
||||
json_len += sprintf(json + json_len, "\"dyn_str\":");\
|
||||
if(src->dyn_str != NULL) {\
|
||||
json_len += sprintf(json + json_len, "\"%s\"", src->dyn_str);\
|
||||
}\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
strcpy(json + json_len - 1, "}");\
|
||||
return json_capacity;\
|
||||
}\
|
||||
int struct_B_to_json(struct struct_B* src, char** out_json) {\
|
||||
int json_capacity = 2; \
|
||||
/* length of f */\
|
||||
json_capacity += 5 + snprintf(NULL, 0, "%f", src->f);\
|
||||
/* length of i */\
|
||||
json_capacity += 5+ snprintf(NULL, 0, "%d", src->i);\
|
||||
/* length of str */\
|
||||
json_capacity += 7 + strlen(src->str);\
|
||||
/* length of str_static */\
|
||||
json_capacity += 14 + strlen(src->str_static);\
|
||||
/* length of other_struct */\
|
||||
json_capacity += 16;\
|
||||
/* length of other_struct_typedef */\
|
||||
json_capacity += 24;\
|
||||
/* length of other_enum */\
|
||||
json_capacity += 14;\
|
||||
/* length of other_enum_typedef */\
|
||||
json_capacity += 22;\
|
||||
char* json = malloc(json_capacity);\
|
||||
int json_len = 1;\
|
||||
strcpy(json, "{");\
|
||||
*out_json = json;\
|
||||
/* field: f */\
|
||||
json_len += sprintf(json + json_len, "\"f\":");\
|
||||
json_len += sprintf(json + json_len, "%f", src->f);\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: i */\
|
||||
json_len += sprintf(json + json_len, "\"i\":");\
|
||||
json_len += sprintf(json + json_len, "%d", src->i);\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: str */\
|
||||
json_len += sprintf(json + json_len, "\"str\":");\
|
||||
if(src->str != NULL) {\
|
||||
json_len += sprintf(json + json_len, "\"%s\"", src->str);\
|
||||
}\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: str_static */\
|
||||
json_len += sprintf(json + json_len, "\"str_static\":");\
|
||||
if(src->str_static != NULL) {\
|
||||
json_len += sprintf(json + json_len, "\"%s\"", src->str_static);\
|
||||
}\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: other_struct */\
|
||||
json_len += sprintf(json + json_len, "\"other_struct\":");\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: other_struct_typedef */\
|
||||
json_len += sprintf(json + json_len, "\"other_struct_typedef\":");\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: other_enum */\
|
||||
json_len += sprintf(json + json_len, "\"other_enum\":");\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
/* field: other_enum_typedef */\
|
||||
json_len += sprintf(json + json_len, "\"other_enum_typedef\":");\
|
||||
strcpy(json + json_len, ",");\
|
||||
++json_len;\
|
||||
strcpy(json + json_len - 1, "}");\
|
||||
return json_capacity;\
|
||||
}\
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue