jdict

command line tool for looking up terms in yomidict dictionaries
git clone anongit@rnpnr.xyz:jdict.git
Log | Files | Refs | Feed | README | LICENSE

Commit: a7c87daaeaef492de6690dacb07fdf348c758b78
Parent: 4b2171a12d0bee786015d73f521714feae06788c
Author: Randy Palamar
Date:   Sat, 22 Oct 2022 09:58:43 -0600

add rudimentary duplicate entry merging

this method is a little hacky and could be improved by completely
removing the second entry after moving its definitions.

Diffstat:
Mjdict.c | 29++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/jdict.c b/jdict.c @@ -49,11 +49,38 @@ free_ents(DictEnt *ents, size_t nents) ents = NULL; } +/* FIXME: this isn't the best, we are modifying the value of const ptrs + * and wasting some memory by not freeing b. + */ +static int +merge_ents(DictEnt *a, DictEnt *b) +{ + size_t i, nlen = a->ndefs + b->ndefs; + + a->defs = xreallocarray(a->defs, nlen, sizeof(char *)); + + for (i = 0; i < b->ndefs; i++) { + a->defs[a->ndefs + i] = b->defs[i]; + b->defs[i] = NULL; + } + a->ndefs = nlen; + + free(b->defs); + b->defs = NULL; + b->ndefs = 0; + + return 1; +} + static int entcmp(const void *va, const void *vb) { + int r; const DictEnt *a = va, *b = vb; - return strcmp(a->term, b->term); + + if (!(r = strcmp(a->term, b->term))) + return merge_ents((DictEnt *)a, (DictEnt *)b); + return r; } /* takes a token of type YOMI_ENTRY and creates a DictEnt */