133 lines
4.7 KiB
C
133 lines
4.7 KiB
C
#include <string.h>
|
|
#define KWIL_GEN_IMPL(...)\
|
|
size_t enum_A_json_length(enum enum_A* src) {\
|
|
switch(*src) {\
|
|
case VALUE_A: return 7;\
|
|
case VALUE_B: return 7;\
|
|
case VALUE_C: return 7;\
|
|
}\
|
|
return 2;\
|
|
}\
|
|
size_t enum_A_to_json(enum enum_A* src, char* out) {\
|
|
switch(*src) {\
|
|
case VALUE_A:\
|
|
return sprintf(out, "\"VALUE_A\"");\
|
|
case VALUE_B:\
|
|
return sprintf(out, "\"VALUE_B\"");\
|
|
case VALUE_C:\
|
|
return sprintf(out, "\"VALUE_C\"");\
|
|
}\
|
|
return sprintf(out, "\"\"");\
|
|
}\
|
|
size_t struct_A_json_length(struct struct_A* src) {\
|
|
size_t 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);\
|
|
return json_capacity;\
|
|
}\
|
|
int struct_A_to_json(struct struct_A* src, char* json) {\
|
|
int json_len = 1;\
|
|
strcpy(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_len;\
|
|
}\
|
|
size_t struct_B_json_length(struct struct_B* src) {\
|
|
size_t 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 + struct_A_json_length(&src->other_struct);\
|
|
/* length of other_struct_typedef */\
|
|
json_capacity += 24 + struct_A_json_length(&src->other_struct_typedef);\
|
|
/* length of other_enum */\
|
|
json_capacity += 14 + enum_A_json_length(&src->other_enum);\
|
|
/* length of other_enum_typedef */\
|
|
json_capacity += 22 + enum_A_json_length(&src->other_enum_typedef);\
|
|
return json_capacity;\
|
|
}\
|
|
int struct_B_to_json(struct struct_B* src, char* json) {\
|
|
int json_len = 1;\
|
|
strcpy(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\":");\
|
|
json_len += struct_A_to_json(&src->other_struct, json + json_len);\
|
|
strcpy(json + json_len, ",");\
|
|
++json_len;\
|
|
/* field: other_struct_typedef */\
|
|
json_len += sprintf(json + json_len, "\"other_struct_typedef\":");\
|
|
json_len += struct_A_to_json(&src->other_struct_typedef, json + json_len);\
|
|
strcpy(json + json_len, ",");\
|
|
++json_len;\
|
|
/* field: other_enum */\
|
|
json_len += sprintf(json + json_len, "\"other_enum\":");\
|
|
json_len += enum_A_to_json(&src->other_enum, json + json_len);\
|
|
strcpy(json + json_len, ",");\
|
|
++json_len;\
|
|
/* field: other_enum_typedef */\
|
|
json_len += sprintf(json + json_len, "\"other_enum_typedef\":");\
|
|
json_len += enum_A_to_json(&src->other_enum_typedef, json + json_len);\
|
|
strcpy(json + json_len, ",");\
|
|
++json_len;\
|
|
strcpy(json + json_len - 1, "}");\
|
|
return json_len;\
|
|
}\
|
|
|