1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include "eval.h"
#include "util.h"
#include <stdint.h>
extern int
sha1digest(uint8_t *digest, char *hexdigest, const uint8_t *data, size_t databytes);
void
create_hash(recipe * r)
{
char data[FOOD_MAX_ARRAY] = "\0";
for (int i = 0; i < r->in; i++) {
strcat(data, r->i[i]->name);
strcat(data, r->i[i]->qty);
}
if (r->sn) {
for (int i = 0; i < r->sn; i++)
strcat(data, r->s[i]->inst);
}
sha1digest(NULL, r->sha1, (uint8_t *)data, strlen(data));
}
/* TODO: Needs work to actually remove the extra characters only when they
define a variable/time schedule */
char *
inst2txt(char * s)
{
char txt[FOOD_MAX_ARRAY] = "\0";
int l = 0;
for (int i=0; i<strlen(s); i++) {
l = strlen(txt);
switch(s[i]) {
case '$':
case '[':
case ']':
case '{':
case '}':
continue;
default:
txt[l] = s[i];
txt[l+1] = '\0';
}
}
return strdup(txt);
}
/* TODO: Still needs work for when the `[`s are not balanced */
char *
inst2duration(char * s)
{
char duration[FOOD_MAX_ARRAY] = "\0";
int flag = 0;
int l = 0;
for (int i=0; i<strlen(s); i++) {
if (s[i] == ']')
flag = flag - 1;
if (flag > 0) {
duration[l] = s[i];
duration[l+1] = '\0';
l = strlen(duration);
}
if (s[i] == '[')
flag = flag + 1;
}
if (strlen(duration)) {
return strdup(duration);
}
return NULL;
}
int
eval_step(step * s)
{
s->txt = inst2txt(s->inst);
s->duration = inst2duration(s->inst);
// s->result = strdup(s->inst);
return 0;
}
int
resolve_steps(recipe * r)
{
if (!r) return 1;
int rc = 0;
for (int i=0; i < r->sn; i++) {
rc += eval_step(r->s[i]);
}
if (rc > 0) {
return 1;
}
return 0;
}
recipe *
eval(recipe * r)
{
if (!r) return NULL;
recipe * _r = new_recipe();
merge_items(_r, r);
merge_steps(_r, r);
copy_metadata(_r, r);
copy_subrecipes(_r, r, 0 /* shallow copy off */);
create_hash(_r);
resolve_steps(_r);
return _r;
}
|