diff options
Diffstat (limited to 'types.c')
-rw-r--r-- | types.c | 50 |
1 files changed, 46 insertions, 4 deletions
@@ -11,10 +11,13 @@ 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; @@ -22,6 +25,17 @@ new_recipe() } 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) { r->i = (item **)realloc(r->i, (r->in + 1) * sizeof(item *)); @@ -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); + } } } |