working on db.c

This commit is contained in:
Aaron Blakely 2024-03-04 13:37:24 -06:00
parent f275caf11f
commit 1ef5bdf07b
6 changed files with 195 additions and 0 deletions

25
lib/db.h Executable file
View File

@ -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

2
mods/lua/scripts.tmp Executable file
View File

@ -0,0 +1,2 @@
hello.lua
test.lua

129
src/db.c Executable file
View File

@ -0,0 +1,129 @@
#include "util.h"
#include "irc.h"
#include "db.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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;
}

BIN
test.db Executable file

Binary file not shown.

39
test_db.c Executable file
View File

@ -0,0 +1,39 @@
#include "db.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#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;
}

BIN
testdb Executable file

Binary file not shown.