300 lines
11 KiB
C
300 lines
11 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 + 2;\
|
|
case VALUE_B: return 7 + 2;\
|
|
case VALUE_C: return 7 + 2;\
|
|
}\
|
|
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, "\"\"");\
|
|
}\
|
|
enum enum_A enum_A_from_json(const char* json) {\
|
|
if(strncmp("\"VALUE_A\"", json, 8) == 0) {\
|
|
return VALUE_A;\
|
|
} else if(strncmp("\"VALUE_B\"", json, 8) == 0) {\
|
|
return VALUE_B;\
|
|
} else if(strncmp("\"VALUE_C\"", json, 8) == 0) {\
|
|
return VALUE_C;\
|
|
} else { return VALUE_A; }\
|
|
}\
|
|
size_t struct_A_json_length(struct struct_A* src) {\
|
|
size_t json_capacity = 0;\
|
|
/* 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) + 2;\
|
|
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;\
|
|
}\
|
|
struct struct_A struct_A_from_json(const char* json) {\
|
|
struct struct_A dest;\
|
|
memset(&dest, 0x0, sizeof(struct struct_A)); const char* reader = json;\
|
|
char buffer[48];\
|
|
char* writer = buffer;\
|
|
buffer[47] = '\0';\
|
|
int counter = 0;counter = counter;\
|
|
do {\
|
|
while(*reader != '"') { reader++; }\
|
|
reader++;\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != '"');\
|
|
*writer = '\0';\
|
|
do {\
|
|
reader++;\
|
|
} while(*reader != ':');\
|
|
if(strcmp(buffer, "b") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
dest.b = atoi(buffer);\
|
|
} else if(strcmp(buffer, "a") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
dest.a = atof(buffer);\
|
|
} else if(strcmp(buffer, "u") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
dest.u = atoi(buffer);\
|
|
} else if(strcmp(buffer, "dyn_str") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
dest.dyn_str = malloc(strlen(buffer)-1);\
|
|
dest.dyn_str[0] = '\0';\
|
|
strncpy(dest.dyn_str, buffer+1, strlen(buffer)-2);\
|
|
} else {}\
|
|
} while(*reader != '}');\
|
|
return dest;\
|
|
}\
|
|
size_t struct_B_json_length(struct struct_B* src) {\
|
|
size_t json_capacity = 0;\
|
|
/* 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_static */\
|
|
json_capacity += 14 + strlen(src->str_static) + 2;\
|
|
/* 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_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;\
|
|
}\
|
|
struct struct_B struct_B_from_json(const char* json) {\
|
|
struct struct_B dest;\
|
|
memset(&dest, 0x0, sizeof(struct struct_B)); const char* reader = json;\
|
|
char buffer[48];\
|
|
char* writer = buffer;\
|
|
buffer[47] = '\0';\
|
|
int counter = 0;counter = counter;\
|
|
do {\
|
|
while(*reader != '"') { reader++; }\
|
|
reader++;\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != '"');\
|
|
*writer = '\0';\
|
|
do {\
|
|
reader++;\
|
|
} while(*reader != ':');\
|
|
if(strcmp(buffer, "f") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
dest.f = atof(buffer);\
|
|
} else if(strcmp(buffer, "i") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
dest.i = atoi(buffer);\
|
|
} else if(strcmp(buffer, "str_static") == 0) {\
|
|
while(*reader == ' ' || *reader == ':') { reader++; }\
|
|
writer = buffer;\
|
|
do {\
|
|
*writer = *reader;\
|
|
writer++; reader++;\
|
|
} while(*reader != ',' && *reader != '}');\
|
|
*writer = '\0';\
|
|
strncpy(dest.str_static, buffer+1, strlen(buffer)-2);\
|
|
} else if(strcmp(buffer, "other_struct") == 0) {\
|
|
while(*reader != '{' && *reader != '"') { reader++; }\
|
|
dest.other_struct = struct_A_from_json(reader);\
|
|
counter = 0;\
|
|
do {\
|
|
if(*reader == '}') counter--;\
|
|
else if(*reader == '{') counter++;\
|
|
reader++;\
|
|
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
|
if(*reader == '\0') {\
|
|
reader--;\
|
|
} else {\
|
|
reader++;\
|
|
}\
|
|
} else if(strcmp(buffer, "other_struct_typedef") == 0) {\
|
|
while(*reader != '{' && *reader != '"') { reader++; }\
|
|
dest.other_struct_typedef = struct_A_from_json(reader);\
|
|
counter = 0;\
|
|
do {\
|
|
if(*reader == '}') counter--;\
|
|
else if(*reader == '{') counter++;\
|
|
reader++;\
|
|
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
|
if(*reader == '\0') {\
|
|
reader--;\
|
|
} else {\
|
|
reader++;\
|
|
}\
|
|
} else if(strcmp(buffer, "other_enum") == 0) {\
|
|
while(*reader != '{' && *reader != '"') { reader++; }\
|
|
dest.other_enum = enum_A_from_json(reader);\
|
|
counter = 0;\
|
|
do {\
|
|
if(*reader == '}') counter--;\
|
|
else if(*reader == '{') counter++;\
|
|
reader++;\
|
|
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
|
if(*reader == '\0') {\
|
|
reader--;\
|
|
} else {\
|
|
reader++;\
|
|
}\
|
|
} else if(strcmp(buffer, "other_enum_typedef") == 0) {\
|
|
while(*reader != '{' && *reader != '"') { reader++; }\
|
|
dest.other_enum_typedef = enum_A_from_json(reader);\
|
|
counter = 0;\
|
|
do {\
|
|
if(*reader == '}') counter--;\
|
|
else if(*reader == '{') counter++;\
|
|
reader++;\
|
|
} while(counter > 0 || (counter == 0 && *reader != ','));\
|
|
if(*reader == '\0') {\
|
|
reader--;\
|
|
} else {\
|
|
reader++;\
|
|
}\
|
|
} else {}\
|
|
} while(*reader != '}');\
|
|
return dest;\
|
|
}\
|
|
|