db.c supports multiple types now

This commit is contained in:
Aaron Blakely 2024-03-05 22:53:00 -06:00
parent b61a71e6ae
commit 3ff13e35b1
5 changed files with 79 additions and 33 deletions

View File

@ -4,12 +4,20 @@
#define DB_MAGIC 0xdeadbeef
#define DB_VER 0x10
enum db_type
{
DB_TYPE_CHAR,
DB_TYPE_INT
};
struct db_hash
{
int key_len;
int value_len;
int type;
char *key;
char *value;
void *value;
};
struct db_table
@ -23,8 +31,15 @@ struct db_table
int write_db(struct db_table *db, char *fname);
struct db_table *read_db(char *fname);
int db_add_hash(struct db_table *db, char *key, char *value);
int db_add_hash(struct db_table *db, char *key, void *value);
int db_add_hash_char(struct db_table *db, char *key, char *value);
int db_add_hash_int(struct db_table *db, char *key, int value);
int db_del_hash(struct db_table *db, char *key);
char *get_hash(struct db_table *db, char *key);
void *get_hash(struct db_table *db, char *key);
char *get_hash_char(struct db_table *db, char *key);
int get_hash_int(struct db_table *db, char *key);
#endif

View File

@ -89,7 +89,7 @@ struct db_table *read_db(char *fname)
return db;
}
int db_add_hash(struct db_table *db, char *key, char *value)
int db_add_hash(struct db_table *db, char *key, void *value)
{
int i;
@ -101,27 +101,52 @@ int db_add_hash(struct db_table *db, char *key, char *value)
}
}
db->hashes[db->count].key_len = strlen(key) + 1;
db->hashes[db->count].key = (char *)malloc(sizeof(char) * db->hashes[db->count].key_len);
memset(db->hashes[db->count].key, 0, sizeof(char) * db->hashes[db->count].key_len);
strlcpy(db->hashes[db->count].key, key, sizeof(char) * db->hashes[db->count].key_len);
db->hashes[db->count].value = value;
db->count++;
return 0;
}
int db_add_hash_char(struct db_table *db, char *key, char *value)
{
db->hashes = (struct db_hash *)realloc(db->hashes, sizeof(struct db_hash) * (db->count + 1));
// zero out reallocated memory
memset(&db->hashes[db->count], 0, sizeof(struct db_hash));
db->hashes[db->count].key_len = strlen(key) + 1;
db->hashes[db->count].type = DB_TYPE_CHAR;
db->hashes[db->count].value_len = strlen(value) + 1;
printf("key_len: %d, value_len: %d\n", sizeof(char) * db->hashes[db->count].key_len, sizeof(char) * db->hashes[db->count].value_len);
db->hashes[db->count].key = (char *)malloc(sizeof(char) * db->hashes[db->count].key_len);
db->hashes[db->count].value = (char *)malloc(sizeof(char) * db->hashes[db->count].value_len);
memset(db->hashes[db->count].key, 0, sizeof(char) * db->hashes[db->count].key_len);
memset(db->hashes[db->count].value, 0, sizeof(char) * db->hashes[db->count].value_len);
strlcpy(db->hashes[db->count].key, key, sizeof(char) * db->hashes[db->count].key_len);
strlcpy(db->hashes[db->count].value, value, sizeof(char) * db->hashes[db->count].value_len);
db->count++;
return 0;
return db_add_hash(db, key, db->hashes[db->count].value);
}
int db_add_hash_int(struct db_table *db, char *key, int value)
{
db->hashes = (struct db_hash *)realloc(db->hashes, sizeof(struct db_hash) * (db->count + 1));
// zero out reallocated memory
memset(&db->hashes[db->count], 0, sizeof(struct db_hash));
db->hashes[db->count].type = DB_TYPE_INT;
db->hashes[db->count].value_len = sizeof(int);
db->hashes[db->count].value = (int *)malloc(sizeof(int));
memcpy(db->hashes[db->count].value, &value, sizeof(int));
return db_add_hash(db, key, db->hashes[db->count].value);
}
int db_del_hash(struct db_table *db, char *key)
@ -144,7 +169,7 @@ int db_del_hash(struct db_table *db, char *key)
return -1;
}
char *get_hash(struct db_table *db, char *key)
void *get_hash(struct db_table *db, char *key)
{
int i;
@ -158,3 +183,18 @@ char *get_hash(struct db_table *db, char *key)
return NULL;
}
char *get_hash_char(struct db_table *db, char *key)
{
return (char *)get_hash(db, key);
}
int get_hash_int(struct db_table *db, char *key)
{
int value;
memcpy(&value, get_hash(db, key), sizeof(int));
return value;
}

View File

@ -11,22 +11,6 @@ int main()
struct db_table *db;
int i;
/*
db = (struct db_table *)malloc(sizeof(struct db_table));
db->count = 0;
db->hashes = NULL;
db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
db_add_hash(db, "lua.scriptcount", "2");
if (write_db(db, FNAME) == -1)
{
printf("Error writing db\n");
}
free(db);
*/
if (access(FNAME, F_OK) == -1)
{
printf("Creating db\n");
@ -43,8 +27,8 @@ int main()
// write some data if db is empty
if (db->count == 0)
{
db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
db_add_hash(db, "lua.scriptcount", "2");
db_add_hash_char(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
db_add_hash_int(db, "lua.scriptcount", 2);
write_db(db, FNAME);
return 0;
@ -52,7 +36,14 @@ int main()
for (i = 0; i < db->count; i++)
{
printf("Key: %s, Value: %s\n", db->hashes[i].key, db->hashes[i].value);
if (db->hashes[i].type == DB_TYPE_INT)
{
printf("Key: %s, Value: %d\n", db->hashes[i].key, get_hash_int(db, db->hashes[i].key));
}
else
{
printf("Key: %s, Value: %s\n", db->hashes[i].key, get_hash_char(db, db->hashes[i].key));
}
}
free(db);

BIN
testdb

Binary file not shown.

BIN
xbot.db Executable file → Normal file

Binary file not shown.