diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-10 15:26:32 -0400 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-10 15:26:32 -0400 |
commit | de52acd37ea5634712ed78c624f77566e3ac634e (patch) | |
tree | e09c9fd6980a9d81dbfe50b2e7aed86566c07654 /src/keytab.c | |
parent | 7c8e83f655e41b11e970fd0abad5a96a3fb73e47 (diff) |
Removed need for parser and revised readdb to return struct keytablist *
Diffstat (limited to 'src/keytab.c')
-rw-r--r-- | src/keytab.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/src/keytab.c b/src/keytab.c index b1b12d7..3cba87a 100644 --- a/src/keytab.c +++ b/src/keytab.c @@ -5,6 +5,8 @@ #include "include/mdb.h" +static const struct keytablist empty; + static int hash(char *key); int *getkeys(struct keytablist *list, int id) @@ -35,11 +37,14 @@ struct keytab getkey(struct keytablist *list, int id, char *key) return list[id].tab[idx]; } -void setkey(struct keytablist **list, int *len, int id, char *pair) +int setkey(struct keytablist **list, int *len, int id, char *pair) { if (id >= *len) { - *len += (id - *len) + 1; - *list = realloc(*list, (*len) * sizeof(struct keytablist)); + *list = realloc(*list, (id + 1) * sizeof(struct keytablist)); + for (int i = *len; i <= id; ++i) + (*list)[i] = empty; + *len = id + 1; + (*list)[0].len = *len; } char *tok = strtok(pair, ":"); char *key = calloc(strlen(tok) + 1, sizeof(char)); @@ -48,7 +53,7 @@ void setkey(struct keytablist **list, int *len, int id, char *pair) tok = strtok(NULL, ":"); if (tok == NULL) { fprintf(stderr, "Invalid key-value pair\n"); - return; + return 1; } union value v; int flag; @@ -67,11 +72,10 @@ void setkey(struct keytablist **list, int *len, int id, char *pair) int idx = hash(key); while ((*list)[id].tab[idx].key != NULL && strcmp((*list)[id].tab[idx].key, key) && - idx < TABLEN) - idx++; + idx < TABLEN) idx++; if (idx >= TABLEN) { fprintf(stderr, "No more room in table\n"); - return; + return 2; } if (!(*list)[id].tab[idx].key) (*list)[id].tab[idx].key = key; @@ -79,6 +83,7 @@ void setkey(struct keytablist **list, int *len, int id, char *pair) free(key); (*list)[id].tab[idx].v = v; (*list)[id].tab[idx].flag = flag; + return 0; } void delkey(struct keytablist *list, int id, char *key) |