blist/src/libklist/util.c

173 lines
No EOL
4 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include "util.h"
#include <string.h>
#include "models.h"
#include "sql.h"
/*
* command parsing
*/
klist *klist_init(char *db) {
klist *ctx = malloc(sizeof(klist));
ctx->stmts = malloc(_KLIST_SQL_COUNT * sizeof(sqlite3_stmt *));
ctx->cmd_ctx = NULL;
ctx->error = 0;
ctx->log_level = KLIST_LOG_WARNING;
ctx->log_target = stderr;
klist_sql_prepare(ctx, db);
return ctx;
}
void klist_deinit(klist *ctx) {
int i = 0;
for (; i < _KLIST_SQL_COUNT; i++)
sqlite3_finalize(ctx->stmts[i]);
sqlite3_close(ctx->db);
// if (ctx->cmd_ctx) free(ctx->cmd_ctx);
free(ctx->stmts);
free(ctx);
ctx = NULL;
}
klist_user_context *klist_user_context_init() {
klist_user_context *ctx = malloc(sizeof(klist_user_context));
ctx->cmd = -1;
return ctx;
}
void klist_user_context_deinit(klist_user_context *ctx) {
free(ctx);
ctx = NULL;
}
klist_list_context *klist_list_context_init() {
klist_list_context *ctx = malloc(sizeof(klist_list_context));
ctx->cmd = -1;
ctx->name = NULL;
ctx->desc = NULL;
ctx->stages = NULL;
ctx->stages_len = 0;
ctx->preset = NULL;
return ctx;
}
void klist_list_context_deinit(klist_list_context *ctx) {
if (ctx->name)
free(ctx->name);
if (ctx->desc)
free(ctx->desc);
if (ctx->preset)
free(ctx->preset);
if (ctx->stages) {
int i = 0;
for (; i < ctx->stages_len; i++)
free(ctx->stages[i]);
free(ctx->stages);
}
free(ctx);
ctx = NULL;
}
klist_task_context *klist_task_context_init(klist *list) {
klist_task_context *ctx = malloc(sizeof(klist_task_context));
ctx->cmd = -1;
ctx->name = NULL;
ctx->desc = NULL;
ctx->list = NULL;
ctx->stage = NULL;
return ctx;
}
void klist_task_context_deinit(klist_task_context *ctx) {
if (ctx->name)
free(ctx->name);
if (ctx->list)
free(ctx->list);
if (ctx->stage)
free(ctx->stage);
free(ctx);
ctx = NULL;
}
/*
* sql preparations
*/
void klist_sql_prepare(klist *ctx, char *db) {
if (!db)
klist_print(
ctx, KLIST_LOG_WARNING,
"Database will be in-memory only, changes are not persistent!\n");
if (sqlite3_open(db ? db : ":memory:", &ctx->db) != SQLITE_OK) {
klist_print(ctx, KLIST_LOG_ERROR, "Can't open database: %s\n",
sqlite3_errmsg(ctx->db));
return;
}
char *errmsg = NULL;
sqlite3_exec(ctx->db, klist_sql_get(INIT), NULL, NULL, &errmsg);
if (errmsg) {
klist_print(ctx, KLIST_LOG_ERROR,
"Database initialization failed (%s), expect issues.\n",
sqlite3_errmsg(ctx->db));
sqlite3_free(errmsg);
}
int i = 0;
for (; i < _KLIST_SQL_COUNT; i++)
if (sqlite3_prepare(ctx->db, klist_sql_get(i), -1, &ctx->stmts[i], NULL) !=
SQLITE_OK)
klist_print(ctx, KLIST_LOG_WARNING,
"sqlite3_prepare for '%s' failed: %s\n", klist_sql_get(i),
sqlite3_errmsg(ctx->db));
}
void klist_sql_seed(klist *ctx) {}
/*
* assuring things
*/
bool klist_assure_user(const klist *ctx, const __uid_t id, const char *name) {
klist_user *user = klist_user_get_by_local(ctx, id);
bool created = false;
if (!user) {
user = klist_user_init();
user->name = malloc((strlen(name) + 1) * sizeof(char));
strcpy((char *)user->name, name);
user->local_id = id;
klist_user_save(ctx, user);
created = true;
}
klist_user_deinit(user);
return created;
}
/*
* printing methods
*/
void klist_print(const klist *ctx, KLIST_LOG_LEVEL log_level,
const char *format, ...) {
if (ctx->log_level >= log_level) {
switch (log_level) {
case KLIST_LOG_DEBUG:
fprintf(ctx->log_target, "[\x1B[34mDBG\x1B[0m] ");
break;
case KLIST_LOG_INFO:
fprintf(ctx->log_target, "[\x1B[36mINF\x1B[0m] ");
break;
case KLIST_LOG_WARNING:
fprintf(ctx->log_target, "[\x1B[33mWRN\x1B[0m] ");
break;
case KLIST_LOG_ERROR:
fprintf(ctx->log_target, "[\x1B[31mERR\x1B[0m] ");
break;
}
va_list args;
va_start(args, format);
vprintf(format, args);
va_end(args);
}
}