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 0000000..1fd816b Binary files /dev/null and b/test.db differ diff --git a/test_db.c b/test_db.c new file mode 100755 index 0000000..a6289a6 --- /dev/null +++ b/test_db.c @@ -0,0 +1,39 @@ +#include "db.h" +#include +#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 0000000..c6f3e38 Binary files /dev/null and b/testdb differ