From 675aadbe0da95e4023035f1ac34a784d8e217778 Mon Sep 17 00:00:00 2001 From: Sara Date: Wed, 13 Sep 2023 10:39:33 +0200 Subject: [PATCH] kwil now generates a length get function and _to_json will no longer allocate memory --- src/kwil_generate.c | 48 +++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/kwil_generate.c b/src/kwil_generate.c index 55a3349..f0ed50e 100644 --- a/src/kwil_generate.c +++ b/src/kwil_generate.c @@ -43,13 +43,16 @@ int kwil_header_generate(struct kwil_header_t* self) { return 1; } - fprintf(file, "#include \n" - "#define KWIL_GEN_IMPL(...)\\\n"); + fprintf(file, "#include \n"); + + fprintf(file, "#define KWIL_GEN_IMPL(...)\\\n"); for(int type_index = 0; type_index < self->types_len; ++type_index) { struct kwil_type_t* type = self->types + type_index; + printf("Generating functions for %s\n", type->type_name); switch(type->type_tag) { case KWIL_TYPE_STRUCT: + kwil_struct_generate_json_length(&type->struct_type, file, type->type_name); kwil_struct_generate_to_json(&type->struct_type, file, type->type_name); break; case KWIL_TYPE_ENUM: @@ -67,6 +70,7 @@ int kwil_header_generate(struct kwil_header_t* self) { static void kwil_field_generate_to_string(struct kwil_field_t* self, FILE* file) { + char custom_type_prefix[48]; switch(self->type_tag) { case KWIL_FIELD_CHAR: if(self->array_length > 0 || self->array_dynamic) { @@ -85,12 +89,17 @@ void kwil_field_generate_to_string(struct kwil_field_t* self, FILE* file) { case KWIL_FIELD_UNSIGNED: fprintf(file, " json_len += sprintf(json + json_len, \"%%u\", src->%s);\\\n", self->name_str); break; - default: break; + case KWIL_FIELD_UNKNOWN: + case KWIL_FIELD_CUSTOM: + kwil_typename_to_prefix(self->type_str, custom_type_prefix, 47); + fprintf(file, " json_len += %s_to_json(&src->%s, json + json_len);\\\n", custom_type_prefix, self->name_str); + break; } } static void kwil_field_get_serialized_length(struct kwil_field_t* self, FILE* file) { + char custom_type_prefix[48]; size_t len = strlen(self->name_str) + 4; // "name": , fprintf(file, " /* length of %s */\\\n" @@ -116,34 +125,43 @@ void kwil_field_get_serialized_length(struct kwil_field_t* self, FILE* file) { break; case KWIL_FIELD_CUSTOM: case KWIL_FIELD_UNKNOWN: - fprintf(file, ";\\\n"); + kwil_typename_to_prefix(self->type_str, custom_type_prefix, 47); + fprintf(file, " + %s_json_length(&src->%s);\\\n", custom_type_prefix, self->name_str); break; } } +int kwil_struct_generate_json_length(struct kwil_struct_t* self, FILE* file, const char* type_name) { + char prefix[48]; + kwil_typename_to_prefix(type_name, prefix, 47); + + fprintf(file, "size_t %s_json_length(struct %s* src) {\\\n" + " size_t json_capacity = 2;\\\n",// allocate at least two for "{}\0" + prefix, type_name); + for(int field_index = 0; field_index < self->fields_len; ++field_index) { + kwil_field_get_serialized_length(self->fields + field_index, file); + } + fprintf(file, " return json_capacity;\\\n" + "}\\\n"); + return 0; +} + int kwil_struct_generate_to_json(struct kwil_struct_t* self, FILE* file, const char* type_name) { char prefix[48]; kwil_typename_to_prefix(type_name, prefix, 47); - fprintf(file, "int %s_to_json(struct %s* src, char** out_json) {\\\n" - " int json_capacity = 2; \\\n" // allocate at least two for "{}\0" + fprintf(file, "int %s_to_json(struct %s* src, char* json) {\\\n" , prefix, type_name); - for(int field_index = 0; field_index < self->fields_len; ++field_index) { - kwil_field_get_serialized_length(self->fields + field_index, file); - } - - fprintf(file, " char* json = malloc(json_capacity);\\\n" - " int json_len = 1;\\\n" - " strcpy(json, \"{\");\\\n" - " *out_json = json;\\\n"); + fprintf(file, " int json_len = 1;\\\n" + " strcpy(json, \"{\");\\\n"); for(int field_index = 0; field_index < self->fields_len; ++field_index) { kwil_field_generate_to_json(self->fields + field_index, file); } fprintf(file, " strcpy(json + json_len - 1, \"}\");\\\n" - " return json_capacity;\\\n" + " return json_len;\\\n" "}\\\n"); fflush(file);