From a0f30c090575894ad9d15881c70a830019b71db7 Mon Sep 17 00:00:00 2001 From: bpc2003 Date: Sat, 15 Mar 2025 09:52:04 -0400 Subject: Did some refactoring --- src/lib/file.c | 22 ++++++++----------- src/lib/keytab.c | 67 +++++++++++++++++++++++--------------------------------- src/lib/mdb.h | 34 ++++++++++++++-------------- 3 files changed, 53 insertions(+), 70 deletions(-) (limited to 'src/lib') diff --git a/src/lib/file.c b/src/lib/file.c index 34086e0..693d0e5 100644 --- a/src/lib/file.c +++ b/src/lib/file.c @@ -9,10 +9,10 @@ static char *getpair(int *c, FILE *fp); // readdb - reads the given file into a key table list // if fp returns NULL it will return the empty list // if readdb fails it will return NULL -struct keytablist *readdb(char *filename) +tablist_t *readdb(char *filename) { int len = 2; - struct keytablist *list = calloc(len, sizeof(struct keytablist)); + tablist_t *list = calloc(len, sizeof(tablist_t)); list[0].len = len; FILE *fp = fopen(filename, "rb"); if (fp == NULL) @@ -24,18 +24,14 @@ struct keytablist *readdb(char *filename) switch (c) { case 250: p = getpair(&c, fp); - if (p == NULL) { - fprintf(stderr, "missing pair closing byte!\n"); + if (p == NULL) return NULL; - } setkey(&list, i, p); free(p); break; case 251: - if (open == 1) { - fprintf(stderr, "missing object closing byte!\n"); + if (open == 1) return NULL; - } open = 1; break; case 254: @@ -43,7 +39,6 @@ struct keytablist *readdb(char *filename) i++; break; default: - fprintf(stderr, "Unknown byte: %d\n", c); return NULL; } } @@ -52,7 +47,7 @@ struct keytablist *readdb(char *filename) } // writedb - writes a keytablist to a given file -void writedb(char *filename, struct keytablist *list) +void writedb(char *filename, tablist_t *list) { FILE *fp = fopen(filename, "wb"); for (int i = 0; i < list[0].len; ++i) { @@ -62,13 +57,14 @@ void writedb(char *filename, struct keytablist *list) fprintf(fp, "\xfa%s:", list[i].tab[indexes[j]].key); switch (list[i].tab[indexes[j]].flag) { case 1: - fprintf(fp, "%.2lf\xfc", list[i].tab[indexes[j]].v.num); + fprintf(fp, "%.2lf\xfc", list[i].tab[indexes[j]].value.num); break; case 2: - fprintf(fp, "%s\xfc", list[i].tab[indexes[j]].v.b == 1 ? "true" : "false"); + fprintf(fp, "%s\xfc", list[i].tab[indexes[j]].value.boolean ? + "true" : "false"); break; case 3: - fprintf(fp, "%s\xfc", list[i].tab[indexes[j]].v.str); + fprintf(fp, "%s\xfc", list[i].tab[indexes[j]].value.str); break; } } diff --git a/src/lib/keytab.c b/src/lib/keytab.c index ed61264..35d8b59 100644 --- a/src/lib/keytab.c +++ b/src/lib/keytab.c @@ -8,7 +8,7 @@ static int hash(char *key); // getkeys - gets every single key in a key table -int *getkeys(struct keytablist *list, int id) +int *getkeys(tablist_t *list, int id) { int len = 2; int *indexes = calloc(len, sizeof(int)); @@ -25,93 +25,82 @@ int *getkeys(struct keytablist *list, int id) // getkey - gets a single key from a keytable // if it can't find the key it will return an empty table index -struct keytab getkey(struct keytablist *list, int id, char *key) +tabidx_t getkey(tablist_t *list, int id, char *key) { int idx = hash(key); if (list[id].tab[idx].key == NULL) - return (struct keytab) { .key = NULL, .flag = 0, .v = { 0 } }; + return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) idx++; if (idx >= TABLEN) - return (struct keytab) { .key = NULL, .flag = 0, .v = { 0 } }; + return (tabidx_t) { .key = NULL, .flag = 0, .value = { 0 } }; return list[id].tab[idx]; } // setkey - adds the given pair to the given key table // if setkey fails it will return 1 otherwise 0 -int setkey(struct keytablist **list, int id, char *pair) +int setkey(tablist_t **list, int id, char *pair) { if (id >= (*list)[0].len) { - *list = realloc(*list, (id + 1) * sizeof(struct keytablist)); + *list = realloc(*list, (id + 1) * sizeof(tablist_t)); for (int i = (*list)[0].len; i <= id; ++i) { for (int j = 0; j < TABLEN; ++j) - (*list)[i].tab[j] = (struct keytab) {NULL, 0, {0}}; + (*list)[i].tab[j] = (tabidx_t) { NULL, 0, { 0 } }; } (*list)[0].len = id + 1; } char *tok = strtok(pair, ":"); char *key = calloc(strlen(tok) + 1, sizeof(char)); strcpy(key, tok); - - tok = strtok(NULL, ":"); - if (tok == NULL) { - fprintf(stderr, "Invalid key-value pair\n"); + if (!(tok = strtok(NULL, ":"))) return 1; - } - union value v; - int flag; - if (isdigit(*tok)) { - flag = 1; - v.num = atof(tok); - } else if (!strcmp(tok, "true") || !strcmp(tok, "false")) { - flag = 2; - v.b = !strcmp(tok, "true"); - } else { - flag = 3; - v.str = calloc(strlen(tok) + 1, sizeof(char)); - strcpy(v.str, tok); - } int idx = hash(key); 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"); + if (idx >= TABLEN) return 2; - } if (!(*list)[id].tab[idx].key) (*list)[id].tab[idx].key = key; else { free(key); if ((*list)[id].tab[idx].flag == 3) - free((*list)[id].tab[idx].v.str); + free((*list)[id].tab[idx].value.str); + } + if (isdigit(*tok)) { + (*list)[id].tab[idx].flag = 1; + (*list)[id].tab[idx].value.num = atof(tok); + } else if (!strcmp(tok, "true") || !strcmp(tok, "false")) { + (*list)[id].tab[idx].flag = 2; + (*list)[id].tab[idx].value.boolean = !strcmp(tok, "true"); + } else { + (*list)[id].tab[idx].flag = 3; + (*list)[id].tab[idx].value.str = calloc(strlen(tok) + 1, sizeof(char)); + strcpy((*list)[id].tab[idx].value.str, tok); } - (*list)[id].tab[idx].v = v; - (*list)[id].tab[idx].flag = flag; return 0; } // delkey - removes the given key from the given key table -void delkey(struct keytablist *list, int id, char *key) +int delkey(tablist_t *list, int id, char *key) { int idx = hash(key); if (list[id].tab[idx].key == NULL) - return; + return 1; while (strcmp(list[id].tab[idx].key, key) && idx < TABLEN) idx++; - if (idx >= TABLEN) { - fprintf(stderr, "Invalid key: %s\n", key); - return; - } + if (idx >= TABLEN) + return 2; free(list[id].tab[idx].key); list[id].tab[idx].key = NULL; if (list[id].tab[idx].flag == 3) { - free(list[id].tab[idx].v.str); - list[id].tab[idx].v.str = NULL; + free(list[id].tab[idx].value.str); + list[id].tab[idx].value.str = NULL; } list[id].tab[idx].flag = 0; + return 0; } static int hash(char *key) diff --git a/src/lib/mdb.h b/src/lib/mdb.h index 7408dc7..bd4554b 100644 --- a/src/lib/mdb.h +++ b/src/lib/mdb.h @@ -3,31 +3,29 @@ #define TABLEN 1024 -union value { - char *str; - double num; - unsigned int b : 1; -}; - -struct keytab { +typedef struct { char *key; int flag; - union value v; -}; + union { + char *str; + double num; + unsigned int boolean : 1; + } value; +} tabidx_t; -struct keytablist { +typedef struct { int len; - struct keytab tab[TABLEN]; -}; + tabidx_t tab[TABLEN]; +} tablist_t; // Table operations -int *getkeys(struct keytablist *list, int id); -struct keytab getkey(struct keytablist *list, int id, char *key); -int setkey(struct keytablist **list, int id, char *pair); -void delkey(struct keytablist *list, int id, char *key); +int *getkeys(tablist_t *list, int id); +tabidx_t getkey(tablist_t *list, int id, char *key); +int setkey(tablist_t **list, int id, char *pair); +int delkey(tablist_t *list, int id, char *key); // file operations -struct keytablist *readdb(char *filename); -void writedb(char *filename, struct keytablist *list); +tablist_t *readdb(char *filename); +void writedb(char *filename, tablist_t *list); #endif -- cgit v1.2.3