From dbd6366285b23483567f2c1dc814fc9f371c4c64 Mon Sep 17 00:00:00 2001 From: gramanas Date: Mon, 20 Dec 2021 22:52:48 +0200 Subject: merge steps --- src/types.c | 51 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 6 deletions(-) (limited to 'src/types.c') diff --git a/src/types.c b/src/types.c index d9253af..3e720b8 100644 --- a/src/types.c +++ b/src/types.c @@ -237,6 +237,17 @@ copy_items(recipe * dst, recipe * src) } } +void +copy_steps(recipe * dst, recipe * src) +{ + if (!dst || !src) return; + for (int i = 0; i < src->sn; i++) { + new_step(dst); + dst->s[dst->sn - 1]->inst = strdup(src->s[i]->inst); + dst->s[dst->sn - 1]->type = src->s[i]->type; + } +} + static void join_subrecipe_items(recipe * dst, recipe * src) { @@ -258,13 +269,27 @@ join_subrecipe_items(recipe * dst, recipe * src) } } +static void +join_subrecipe_steps(recipe * dst, recipe * src) +{ + if (!src || !dst) return; + for (int i = 0; i < src->rn; i++) { + join_subrecipe_steps(dst, src->r[i]); + for (int j = 0; j < src->r[i]->sn; j++) { + new_step(dst); + dst->s[dst->sn - 1]->inst = strdup(src->r[i]->s[j]->inst); + dst->s[dst->sn - 1]->type = src->r[i]->s[j]->type; + } + } +} + void -merge_items(recipe * dst, recipe * src) +merge_steps(recipe * dst, recipe * src) { - /* Join all items in src's subrecipes to dst */ - join_subrecipe_items(dst, src); - /* Copy src items as well to dst */ - copy_items(dst, src); + /* Join all steps in src's subrecipes to dst */ + join_subrecipe_steps(dst, src); + /* Copy src steps as well to dst */ + copy_steps(dst, src); } static int @@ -276,7 +301,11 @@ item_exists(const char * name, const recipe * r) return -1; } -void +/** + * Sum all top level item qtys in `src` and add them to `dst` + * making sure each item name exists only once. + */ +static void distinct_sum_items(recipe * dst, recipe * src) { if (!dst || !src) return; @@ -297,3 +326,13 @@ distinct_sum_items(recipe * dst, recipe * src) } } } + +void +merge_items(recipe * dst, recipe * src) +{ + /* Join all items in src's subrecipes to dst */ + join_subrecipe_items(dst, src); + /* Copy src items as well to dst */ + copy_items(dst, src); + distinct_sum_items(dst, dst); +} -- cgit v1.2.3