From 021fe93cf7656b3e3e7eb7fc01a30a4727bc359c Mon Sep 17 00:00:00 2001 From: bpc2003 Date: Mon, 10 Mar 2025 10:33:20 -0400 Subject: Offloaded reallocing list to setkey --- src/include/fileops.h | 1 + src/include/keytab.h | 3 +-- src/keytab.c | 18 ++++++++++++------ src/main.c | 8 ++------ 4 files changed, 16 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/include/fileops.h b/src/include/fileops.h index 2354913..63294ea 100644 --- a/src/include/fileops.h +++ b/src/include/fileops.h @@ -2,5 +2,6 @@ #define FILEOPS_H unsigned char *readdb(char *filename); +// TODO: Implement writedb function #endif diff --git a/src/include/keytab.h b/src/include/keytab.h index 40db10f..7e15643 100644 --- a/src/include/keytab.h +++ b/src/include/keytab.h @@ -21,8 +21,7 @@ struct keytablist { int *getkeys(struct keytablist *list, int id); struct keytab getkey(struct keytablist *list, int id, char *key); -// TODO: rewrite setkey to take len as an argument -void setkey(struct keytablist *list, int id, char *pair); +void setkey(struct keytablist **list, int *len, int id, char *pair); void delkey(struct keytablist *list, int id, char *key); #endif diff --git a/src/keytab.c b/src/keytab.c index 160e50a..6fcf303 100644 --- a/src/keytab.c +++ b/src/keytab.c @@ -35,8 +35,12 @@ struct keytab getkey(struct keytablist *list, int id, char *key) return list[id].tab[idx]; } -void setkey(struct keytablist *list, int id, char *pair) +void setkey(struct keytablist **list, int *len, int id, char *pair) { + if (id >= *len) { + *len += (id - *len) + 1; + *list = realloc(*list, (*len) * sizeof(struct keytablist)); + } char *tok = strtok(pair, ":"); char *key = calloc(strlen(tok) + 1, sizeof(char)); strcpy(key, tok); @@ -61,18 +65,20 @@ void setkey(struct keytablist *list, int id, char *pair) } int idx = hash(key); - while (list[id].tab[idx].key != NULL && strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + while ((*list)[id].tab[idx].key != NULL && + strcmp((*list)[id].tab[idx].key, key) && + idx < TABLEN) idx++; if (idx >= TABLEN) { fprintf(stderr, "No more room in table\n"); return; } - if (!list[id].tab[idx].key) - list[id].tab[idx].key = key; + if (!(*list)[id].tab[idx].key) + (*list)[id].tab[idx].key = key; else free(key); - list[id].tab[idx].v = v; - list[id].tab[idx].flag = flag; + (*list)[id].tab[idx].v = v; + (*list)[id].tab[idx].flag = flag; } void delkey(struct keytablist *list, int id, char *key) diff --git a/src/main.c b/src/main.c index e8a3a08..9c71bc7 100644 --- a/src/main.c +++ b/src/main.c @@ -14,7 +14,7 @@ int main(int argc, char **argv) if (argc != 2) exit(1); char *filename = argv[1]; - + uint8_t *buf = readdb(filename); if (buf == NULL) exit(0); @@ -24,10 +24,6 @@ int main(int argc, char **argv) for (int i = 0, j = 0; i < blen; ++i) { switch (bytes[i].type) { case BEGIN: - if (j >= len) { - len *= 2; - list = realloc(list, len * sizeof(struct keytablist)); - } if (open == 1) { fprintf(stderr, "missing close!\n"); free(bytes); @@ -40,7 +36,7 @@ int main(int argc, char **argv) j++; break; case PAIR: - setkey(list, j, bytes[i].value); + setkey(&list, &len, j, bytes[i].value); free(bytes[i].value); break; case ERROR: -- cgit v1.2.3