From 66a86d1b20a1e97b2d008e73db7557887fc44edf Mon Sep 17 00:00:00 2001 From: gramanas Date: Sat, 6 Nov 2021 13:01:47 +0200 Subject: Progress --- types.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'types.c') diff --git a/types.c b/types.c index 4eb3a1b..091ff8d 100644 --- a/types.c +++ b/types.c @@ -11,16 +11,30 @@ new_recipe() if (!r) die("Couldn't allocate memory for recipe"); + r->n = 1; r->i = NULL; r->in = 0; r->s = NULL; r->sn = 0; + r->r = NULL; + r->rn = 0; r->filename = NULL; r->path = NULL; r->title = NULL; return r; } +void +new_subrecipe(recipe * r, recipe * src) +{ + r->r = (recipe **)realloc(r->r, (r->rn + 1) * sizeof(recipe *)); + if (!r->r) + die("Couldn't allocate memory for subricepie"); + + r->r[r->rn] = src; + r->rn++; +} + void new_item(recipe * r) { @@ -97,6 +111,12 @@ free_recipe(recipe * r) free(r->s); } + if (r->r) { + for (int i = 0; i < r->rn; i++) + free_recipe(r->r[i]); + free(r->r); + } + if (r->filename) free(r->filename); if (r->path) @@ -128,7 +148,18 @@ tojson(recipe * r) { printf("{\"filename\":\"%s\",", r->filename); printf("\"dirname\":\"%s\",", r->path); - printf("\"title\":\"%s\"", r->title); + printf("\"title\":\"%s\",", r->title); + printf("\"n\":\"%d\"", r->n); + if (r->rn) { + printf(",\"subrecipes\":["); + int i = 0; + for (; i < r->rn - 1; i++) { + tojson(r->r[i]); + printf(","); + } + tojson(r->r[i]); + printf("]"); + } if (r->in) { printf(",\"ingredients\":{"); int i = 0; @@ -143,7 +174,7 @@ tojson(recipe * r) printf("\"%s\",", r->s[i]->inst); printf("\"%s\"]", r->s[i]->inst); } - printf("}\n"); + printf("}"); } void @@ -157,12 +188,23 @@ torcp(recipe * r) { printf("# %s/%s\n\n", r->path, r->filename); printf("@%s\n\n", r->title); + for (int i = 0; i < r->rn; i++) { + printf("!%s/%s\n", r->r[i]->path, r->r[i]->filename); + } for (int i = 0; i < r->in; i++) printf("%s = %s\n", r->i[i]->name, r->i[i]->qty); if (r->sn) { printf("\n---\n\n"); - for (int i = 0; i < r->sn; i++) - printf("%s %s\n", r->s[i]->type == PREP ? "-" : ">", r->s[i]->inst); + for (int i = 0; i < r->sn; i++) { + char c; + if (r->s[i]->type == PREP) + c = '-'; + else if (r->s[i]->type == COOK) + c = '>'; + else + c = '+'; + printf("%c %s\n", c, r->s[i]->inst); + } } } -- cgit v1.2.3