xbot/test_db.c

62 lines
1.1 KiB
C
Raw Normal View History

2024-03-04 19:37:24 +00:00
#include "db.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
2024-03-06 02:12:10 +00:00
#include <unistd.h>
2024-03-04 19:37:24 +00:00
2024-03-06 02:12:10 +00:00
#define FNAME "xbot.db"
2024-03-04 19:37:24 +00:00
int main()
{
struct db_table *db;
int i;
2024-03-06 02:12:10 +00:00
/*
2024-03-04 19:37:24 +00:00
db = (struct db_table *)malloc(sizeof(struct db_table));
db->count = 0;
db->hashes = NULL;
2024-03-06 02:12:10 +00:00
db_add_hash(db, "lua.scripts", "hello.lua,test.lua,youtube.lua");
db_add_hash(db, "lua.scriptcount", "2");
2024-03-04 19:37:24 +00:00
if (write_db(db, FNAME) == -1)
{
printf("Error writing db\n");
}
free(db);
2024-03-06 02:12:10 +00:00
*/
2024-03-04 19:37:24 +00:00
2024-03-06 02:12:10 +00:00
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;
}
2024-03-04 19:37:24 +00:00
for (i = 0; i < db->count; i++)
{
printf("Key: %s, Value: %s\n", db->hashes[i].key, db->hashes[i].value);
}
free(db);
return 0;
}