db.c working on windows

This commit is contained in:
Aaron Blakely 2024-03-06 03:45:01 -06:00
parent c3281dc946
commit 8ebf060f48
10 changed files with 53 additions and 37 deletions

View File

@ -1,6 +1,8 @@
#ifndef DB_H
#define DB_H
#include "util.h"
#define DB_MAGIC 0xdeadbeef
#define DB_VER 0x10
@ -30,21 +32,21 @@ struct db_table
struct db_hash *hashes;
};
int db_write(struct db_table *db, char *fname);
struct db_table *db_read(char *fname);
MY_API int db_write(struct db_table *db, char *fname);
MY_API struct db_table *db_read(char *fname);
int db_set_hash(struct db_table *db, char *key, void *value);
int db_set_hash_char(struct db_table *db, char *key, char *value);
int db_set_hash_int(struct db_table *db, char *key, int value);
int db_set_hash_float(struct db_table *db, char *key, float value);
MY_API int db_set_hash(struct db_table *db, char *key, void *value);
MY_API int db_set_hash_char(struct db_table *db, char *key, char *value);
MY_API int db_set_hash_int(struct db_table *db, char *key, int value);
MY_API int db_set_hash_float(struct db_table *db, char *key, float value);
int db_del_hash(struct db_table *db, char *key);
MY_API int db_del_hash(struct db_table *db, char *key);
void *db_get_hash(struct db_table *db, char *key);
int db_get_hash_type(struct db_table *db, char *key);
MY_API void *db_get_hash(struct db_table *db, char *key);
MY_API int db_get_hash_type(struct db_table *db, char *key);
char *db_get_hash_char(struct db_table *db, char *key);
int db_get_hash_int(struct db_table *db, char *key);
float db_get_hash_float(struct db_table *db, char *key);
MY_API char *db_get_hash_char(struct db_table *db, char *key);
MY_API int db_get_hash_int(struct db_table *db, char *key);
MY_API float db_get_hash_float(struct db_table *db, char *key);
#endif

View File

@ -9,6 +9,7 @@
#include <stdio.h>
#include "util.h"
#include "db.h"
#ifdef _WIN32
@ -42,16 +43,6 @@ struct irc_conn
typedef struct handler event_handler;
#ifdef _WIN32
#ifdef MY_DLL_EXPORTS
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
#else
#define MY_API
#endif
void irc_connect(struct irc_conn *bot);
void irc_auth(struct irc_conn *bot);

View File

@ -12,17 +12,27 @@
#define false FALSE
#endif
#ifdef _WIN32
#ifdef MY_DLL_EXPORTS
#define MY_API __declspec(dllexport)
#else
#define MY_API __declspec(dllimport)
#endif
#else
#define MY_API
#endif
void eprint(char *fmt, ...);
#if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 38)) || defined(_WIN32)
void strlcpy(char *to, const char *from, int len);
MY_API void strlcpy(char *to, const char *from, int len);
#endif
#ifdef _WIN32
char *basename(char *path);
MY_API char *basename(char *path);
#endif
char *skip(char *s, char c);
void trim(char *s);
MY_API char *skip(char *s, char c);
MY_API void trim(char *s);
#endif

View File

@ -95,10 +95,6 @@ int remove_script(char *fname)
struct script_list get_scripts()
{
char *scriptlist = db_get_hash_char(get_bot_db(), "lua.scripts");
printf("dbug: scriptlist: %s\n", scriptlist);
// dbug: scriptlist: hello.lua,test.lua,youtube.lua
struct script_list list = {0};
char *p = scriptlist;
int i = 0;

2
mods/lua/scripts Executable file
View File

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

View File

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

View File

@ -10,6 +10,7 @@ int db_write(struct db_table *db, char *fname)
{
FILE *fp;
int i;
char *fullpath;
if ((fp = fopen(fname, "wb")) == NULL)
{
@ -19,6 +20,15 @@ int db_write(struct db_table *db, char *fname)
db->db_magic = DB_MAGIC;
db->db_ver = DB_VER;
// get the full path to the db file
#ifdef _WIN32
fullpath = _fullpath(NULL, fname, 0);
#else
fullpath = realpath(fname, NULL);
#endif
printf("Writing db to file: %s\n", fullpath);
// write the header
fwrite(db, sizeof(struct db_table), 1, fp);

View File

@ -9,7 +9,6 @@
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include "config.h"
#include "irc.h"
@ -24,6 +23,7 @@
#include <winsock2.h>
#else
#include <sys/select.h>
#include <unistd.h>
#endif
static time_t trespond;
@ -51,11 +51,14 @@ int main()
init_timers();
init_mods();
// Read the config
bot = read_config(bot, "xbot.cfg");
// check if the db exists, if not, create it
#ifdef _WIN32
if (access(bot.db_file, 0) == -1)
#else
if (access(bot.db_file, F_OK) == -1)
#endif
{
printf("Creating database file: %s\n", bot.db_file);
bot.db = (struct db_table *)malloc(sizeof(struct db_table));

View File

@ -11,7 +11,7 @@
#include <stdlib.h>
#include <ctype.h>
#include "irc.h"
#include "util.h"
void eprint(char *fmt, ...)
{
@ -30,7 +30,7 @@ void eprint(char *fmt, ...)
}
#if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 38)) || defined(_WIN32)
MY_API void strlcpy(char *to, const char *from, int len)
void strlcpy(char *to, const char *from, int len)
{
memccpy(to, from, '\0', len);
to[len-1] = '\0';
@ -38,14 +38,14 @@ MY_API void strlcpy(char *to, const char *from, int len)
#endif
#ifdef _WIN32
MY_API char *basename(char *path)
char *basename(char *path)
{
char *p = strrchr(path, '\\');
return p ? p + 1 : path;
}
#endif
MY_API char *skip(char *s, char c)
char *skip(char *s, char c)
{
while (*s != c && *s != '\0')
{

View File

@ -105,6 +105,7 @@
<ItemGroup>
<ClInclude Include="lib\channel.h" />
<ClInclude Include="lib\config.h" />
<ClInclude Include="lib\db.h" />
<ClInclude Include="lib\events.h" />
<ClInclude Include="lib\irc.h" />
<ClInclude Include="lib\module.h" />
@ -114,6 +115,7 @@
<ItemGroup>
<ClCompile Include="src\channel.c" />
<ClCompile Include="src\config.c" />
<ClCompile Include="src\db.c" />
<ClCompile Include="src\events.c" />
<ClCompile Include="src\irc.c" />
<ClCompile Include="src\main.c" />