summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorAnastasios Grammenos <anastasios.grammenos@noris.gr>2022-10-23 21:05:57 +0300
committerAnastasios Grammenos <anastasios.grammenos@noris.gr>2022-10-23 21:05:57 +0300
commit51d16373fbaa027e91f230a7423ebb1b2d339042 (patch)
tree9f088ae6544eb646c9c90e2323beeab9663e4360 /src
parent7c5af1b2768247984a08569b35e47ff927e70389 (diff)
downloadfoodtools-51d16373fbaa027e91f230a7423ebb1b2d339042.tar.gz
foodtools-51d16373fbaa027e91f230a7423ebb1b2d339042.tar.bz2
foodtools-51d16373fbaa027e91f230a7423ebb1b2d339042.zip
More
Diffstat (limited to 'src')
-rw-r--r--src/eval.c57
-rw-r--r--src/food.c2
-rw-r--r--src/types.c7
-rw-r--r--src/types.h1
4 files changed, 62 insertions, 5 deletions
diff --git a/src/eval.c b/src/eval.c
index 98ad3bf..aa9a236 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -21,11 +21,64 @@ create_hash(recipe * r)
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->duration = strdup(s->inst);
- s->result = strdup(s->inst);
+ s->txt = inst2txt(s->inst);
+ s->duration = inst2duration(s->inst);
+ // s->result = strdup(s->inst);
return 0;
}
diff --git a/src/food.c b/src/food.c
index 95fde55..5c9730b 100644
--- a/src/food.c
+++ b/src/food.c
@@ -80,7 +80,7 @@ main(int argc, char * argv[])
{"to-html", no_argument, 0, 'w', "Format recipe to html", 0, 0 },
{"to-rcp", no_argument, 0, 'r', "Format recipe to rcp", 0, 0 },
{"list-ingredients", no_argument, 0, 'L', "List ingredients for matched recipes", 0, 0 },
- {"list", optional_argument, 0, 'l', "List matched recipes with optional FORMAT", "[FORMAT]", 0 },
+ {"list", optional_argument, 0, 'l', "List matched recipes with optional FORMAT {t,p,h,s}", "[FORMAT]", 0 },
{"search", required_argument, 0, 's', "Return recipes matching QUERY", "QUERY", "Filters" },
{"title", required_argument, 0, 't', "Return recipes matching TITLE", "TITLE", 0 },
{"strict", required_argument, 0, 'S', "Return recipes matching QUERY, exactly", "QUERY", 0 },
diff --git a/src/types.c b/src/types.c
index 68c402c..8debe70 100644
--- a/src/types.c
+++ b/src/types.c
@@ -74,6 +74,7 @@ new_step(recipe * r)
if (!r->s[r->sn])
die("Couldn't allocate memory for step");
r->s[r->sn]->inst = NULL;
+ r->s[r->sn]->txt = NULL;
r->s[r->sn]->duration = NULL;
r->s[r->sn]->result = NULL;
r->s[r->sn]->type = 0;
@@ -87,6 +88,8 @@ free_step(step * s)
return;
if (s->inst)
free(s->inst);
+ if (s->txt)
+ free(s->txt);
if (s->duration)
free(s->duration);
if (s->result)
@@ -220,8 +223,8 @@ tojson(recipe * r)
printf(",\"steps\":[");
int i = 0;
for (; i < r->sn - 1; i++)
- printf("{\"inst\":\"%s\",\"duration\":\"%s\",\"result\":\"%s\",\"type\":\"%s\"},", r->s[i]->inst, r->s[i]->duration, r->s[i]->result, r->s[i]->type == 0 ? "prep" : (r->s[i]->type == 1 ? "cook" : "serve") );
- printf("{\"inst\":\"%s\",\"duration\":\"%s\",\"result\":\"%s\",\"type\":\"%s\"}]", r->s[i]->inst, r->s[i]->duration, r->s[i]->result, r->s[i]->type == 0 ? "prep" : (r->s[i]->type == 1 ? "cook" : "serve") );
+ printf("{\"inst\":\"%s\",\"txt\":\"%s\",\"duration\":\"%s\",\"result\":\"%s\",\"type\":\"%s\"},", r->s[i]->inst, r->s[i]->txt, r->s[i]->duration, r->s[i]->result, r->s[i]->type == 0 ? "prep" : (r->s[i]->type == 1 ? "cook" : "serve") );
+ printf("{\"inst\":\"%s\",\"txt\":\"%s\",\"duration\":\"%s\",\"result\":\"%s\",\"type\":\"%s\"}]", r->s[i]->inst, r->s[i]->txt, r->s[i]->duration, r->s[i]->result, r->s[i]->type == 0 ? "prep" : (r->s[i]->type == 1 ? "cook" : "serve") );
}
printf("}");
}
diff --git a/src/types.h b/src/types.h
index d589c79..057f889 100644
--- a/src/types.h
+++ b/src/types.h
@@ -14,6 +14,7 @@ enum step_type {
typedef struct step_t {
char *inst;
+ char *txt;
char *duration;
char *result;
enum step_type type;