diff options
author | Anastasios Grammenos <anastasios.grammenos@noris.gr> | 2022-10-23 21:05:57 +0300 |
---|---|---|
committer | Anastasios Grammenos <anastasios.grammenos@noris.gr> | 2022-10-23 21:05:57 +0300 |
commit | 51d16373fbaa027e91f230a7423ebb1b2d339042 (patch) | |
tree | 9f088ae6544eb646c9c90e2323beeab9663e4360 | |
parent | 7c5af1b2768247984a08569b35e47ff927e70389 (diff) | |
download | foodtools-51d16373fbaa027e91f230a7423ebb1b2d339042.tar.gz foodtools-51d16373fbaa027e91f230a7423ebb1b2d339042.tar.bz2 foodtools-51d16373fbaa027e91f230a7423ebb1b2d339042.zip |
More
-rw-r--r-- | lib/minced-meat.rcp | 2 | ||||
-rw-r--r-- | src/eval.c | 57 | ||||
-rw-r--r-- | src/food.c | 2 | ||||
-rw-r--r-- | src/types.c | 7 | ||||
-rw-r--r-- | src/types.h | 1 |
5 files changed, 63 insertions, 6 deletions
diff --git a/lib/minced-meat.rcp b/lib/minced-meat.rcp index 9feb48b..c9fc5e4 100644 --- a/lib/minced-meat.rcp +++ b/lib/minced-meat.rcp @@ -29,4 +29,4 @@ olive oil = 3 tbsp > stir [until the meat is brown and starts to stick on the pan] > deglaze with ${white wine} and stir [until most wine has evaporated] > add the ${tomato sauce} and stir, if too thick add some water -> [cook for 30 minutesd] +> [cook for 30 minutes] @@ -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; } @@ -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; |