more work on db.c
This commit is contained in:
parent
1ef5bdf07b
commit
b61a71e6ae
2
Makefile
2
Makefile
@ -22,6 +22,8 @@ main:
|
||||
$(CC) $(CFLAGS) $(SRC)/module.c -o $(OBJ)/module.o
|
||||
$(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o
|
||||
$(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o
|
||||
$(CC) $(CFLAGS) $(SRC)/db.c -o $(OBJ)/db.o
|
||||
|
||||
$(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS)
|
||||
@echo "All Done!"
|
||||
|
||||
|
13
lib/db.h
13
lib/db.h
@ -1,10 +1,15 @@
|
||||
#ifndef DB_H
|
||||
#define DB_H
|
||||
|
||||
#define DB_MAGIC 0xdeadbeef
|
||||
#define DB_VER 0x10
|
||||
|
||||
struct db_hash
|
||||
{
|
||||
char key[85];
|
||||
char value[4096];
|
||||
int key_len;
|
||||
int value_len;
|
||||
char *key;
|
||||
char *value;
|
||||
};
|
||||
|
||||
struct db_table
|
||||
@ -18,8 +23,8 @@ struct db_table
|
||||
|
||||
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);
|
||||
int db_add_hash(struct db_table *db, char *key, char *value);
|
||||
int db_del_hash(struct db_table *db, char *key);
|
||||
char *get_hash(struct db_table *db, char *key);
|
||||
|
||||
#endif
|
||||
|
@ -9,6 +9,8 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "db.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
#endif
|
||||
@ -30,6 +32,9 @@ struct irc_conn
|
||||
char port[5];
|
||||
char real_name[512];
|
||||
|
||||
char db_file[256];
|
||||
struct db_table *db;
|
||||
|
||||
// I/O Buffers
|
||||
char *out;
|
||||
char *in;
|
||||
|
@ -41,6 +41,9 @@ struct irc_conn read_config(struct irc_conn bot, char *file)
|
||||
if (config_lookup_string(cf, "bot.admin", &base))
|
||||
strlcpy(bot.admin, base, sizeof bot.admin);
|
||||
|
||||
if (config_lookup_string(cf, "bot.db", &base))
|
||||
strlcpy(bot.db_file, base, sizeof bot.db_file);
|
||||
|
||||
autoload = config_lookup(cf, "mods.autoload");
|
||||
count = config_setting_length(autoload);
|
||||
|
||||
|
65
src/db.c
65
src/db.c
@ -16,10 +16,21 @@ int write_db(struct db_table *db, char *fname)
|
||||
return -1;
|
||||
}
|
||||
|
||||
db->db_magic = 0xdeadbeef;
|
||||
db->db_ver = 0x10;
|
||||
db->db_magic = DB_MAGIC;
|
||||
db->db_ver = DB_VER;
|
||||
|
||||
fwrite(db, sizeof(struct db_table) + (sizeof(struct db_hash) * db->count), 1, fp);
|
||||
// write the header
|
||||
fwrite(db, sizeof(struct db_table), 1, fp);
|
||||
|
||||
// write the hashes
|
||||
fwrite(db->hashes, sizeof(struct db_hash), db->count, fp);
|
||||
|
||||
// write the keys and values
|
||||
for (i = 0; i < db->count; i++)
|
||||
{
|
||||
fwrite(db->hashes[i].key, sizeof(char), db->hashes[i].key_len, fp);
|
||||
fwrite(db->hashes[i].value, sizeof(char), db->hashes[i].value_len, fp);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
@ -30,6 +41,7 @@ struct db_table *read_db(char *fname)
|
||||
{
|
||||
FILE *fp;
|
||||
struct db_table *db;
|
||||
int tmp;
|
||||
|
||||
if ((fp = fopen(fname, "rb")) == NULL)
|
||||
{
|
||||
@ -41,35 +53,43 @@ struct db_table *read_db(char *fname)
|
||||
fread(db, sizeof(struct db_table), 1, fp);
|
||||
|
||||
// check the magic value
|
||||
if (db->db_magic != 0xdeadbeef)
|
||||
if (db->db_magic != DB_MAGIC)
|
||||
{
|
||||
printf("Error: %s incompatible or unknown db file format\n", fname);
|
||||
printf("Error: %s incompatible or unknown db file format: Bad Magic\n", fname);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
// check the version
|
||||
if (db->db_ver != 0x10)
|
||||
if (db->db_ver != DB_VER)
|
||||
{
|
||||
printf("Error: %s incompatible or unknown db file format\n", fname);
|
||||
printf("Error: %s incompatible or unknown db file format: Incompatible Version\n", fname);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * db->count);
|
||||
|
||||
// skip padding
|
||||
fseek(fp, 8, SEEK_CUR);
|
||||
tmp = db->count != 0 ? db->count : sizeof(struct db_hash);
|
||||
db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * tmp);
|
||||
|
||||
fread(db->hashes, sizeof(struct db_hash), db->count, fp);
|
||||
|
||||
// read the keys and values
|
||||
for (tmp = 0; tmp < db->count; tmp++)
|
||||
{
|
||||
db->hashes[tmp].key = (char *)malloc(sizeof(char) * db->hashes[tmp].key_len);
|
||||
db->hashes[tmp].value = (char *)malloc(sizeof(char) * db->hashes[tmp].value_len);
|
||||
|
||||
fread(db->hashes[tmp].key, sizeof(char), db->hashes[tmp].key_len, fp);
|
||||
fread(db->hashes[tmp].value, sizeof(char), db->hashes[tmp].value_len, fp);
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
int add_hash(struct db_table *db, char *key, char *value)
|
||||
int db_add_hash(struct db_table *db, char *key, char *value)
|
||||
{
|
||||
int i;
|
||||
|
||||
@ -83,17 +103,28 @@ int add_hash(struct db_table *db, char *key, char *value)
|
||||
|
||||
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));
|
||||
// zero out reallocated memory
|
||||
memset(&db->hashes[db->count], 0, sizeof(struct db_hash));
|
||||
|
||||
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->hashes[db->count].key_len = strlen(key) + 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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int del_hash(struct db_table *db, char *key)
|
||||
int db_del_hash(struct db_table *db, char *key)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
20
src/main.c
20
src/main.c
@ -9,8 +9,11 @@
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "irc.h"
|
||||
#include "db.h"
|
||||
#include "util.h"
|
||||
#include "events.h"
|
||||
#include "module.h"
|
||||
@ -50,6 +53,23 @@ int main()
|
||||
// Read the config
|
||||
bot = read_config(bot, "xbot.cfg");
|
||||
|
||||
// check if the db exists, if not, create it
|
||||
if (access(bot.db_file, F_OK) == -1)
|
||||
{
|
||||
printf("Creating db\n");
|
||||
bot.db = (struct db_table *)malloc(sizeof(struct db_table));
|
||||
memset(bot.db, 0, sizeof(struct db_table));
|
||||
|
||||
bot.db->count = 0;
|
||||
bot.db->hashes = NULL;
|
||||
|
||||
write_db(bot.db, bot.db_file);
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.db = read_db(bot.db_file);
|
||||
}
|
||||
|
||||
// Connect to the server
|
||||
printf("Connecting to %s...\n", bot.host);
|
||||
|
||||
|
34
test_db.c
34
test_db.c
@ -2,20 +2,22 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FNAME "test.db"
|
||||
#define FNAME "xbot.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");
|
||||
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)
|
||||
{
|
||||
@ -23,16 +25,36 @@ int main()
|
||||
}
|
||||
|
||||
free(db);
|
||||
*/
|
||||
|
||||
db = read_db(FNAME);
|
||||
if (access(FNAME, F_OK) == -1)
|
||||
{
|
||||
printf("Creating db\n");
|
||||
|
||||
db = (struct db_table *)malloc(sizeof(struct db_table));
|
||||
db->count = 0;
|
||||
db->hashes = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
db = read_db(FNAME);
|
||||
}
|
||||
|
||||
// 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");
|
||||
write_db(db, FNAME);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
|
2
xbot.cfg
2
xbot.cfg
@ -6,6 +6,8 @@ bot:
|
||||
nick = "xbot";
|
||||
user = "xbot";
|
||||
admin = "ab3800";
|
||||
|
||||
db = "xbot.db";
|
||||
};
|
||||
|
||||
server:
|
||||
|
Loading…
Reference in New Issue
Block a user