summaryrefslogtreecommitdiffstats
path: root/src/types.c
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2021-12-20 22:52:48 +0200
committergramanas <anastasis.gramm2@gmail.com>2021-12-20 22:52:48 +0200
commitdbd6366285b23483567f2c1dc814fc9f371c4c64 (patch)
tree6df025dc5e668ddc618fddde289c4c2b1025e8dd /src/types.c
parentbf4733f991bb9e643ebc697d6f9f92b3bb6ad69c (diff)
downloadfoodtools-dbd6366285b23483567f2c1dc814fc9f371c4c64.tar.gz
foodtools-dbd6366285b23483567f2c1dc814fc9f371c4c64.tar.bz2
foodtools-dbd6366285b23483567f2c1dc814fc9f371c4c64.zip
merge steps
Diffstat (limited to 'src/types.c')
-rw-r--r--src/types.c51
1 files changed, 45 insertions, 6 deletions
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);
+}