From 7b7240d50dd5e418bc17beff48aa5681b398b980 Mon Sep 17 00:00:00 2001 From: gramanas Date: Fri, 14 Oct 2022 22:11:50 +0300 Subject: More changes --- src/types.c | 114 ++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 85 insertions(+), 29 deletions(-) (limited to 'src/types.c') diff --git a/src/types.c b/src/types.c index 5ce0203..9314f8d 100644 --- a/src/types.c +++ b/src/types.c @@ -132,15 +132,46 @@ void pprint_items(recipe * r) { printf("Ingredients for %s:\n", r->title); - for (int i = 0; i < r->in; i++) - printf("%s: %s\n", r->i[i]->name, r->i[i]->qty); + for (int i = 0; i < r->in; i++) { + if (strlen(r->i[i]->qty)) { + printf("%s: %s\n", r->i[i]->name, r->i[i]->qty); + } + else { + printf("%s\n", r->i[i]->name); + } + } printf("\n"); } +char * last_n(const char * s, size_t n) +{ + size_t length = strlen( s ); + return ( char * )( length < n ? s : s + length - n ); +} + void -listing(recipe * r) +listing(recipe * r, const char * fmt) { - printf("%.*s %d:%d\t%d\t%s\n", 8, (strcmp(r->sha1, "") ? r->sha1 : "neval"), r->in, r->sn, r->rn, r->title); + if (!strcmp(fmt, "title") || !strcmp(fmt, "t")) { + printf("%s\n", r->title); + } + else if (!strcmp(fmt, "path") || !strcmp(fmt, "p")) { + printf("%s/%s\n", r->path, r->filename); + } + else if (!strcmp(fmt, "hash") || !strcmp(fmt, "h")) { + printf("%s\n", strcmp(r->sha1, "") ? r->sha1 : "neval"); + } + else if (!strcmp(fmt, "shorthash") || !strcmp(fmt, "s")) { + printf("%.*s\n", 8, strcmp(r->sha1, "") ? r->sha1 : "neval"); + } + else { + printf("%.*s %10.10s %d:%d\t%d\t%s\n", + 8, (strcmp(r->sha1, "") ? r->sha1 : "neval"), + basename(r->path), + r->in, r->sn, + r->rn, + r->title); + } } void @@ -244,8 +275,14 @@ torcp(recipe * r) for (int i = 0; i < r->rn; i++) { printf("%s!%s/%s\n", (r->sha1[0] != '\0') ? "# ": "", r->r[i]->path, r->r[i]->filename); } - for (int i = 0; i < r->in; i++) - printf("%s = %s\n", r->i[i]->name, r->i[i]->qty); + for (int i = 0; i < r->in; i++) { + if (strlen(r->i[i]->qty)) { + printf("%s = %s\n", r->i[i]->name, r->i[i]->qty); + } + else { + printf("%s\n", r->i[i]->name); + } + } if (r->sn) { printf("\n---\n\n"); for (int i = 0; i < r->sn; i++) { @@ -261,20 +298,6 @@ torcp(recipe * r) } } -void -copy_subrecipes(recipe * dst, recipe * src) -{ - if (!dst || !src) return; - for (int i = 0; i < src->rn; i++) { - recipe * r = new_recipe(); - r->title = strdup(src->r[i]->title); - r->path = strdup(src->r[i]->path); - r->filename = strdup(src->r[i]->filename); - r->n = src->r[i]->n; - new_subrecipe(dst, r); - } -} - void copy_metadata(recipe * dst, recipe * src) { @@ -309,6 +332,24 @@ copy_steps(recipe * dst, recipe * src) } } +void +copy_subrecipes(recipe * dst, recipe * src, int shallow_copy) +{ + if (!dst || !src) return; + for (int i = 0; i < src->rn; i++) { + recipe * r = new_recipe(); + r->title = strdup(src->r[i]->title); + r->path = strdup(src->r[i]->path); + r->filename = strdup(src->r[i]->filename); + r->n = src->r[i]->n; + if (!shallow_copy) { + copy_items(r, src->r[i]); + copy_steps(r, src->r[i]); + } + new_subrecipe(dst, r); + } +} + static void join_subrecipe_items(recipe * dst, recipe * src) { @@ -319,9 +360,13 @@ join_subrecipe_items(recipe * dst, recipe * src) new_item(dst); dst->i[dst->in - 1]->name = strdup(src->r[i]->i[j]->name); if (src->r[i]->n > 1) { - char tmp[2048] = ""; - sprintf(tmp, "%d X ( %s )", src->r[i]->n, src->r[i]->i[j]->qty); - dst->i[dst->in - 1]->qty = strdup(tmp); + if (strcmp(src->r[i]->i[j]->qty, "")) { + char tmp[2048] = ""; + sprintf(tmp, "%d X ( %s )", src->r[i]->n, src->r[i]->i[j]->qty); + dst->i[dst->in - 1]->qty = strdup(tmp); + } else { + dst->i[dst->in - 1]->qty = strdup(""); + } } else { dst->i[dst->in - 1]->qty = strdup(src->r[i]->i[j]->qty); @@ -373,12 +418,23 @@ distinct_sum_items(recipe * dst, recipe * src) for (int i = 0; i < src->in; i++) { int n = item_exists(src->i[i]->name, dst); if (n != -1) { - char tmp[FODD_MAX_ARRAY] = ""; - strcat(tmp, dst->i[n]->qty); - strcat(tmp, " + "); - strcat(tmp, src->i[i]->qty); - free(dst->i[n]->qty); - dst->i[n]->qty = strdup(tmp); + if (!strcmp(dst->i[n]->qty, "") + && !strcmp(src->i[i]->qty, "")) { + // noop + } else if (strlen(dst->i[n]->qty) + strlen(src->i[i]->qty) != 0) { + char tmp[FOOD_MAX_ARRAY] = ""; + strcat(tmp, dst->i[n]->qty); + strcat(tmp, src->i[i]->qty); + free(dst->i[n]->qty); + dst->i[n]->qty = strdup(tmp); + } else { + char tmp[FOOD_MAX_ARRAY] = ""; + strcat(tmp, dst->i[n]->qty); + strcat(tmp, " + "); + strcat(tmp, src->i[i]->qty); + free(dst->i[n]->qty); + dst->i[n]->qty = strdup(tmp); + } } else { new_item(dst); -- cgit v1.2.3