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)/module.c -o $(OBJ)/module.o
|
||||||
$(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o
|
$(CC) $(CFLAGS) $(SRC)/channel.c -o $(OBJ)/channel.o
|
||||||
$(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o
|
$(CC) $(CFLAGS) $(SRC)/timers.c -o $(OBJ)/timers.o
|
||||||
|
$(CC) $(CFLAGS) $(SRC)/db.c -o $(OBJ)/db.o
|
||||||
|
|
||||||
$(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS)
|
$(CC) -o $(EXEC) $(OBJECTS) $(BINFLAGS)
|
||||||
@echo "All Done!"
|
@echo "All Done!"
|
||||||
|
|
||||||
|
13
lib/db.h
13
lib/db.h
@ -1,10 +1,15 @@
|
|||||||
#ifndef DB_H
|
#ifndef DB_H
|
||||||
#define DB_H
|
#define DB_H
|
||||||
|
|
||||||
|
#define DB_MAGIC 0xdeadbeef
|
||||||
|
#define DB_VER 0x10
|
||||||
|
|
||||||
struct db_hash
|
struct db_hash
|
||||||
{
|
{
|
||||||
char key[85];
|
int key_len;
|
||||||
char value[4096];
|
int value_len;
|
||||||
|
char *key;
|
||||||
|
char *value;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct db_table
|
struct db_table
|
||||||
@ -18,8 +23,8 @@ 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 add_hash(struct db_table *db, char *key, char *value);
|
int db_add_hash(struct db_table *db, char *key, char *value);
|
||||||
int 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);
|
char *get_hash(struct db_table *db, char *key);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -9,6 +9,8 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "db.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#endif
|
#endif
|
||||||
@ -30,6 +32,9 @@ struct irc_conn
|
|||||||
char port[5];
|
char port[5];
|
||||||
char real_name[512];
|
char real_name[512];
|
||||||
|
|
||||||
|
char db_file[256];
|
||||||
|
struct db_table *db;
|
||||||
|
|
||||||
// I/O Buffers
|
// I/O Buffers
|
||||||
char *out;
|
char *out;
|
||||||
char *in;
|
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))
|
if (config_lookup_string(cf, "bot.admin", &base))
|
||||||
strlcpy(bot.admin, base, sizeof bot.admin);
|
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");
|
autoload = config_lookup(cf, "mods.autoload");
|
||||||
count = config_setting_length(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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
db->db_magic = 0xdeadbeef;
|
db->db_magic = DB_MAGIC;
|
||||||
db->db_ver = 0x10;
|
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);
|
fclose(fp);
|
||||||
|
|
||||||
@ -30,6 +41,7 @@ struct db_table *read_db(char *fname)
|
|||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
struct db_table *db;
|
struct db_table *db;
|
||||||
|
int tmp;
|
||||||
|
|
||||||
if ((fp = fopen(fname, "rb")) == NULL)
|
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);
|
fread(db, sizeof(struct db_table), 1, fp);
|
||||||
|
|
||||||
// check the magic value
|
// 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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// check the version
|
// 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;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * db->count);
|
tmp = db->count != 0 ? db->count : sizeof(struct db_hash);
|
||||||
|
db->hashes = (struct db_hash *)malloc(sizeof(struct db_hash) * tmp);
|
||||||
// skip padding
|
|
||||||
fseek(fp, 8, SEEK_CUR);
|
|
||||||
|
|
||||||
fread(db->hashes, sizeof(struct db_hash), db->count, fp);
|
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);
|
fclose(fp);
|
||||||
|
|
||||||
return db;
|
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;
|
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));
|
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));
|
// zero out reallocated memory
|
||||||
memset(db->hashes[db->count].value, 0, sizeof(db->hashes[db->count].value));
|
memset(&db->hashes[db->count], 0, sizeof(struct db_hash));
|
||||||
|
|
||||||
strlcpy(db->hashes[db->count].key, key, sizeof(db->hashes[db->count].key));
|
db->hashes[db->count].key_len = strlen(key) + 1;
|
||||||
strlcpy(db->hashes[db->count].value, value, sizeof(db->hashes[db->count].value));
|
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++;
|
db->count++;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int del_hash(struct db_table *db, char *key)
|
int db_del_hash(struct db_table *db, char *key)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
20
src/main.c
20
src/main.c
@ -9,8 +9,11 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "irc.h"
|
#include "irc.h"
|
||||||
|
#include "db.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "events.h"
|
#include "events.h"
|
||||||
#include "module.h"
|
#include "module.h"
|
||||||
@ -50,6 +53,23 @@ int main()
|
|||||||
// Read the config
|
// Read the config
|
||||||
bot = read_config(bot, "xbot.cfg");
|
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
|
// Connect to the server
|
||||||
printf("Connecting to %s...\n", bot.host);
|
printf("Connecting to %s...\n", bot.host);
|
||||||
|
|
||||||
|
32
test_db.c
32
test_db.c
@ -2,20 +2,22 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
#define FNAME "test.db"
|
#define FNAME "xbot.db"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
struct db_table *db;
|
struct db_table *db;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
/*
|
||||||
db = (struct db_table *)malloc(sizeof(struct db_table));
|
db = (struct db_table *)malloc(sizeof(struct db_table));
|
||||||
db->count = 0;
|
db->count = 0;
|
||||||
db->hashes = NULL;
|
db->hashes = NULL;
|
||||||
|
|
||||||
add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
|
db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
|
||||||
add_hash(db, "lua.scriptcount", "2");
|
db_add_hash(db, "lua.scriptcount", "2");
|
||||||
|
|
||||||
if (write_db(db, FNAME) == -1)
|
if (write_db(db, FNAME) == -1)
|
||||||
{
|
{
|
||||||
@ -23,16 +25,36 @@ int main()
|
|||||||
}
|
}
|
||||||
|
|
||||||
free(db);
|
free(db);
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
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++)
|
for (i = 0; i < db->count; i++)
|
||||||
{
|
{
|
||||||
printf("Key: %s, Value: %s\n", db->hashes[i].key, db->hashes[i].value);
|
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);
|
free(db);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
2
xbot.cfg
2
xbot.cfg
@ -6,6 +6,8 @@ bot:
|
|||||||
nick = "xbot";
|
nick = "xbot";
|
||||||
user = "xbot";
|
user = "xbot";
|
||||||
admin = "ab3800";
|
admin = "ab3800";
|
||||||
|
|
||||||
|
db = "xbot.db";
|
||||||
};
|
};
|
||||||
|
|
||||||
server:
|
server:
|
||||||
|
Loading…
Reference in New Issue
Block a user