db.c supports multiple types now
This commit is contained in:
parent
b61a71e6ae
commit
3ff13e35b1
21
lib/db.h
21
lib/db.h
@ -4,12 +4,20 @@
|
|||||||
#define DB_MAGIC 0xdeadbeef
|
#define DB_MAGIC 0xdeadbeef
|
||||||
#define DB_VER 0x10
|
#define DB_VER 0x10
|
||||||
|
|
||||||
|
enum db_type
|
||||||
|
{
|
||||||
|
DB_TYPE_CHAR,
|
||||||
|
DB_TYPE_INT
|
||||||
|
};
|
||||||
|
|
||||||
struct db_hash
|
struct db_hash
|
||||||
{
|
{
|
||||||
int key_len;
|
int key_len;
|
||||||
int value_len;
|
int value_len;
|
||||||
|
int type;
|
||||||
|
|
||||||
char *key;
|
char *key;
|
||||||
char *value;
|
void *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct db_table
|
struct db_table
|
||||||
@ -23,8 +31,15 @@ struct db_table
|
|||||||
|
|
||||||
int write_db(struct db_table *db, char *fname);
|
int write_db(struct db_table *db, char *fname);
|
||||||
struct db_table *read_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);
|
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
|
#endif
|
||||||
|
62
src/db.c
62
src/db.c
@ -89,7 +89,7 @@ struct db_table *read_db(char *fname)
|
|||||||
return db;
|
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;
|
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));
|
db->hashes = (struct db_hash *)realloc(db->hashes, sizeof(struct db_hash) * (db->count + 1));
|
||||||
|
|
||||||
// zero out reallocated memory
|
// zero out reallocated memory
|
||||||
memset(&db->hashes[db->count], 0, sizeof(struct db_hash));
|
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;
|
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);
|
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);
|
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);
|
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)
|
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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_hash(struct db_table *db, char *key)
|
void *get_hash(struct db_table *db, char *key)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@ -158,3 +183,18 @@ char *get_hash(struct db_table *db, char *key)
|
|||||||
|
|
||||||
return NULL;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
29
test_db.c
29
test_db.c
@ -11,22 +11,6 @@ int main()
|
|||||||
struct db_table *db;
|
struct db_table *db;
|
||||||
int i;
|
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)
|
if (access(FNAME, F_OK) == -1)
|
||||||
{
|
{
|
||||||
printf("Creating db\n");
|
printf("Creating db\n");
|
||||||
@ -43,8 +27,8 @@ int main()
|
|||||||
// write some data if db is empty
|
// write some data if db is empty
|
||||||
if (db->count == 0)
|
if (db->count == 0)
|
||||||
{
|
{
|
||||||
db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
|
db_add_hash_char(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
|
||||||
db_add_hash(db, "lua.scriptcount", "2");
|
db_add_hash_int(db, "lua.scriptcount", 2);
|
||||||
write_db(db, FNAME);
|
write_db(db, FNAME);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -52,7 +36,14 @@ int main()
|
|||||||
|
|
||||||
for (i = 0; i < db->count; i++)
|
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);
|
free(db);
|
||||||
|
Loading…
Reference in New Issue
Block a user