From 1ef5bdf07b639feba7198a041f6f7e54a68d7858 Mon Sep 17 00:00:00 2001 From: Aaron Blakely Date: Mon, 4 Mar 2024 13:37:24 -0600 Subject: [PATCH] working on db.c --- lib/db.h | 25 +++++++++ mods/lua/scripts.tmp | 2 + src/db.c | 129 +++++++++++++++++++++++++++++++++++++++++++ test.db | Bin 0 -> 8386 bytes test_db.c | 39 +++++++++++++ testdb | Bin 0 -> 21392 bytes 6 files changed, 195 insertions(+) create mode 100755 lib/db.h create mode 100755 mods/lua/scripts.tmp create mode 100755 src/db.c create mode 100755 test.db create mode 100755 test_db.c create mode 100755 testdb diff --git a/lib/db.h b/lib/db.h new file mode 100755 index 0000000..3f7d82e --- /dev/null +++ b/lib/db.h @@ -0,0 +1,25 @@ +#ifndef DB_H +#define DB_H + +struct db_hash +{ + char key[85]; + char value[4096]; +}; + +struct db_table +{ + int db_magic; + int db_ver; + int count; + + struct db_hash *hashes; +}; + +int write_db(struct db_table *db, char *fname); +struct db_table *read_db(char *fname); +int add_hash(struct db_table *db, char *key, char *value); +int del_hash(struct db_table *db, char *key); +char *get_hash(struct db_table *db, char *key); + +#endif diff --git a/mods/lua/scripts.tmp b/mods/lua/scripts.tmp new file mode 100755 index 0000000..d7a1022 --- /dev/null +++ b/mods/lua/scripts.tmp @@ -0,0 +1,2 @@ +hello.lua +test.lua diff --git a/src/db.c b/src/db.c new file mode 100755 index 0000000..cf40b71 --- /dev/null +++ b/src/db.c @@ -0,0 +1,129 @@ +#include "util.h" +#include "irc.h" +#include "db.h" + +#include +#include +#include + +int write_db(struct db_table *db, char *fname) +{ + FILE *fp; + int i; + + if ((fp = fopen(fname, "wb")) == NULL) + { + return -1; + } + + db->db_magic = 0xdeadbeef; + db->db_ver = 0x10; + + fwrite(db, sizeof(struct db_table) + (sizeof(struct db_hash) * db->count), 1, fp); + + fclose(fp); + + return 0; +} + +struct db_table *read_db(char *fname) +{ + FILE *fp; + struct db_table *db; + + if ((fp = fopen(fname, "rb")) == NULL) + { + return NULL; + } + + db = (struct db_table *)malloc(sizeof(struct db_table)); + + fread(db, sizeof(struct db_table), 1, fp); + + // check the magic value + if (db->db_magic != 0xdeadbeef) + { + printf("Error: %s incompatible or unknown db file format\n", fname); + + return NULL; + } + + + // check the version + if (db->db_ver != 0x10) + { + printf("Error: %s incompatible or unknown db file format\n", fname); + + return NULL; + } + + db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * db->count); + + // skip padding + fseek(fp, 8, SEEK_CUR); + + fread(db->hashes, sizeof(struct db_hash), db->count, fp); + + fclose(fp); + + return db; +} + +int add_hash(struct db_table *db, char *key, char *value) +{ + int i; + + for (i = 0; i < db->count; i++) + { + if (strcmp(db->hashes[i].key, key) == 0) + { + return -1; + } + } + + db->hashes = (struct db_hash *)realloc(db->hashes, sizeof(struct db_hash) * (db->count + 1)); + + memset(db->hashes[db->count].key, 0, sizeof(db->hashes[db->count].key)); + memset(db->hashes[db->count].value, 0, sizeof(db->hashes[db->count].value)); + + strlcpy(db->hashes[db->count].key, key, sizeof(db->hashes[db->count].key)); + strlcpy(db->hashes[db->count].value, value, sizeof(db->hashes[db->count].value)); + db->count++; + + return 0; +} + +int del_hash(struct db_table *db, char *key) +{ + int i; + + for (i = 0; i < db->count; i++) + { + if (strcmp(db->hashes[i].key, key) == 0) + { + free(db->hashes[i].key); + free(db->hashes[i].value); + memmove(&db->hashes[i], &db->hashes[i + 1], sizeof(struct db_hash) * (db->count - i)); + db->count--; + + return 0; + } + } + + return -1; +} + +char *get_hash(struct db_table *db, char *key) +{ + int i; + + for (i = 0; i < db->count; i++) + { + if (strcmp(db->hashes[i].key, key) == 0) + { + return db->hashes[i].value; + } + } + + return NULL; +} diff --git a/test.db b/test.db new file mode 100755 index 0000000000000000000000000000000000000000..1fd816b47c50e8b4cd193c28c81adb98ef00fe51 GIT binary patch literal 8386 zcmeI$!3l#f6vpv~6G$g;fa0O^^r8|_2&ObI2d~|yD-^{&T%ovurag3lfIa*JFT9t- zm+(90{dz9Bh-7=kHJ;}6aTS@0IEmO$mA<)mH}&6x6nkr|b>$v?R^#>43@&J>P3%+L zKn4N`Ab +#include +#include + +#define FNAME "test.db" + +int main() +{ + struct db_table *db; + int i; + + db = (struct db_table *)malloc(sizeof(struct db_table)); + db->count = 0; + db->hashes = NULL; + + add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua"); + add_hash(db, "lua.scriptcount", "2"); + + if (write_db(db, FNAME) == -1) + { + printf("Error writing db\n"); + } + + free(db); + + db = read_db(FNAME); + + for (i = 0; i < db->count; i++) + { + printf("Key: %s, Value: %s\n", db->hashes[i].key, db->hashes[i].value); + } + + printf("test: lua.scripts: %s\n", get_hash(db, "lua.scripts")); + + free(db); + + return 0; +} diff --git a/testdb b/testdb new file mode 100755 index 0000000000000000000000000000000000000000..c6f3e382413fe5b706b2568379d8dc18bb0818cf GIT binary patch literal 21392 zcmeHPdvsh!d7rzxSGud``XO8L1Fek>Hoj^mf&^+V1|acmK+-~`NiwY!oQOS|em z{0K=g76yb6$A&|i216a-1k#d_LvxC2Oi-RqAb=BSaC;~f)B&k(g98pYiOKf&eKWI< zYc1sPM^D?M*>mTc?>XOmGk0e0-FxqcTDNU=7>3ZvDXtfUd8SEwaQ0oy&dSgm&OzCAIIYfK~ zbv!91bgWkKv_l_wlzf~EVb?pL^z3kplGl*xp!Dt+Q{g|Sz7D1Lte4FQJKQG8LPK5O zyP!vLd9z7(^UbQhV*Svh;;*3!t7zb7mcM4EIY7z)v{G98DylX@UNfz z=CYqXOfu9CvPm+Oh(?D_#M6hSy%|WcoIZ(QLlOGdK=)1}Pjydbr@RP#M-lq&BJ^8| z(33^zPlN8oVNY)dVKP6%Md-H|q1P9o({wqR{kkIbz9RIWfbPX%Pd*S9+KGe@q*F=J zorpvz3x-91q`yDD4_JI45)&Zx^~FP?UxGjY!#Wg5rhK4gVqAxUXP;~E4L{kw$L;VAyI~j@W6=aXjidEurJ&Kds*~lDC8FogC!Rbyua{Cx)aKEt4LW&!vP5?1&}o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