]> git.ipfire.org Git - people/ms/libloc.git/blobdiff - src/libloc.c
stringpool: Make them initializable right from the file
[people/ms/libloc.git] / src / libloc.c
index fe44b3c159d5ab92cf5496c30f8b9ab0ad971ef7..667fd7cf2f35466a4b333d4a55608711ce7678e9 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <loc/libloc.h>
 #include "libloc-private.h"
+#include "database.h"
 
 struct loc_ctx {
        int refcount;
@@ -32,6 +33,8 @@ struct loc_ctx {
                int priority, const char *file, int line, const char *fn,
                const char *format, va_list args);
        int log_priority;
+
+       struct loc_database* db;
 };
 
 void loc_log(struct loc_ctx* ctx,
@@ -80,6 +83,8 @@ LOC_EXPORT int loc_new(struct loc_ctx** ctx) {
        c->log_fn = log_stderr;
        c->log_priority = LOG_ERR;
 
+       c->db = NULL;
+
        const char* env = secure_getenv("LOC_LOG");
        if (env)
                loc_set_log_priority(c, log_priority(env));
@@ -107,6 +112,10 @@ LOC_EXPORT struct loc_ctx* loc_unref(struct loc_ctx* ctx) {
        if (--ctx->refcount > 0)
                return NULL;
 
+       // Release any loaded databases
+       if (ctx->db)
+               loc_database_unref(ctx->db);
+
        INFO(ctx, "context %p released\n", ctx);
        free(ctx);
 
@@ -127,3 +136,23 @@ LOC_EXPORT int loc_get_log_priority(struct loc_ctx* ctx) {
 LOC_EXPORT void loc_set_log_priority(struct loc_ctx* ctx, int priority) {
        ctx->log_priority = priority;
 }
+
+LOC_EXPORT int loc_load(struct loc_ctx* ctx, const char* path) {
+       FILE* f = fopen(path, "r");
+       if (!f)
+               return -errno;
+
+       // Release any previously openend database
+       if (ctx->db)
+               loc_database_unref(ctx->db);
+
+       // Open the new database
+       int r = loc_database_new(ctx, &ctx->db, f);
+       if (r)
+               return r;
+
+       // Close the file
+       fclose(f);
+
+       return 0;
+}