summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/eval.c15
-rw-r--r--src/search.c19
-rw-r--r--src/search.h8
-rw-r--r--src/types.c51
-rw-r--r--src/types.h5
5 files changed, 79 insertions, 19 deletions
diff --git a/src/eval.c b/src/eval.c
index a26cc21..8bc886a 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -5,17 +5,12 @@ recipe *
eval(recipe * r)
{
if (!r) return NULL;
- recipe * r1 = new_recipe();
- recipe * r2 = new_recipe();
- /* attempt to merge items (adding qtys) */
- merge_items(r1, r);
- distinct_sum_items(r2, r1);
- free_recipe(r1);
+ recipe * _r = new_recipe();
- copy_metadata(r2, r);
- /* /\* Resolve step type, variables, duration and step output (if any) *\/ */
- /* finalize_steps(eve, r); */
+ merge_items(_r, r);
+ merge_steps(_r, r);
+ copy_metadata(_r, r);
- return r2;
+ return _r;
}
diff --git a/src/search.c b/src/search.c
new file mode 100644
index 0000000..cf2eab9
--- /dev/null
+++ b/src/search.c
@@ -0,0 +1,19 @@
+#include "util.h"
+#include "search.h"
+
+/**
+ * Query recipe `r` for input `s` and return 1 if found 0 otherwise
+ */
+int
+query_for_items(const recipe * r, const char * s, int strict)
+{
+ for (int i = 0; i < r->in; i++)
+ if (strict) {
+ if (!strcmp(r->i[i]->name, s))
+ return 1;
+ } else {
+ if (strstr(r->i[i]->name, s) != NULL)
+ return 1;
+ }
+ return 0;
+}
diff --git a/src/search.h b/src/search.h
new file mode 100644
index 0000000..1295cd4
--- /dev/null
+++ b/src/search.h
@@ -0,0 +1,8 @@
+#ifndef SEARCH_H
+#define SEARCH_H
+
+#include "types.h"
+
+int query_for_items(const recipe * r, const char * s, int strict);
+
+#endif
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);
+}
diff --git a/src/types.h b/src/types.h
index 0d870cf..2c58537 100644
--- a/src/types.h
+++ b/src/types.h
@@ -65,9 +65,8 @@ void copy_items(recipe * dst, recipe * src);
void merge_items(recipe * dst, recipe * src);
/**
- * Sum all top level item qtys in `src` and add them to `dst`
- * making sure each item name exists only once.
+ * Merge all steps from `src` and add them to `dst`
*/
-void distinct_sum_items(recipe * dst, recipe * src);
+void merge_steps(recipe * dst, recipe * src);
#endif /* __TYPES_H */