From 7c8e83f655e41b11e970fd0abad5a96a3fb73e47 Mon Sep 17 00:00:00 2001 From: bpc2003 Date: Mon, 10 Mar 2025 11:15:04 -0400 Subject: Consolidated each header into single header --- src/file.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/fileops.c | 54 --------------------------------------------------- src/include/fileops.h | 9 --------- src/include/keytab.h | 27 -------------------------- src/include/mdb.h | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/include/parser.h | 20 ------------------- src/keytab.c | 2 +- src/main.c | 4 +--- src/parser.c | 2 +- 9 files changed, 104 insertions(+), 115 deletions(-) create mode 100644 src/file.c delete mode 100644 src/fileops.c delete mode 100644 src/include/fileops.h delete mode 100644 src/include/keytab.h create mode 100644 src/include/mdb.h delete mode 100644 src/include/parser.h (limited to 'src') diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..ba24ed3 --- /dev/null +++ b/src/file.c @@ -0,0 +1,53 @@ +#include +#include +#include +#include + +#include "include/mdb.h" + +uint8_t *readdb(char *filename) +{ + FILE *fp = fopen(filename, "rb"); + if (fp == NULL) + return NULL; + + int len = 10; + uint8_t *buf = calloc(len, sizeof(uint8_t)); + int c, i = 0; + while ((c = fgetc(fp)) != EOF) { + if (i >= len) { + len *= 2; + buf = realloc(buf, len * sizeof(uint8_t)); + memset(buf + i, 0, (len - i) * sizeof(uint8_t)); + } + buf[i++] = c; + } + + fclose(fp); + return buf; +} + +void writedb(char *filename, struct keytablist *list, int len) +{ + FILE *fp = fopen(filename, "wb"); + for (int i = 0; i < len; ++i) { + fprintf(fp, "\xfb"); + int *indexes = getkeys(list, i); + for (int j = 0; indexes[j]; ++j) { + fprintf(fp, "\xfa%s:", list[i].tab[indexes[j]].key); + switch (list[i].tab[indexes[j]].flag) { + case 1: + fprintf(fp, "%.2lf", list[i].tab[indexes[j]].v.num); + break; + case 2: + fprintf(fp, "%s", list[i].tab[indexes[j]].v.b == 1 ? "true" : "false"); + break; + case 3: + fprintf(fp, "%s", list[i].tab[indexes[j]].v.str); + break; + } + } + free(indexes); + fprintf(fp, "\xfe"); + } +} diff --git a/src/fileops.c b/src/fileops.c deleted file mode 100644 index e43a455..0000000 --- a/src/fileops.c +++ /dev/null @@ -1,54 +0,0 @@ -#include -#include -#include -#include - -#include "include/fileops.h" -#include "include/keytab.h" - -uint8_t *readdb(char *filename) -{ - FILE *fp = fopen(filename, "rb"); - if (fp == NULL) - return NULL; - - int len = 10; - uint8_t *buf = calloc(len, sizeof(uint8_t)); - int c, i = 0; - while ((c = fgetc(fp)) != EOF) { - if (i >= len) { - len *= 2; - buf = realloc(buf, len * sizeof(uint8_t)); - memset(buf + i, 0, (len - i) * sizeof(uint8_t)); - } - buf[i++] = c; - } - - fclose(fp); - return buf; -} - -void writedb(char *filename, struct keytablist *list, int len) -{ - FILE *fp = fopen(filename, "wb"); - for (int i = 0; i < len; ++i) { - fprintf(fp, "\xfb"); - int *indexes = getkeys(list, i); - for (int j = 0; indexes[j]; ++j) { - fprintf(fp, "\xfa%s:", list[i].tab[indexes[j]].key); - switch (list[i].tab[indexes[j]].flag) { - case 1: - fprintf(fp, "%.2lf", list[i].tab[indexes[j]].v.num); - break; - case 2: - fprintf(fp, "%s", list[i].tab[indexes[j]].v.b == 1 ? "true" : "false"); - break; - case 3: - fprintf(fp, "%s", list[i].tab[indexes[j]].v.str); - break; - } - } - free(indexes); - fprintf(fp, "\xfe"); - } -} diff --git a/src/include/fileops.h b/src/include/fileops.h deleted file mode 100644 index 371f6e8..0000000 --- a/src/include/fileops.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef FILEOPS_H -#define FILEOPS_H - -#include "keytab.h" - -unsigned char *readdb(char *filename); -void writedb(char *filename, struct keytablist *list, int len); - -#endif diff --git a/src/include/keytab.h b/src/include/keytab.h deleted file mode 100644 index 7e15643..0000000 --- a/src/include/keytab.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef KEYTAB_H -#define KEYTAB_H - -#define TABLEN 1024 - -union value { - char *str; - double num; - unsigned int b : 1; -}; - -struct keytab { - char *key; - int flag; - union value v; -}; - -struct keytablist { - struct keytab tab[TABLEN]; -}; - -int *getkeys(struct keytablist *list, int id); -struct keytab getkey(struct keytablist *list, int id, char *key); -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/include/mdb.h b/src/include/mdb.h new file mode 100644 index 0000000..6e6fca9 --- /dev/null +++ b/src/include/mdb.h @@ -0,0 +1,48 @@ +#ifndef FILEOPS_H +#define FILEOPS_H + +#define TABLEN 1024 + +extern int blen; + +enum btype { + BEGIN = 1, + END, PAIR, + + ERROR +}; + +struct byte { + enum btype type; + char *value; +}; + +struct byte *parse(unsigned char *buf); + +union value { + char *str; + double num; + unsigned int b : 1; +}; + +struct keytab { + char *key; + int flag; + union value v; +}; + +struct keytablist { + struct keytab tab[TABLEN]; +}; + +int *getkeys(struct keytablist *list, int id); +struct keytab getkey(struct keytablist *list, int id, char *key); +void setkey(struct keytablist **list, int *len, int id, char *pair); +void delkey(struct keytablist *list, int id, char *key); + +// TODO: integrate every header into single file +// TODO: make readdb return struct keytab list* +unsigned char *readdb(char *filename); +void writedb(char *filename, struct keytablist *list, int len); + +#endif diff --git a/src/include/parser.h b/src/include/parser.h deleted file mode 100644 index 25045ae..0000000 --- a/src/include/parser.h +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef PARSER_H -#define PARSER_H - -extern int blen; - -enum btype { - BEGIN = 1, - END, PAIR, - - ERROR -}; - -struct byte { - enum btype type; - char *value; -}; - -struct byte *parse(unsigned char *buf); - -#endif diff --git a/src/keytab.c b/src/keytab.c index 6fcf303..b1b12d7 100644 --- a/src/keytab.c +++ b/src/keytab.c @@ -3,7 +3,7 @@ #include #include -#include "include/keytab.h" +#include "include/mdb.h" static int hash(char *key); diff --git a/src/main.c b/src/main.c index 8bf5319..ac8f102 100644 --- a/src/main.c +++ b/src/main.c @@ -2,9 +2,7 @@ #include #include -#include "include/fileops.h" -#include "include/parser.h" -#include "include/keytab.h" +#include "include/mdb.h" int main(int argc, char **argv) { diff --git a/src/parser.c b/src/parser.c index 54e7ecc..a45c945 100644 --- a/src/parser.c +++ b/src/parser.c @@ -2,7 +2,7 @@ #include #include -#include "include/parser.h" +#include "include/mdb.h" int blen; -- cgit v1.2.3