libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit f3f59213a202161c1ac4e10cddc4c404a03ce542
parent 4b75fc2392072fad400f89fb76615f2cdb568807
Author: lash <dev@holbrook.no>
Date:   Sat, 22 Mar 2025 23:54:46 +0000

Add text index for config entries

Diffstat:
Msrc/crypto/gcrypt.c | 24++++++++++++++----------
Msrc/lq/config.c | 36++++++++++++++++++++++++++----------
Msrc/lq/config.h | 8++++++--
Msrc/test/test_config.c | 27+++++++++++++++------------
4 files changed, 61 insertions(+), 34 deletions(-)

diff --git a/src/crypto/gcrypt.c b/src/crypto/gcrypt.c @@ -29,17 +29,19 @@ struct gpg_store { gcry_sexp_t k; char fingerprint[LQ_FP_LEN]; char public_key[LQ_PUBKEY_LEN]; - char path[LQ_PATH_MAX]; char last_signature[LQ_SIG_LEN]; char last_data[LQ_DIGEST_LEN]; }; +static int gpg_cfg_idx_dir; + int lq_crypto_init() { #ifdef RERR rerr_register(RERR_PFX_GPG, "crypto", _rerr); #endif - char *p; - size_t c; + //char *p; + //size_t c; + int r; gpg = lq_zero(sizeof(struct gpg_store)); if (gpg == NULL) { @@ -47,13 +49,15 @@ int lq_crypto_init() { } gpg->passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256); - strcpy(gpg->path, path); - c = strlen(gpg->path); - p = gpg->path+c; - if (*p != '/') { - *p = '/'; - *(p+1) = 0; - } + gpg_cfg_idx_dir = lq_config_register(LQ_TYP_STR, "CRYPTODIR"); + +// strcpy(gpg->path, path); +// c = strlen(gpg->path); +// p = gpg->path+c; +// if (*p != '/') { +// *p = '/'; +// *(p+1) = 0; +// } return ERR_OK; } diff --git a/src/lq/config.c b/src/lq/config.c @@ -4,14 +4,26 @@ #include "lq/mem.h" #include "lq/config.h" -const int lq_config_core_typs[] = { +static const int config_core_typs[] = { + LQ_TYP_STR, + LQ_TYP_STR, + LQ_TYP_STR, LQ_TYP_STR, }; +static const char *config_core_name[] = { + "BASEDIR", + "CONFIGDIR", + "DATADIR", + "CACHEDIR", +}; + +static char **config_idx; + static struct config_t { void *mem; ///< Config data memory. void **members; ///< Member pointer. - enum lq_typ_e *typs; ///< Member type. + enum lq_typ_e *typs; ///< Member type. size_t last; ///< Last registered members index. size_t cap; ///< Bytes allocated for config content. size_t len; ///< Bytes currently used for content. @@ -22,9 +34,9 @@ static int core_register() { int r; for (i = 0; i < LQ_CFG_LAST; i++) { - r = lq_config_register(lq_config_core_typs[i]); - if (r) { - return r; + r = lq_config_register(config_core_typs[i], config_core_name[i]); + if (r == -1) { + return ERR_INIT; } } return ERR_OK; @@ -35,17 +47,18 @@ int lq_config_init() { if (config.mem == NULL) { return ERR_MEM; } - config.members = lq_alloc(LQ_CONFIG_MEMCAP * sizeof(void**)); + config.members = lq_alloc(LQ_CONFIG_MAX * sizeof(void**)); if (config.members == NULL) { lq_free(config.mem); return ERR_MEM; } - config.typs = lq_alloc(LQ_CONFIG_MEMCAP * sizeof(void*)); + config.typs = lq_alloc(LQ_CONFIG_MAX * sizeof(void*)); if (config.typs == NULL) { lq_free(config.members); lq_free(config.mem); return ERR_MEM; } + config_idx = lq_alloc(LQ_CONFIG_MAX * sizeof(char**)); config.last = 0; config.len = 0; config.cap = LQ_CONFIG_MEMCAP; @@ -53,7 +66,7 @@ int lq_config_init() { return core_register(); } -int lq_config_register(enum lq_typ_e typ) { +int lq_config_register(enum lq_typ_e typ, const char *name) { size_t l; switch (typ) { @@ -71,12 +84,14 @@ int lq_config_register(enum lq_typ_e typ) { } if (!l) { - return ERR_INIT; + return -1; } *(config.typs+config.last) = typ; + *(config_idx + config.last) = name; // name must survive, or NULL for no reverse. + l = config.last; config.last++; - return ERR_OK; + return (int)l; } int lq_config_set(int k, void *v) { @@ -126,6 +141,7 @@ int lq_config_get(int k, void **r) { } void lq_config_free() { + lq_free(config_idx); lq_free(config.typs); lq_free(config.members); lq_free(config.mem); diff --git a/src/lq/config.h b/src/lq/config.h @@ -12,13 +12,17 @@ #include "lq/mem.h" enum lq_config_core_e { - LQ_CFG_DATA, + LQ_CFG_DIR_BASE, + LQ_CFG_DIR_CONFIG, + LQ_CFG_DIR_DATA, + LQ_CFG_DIR_CACHE, LQ_CFG_LAST, }; int lq_config_init(); -int lq_config_register(enum lq_typ_e typ); +int lq_config_register(enum lq_typ_e typ, const char *name); +int lq_config_key(const char *name); int lq_config_set(int k, void *v); int lq_config_get(int k, void **r); void lq_config_free(); diff --git a/src/test/test_config.c b/src/test/test_config.c @@ -18,9 +18,9 @@ START_TEST(check_register) { int r; lq_config_init(); - r = lq_config_register(LQ_TYP_STR); + r = lq_config_register(LQ_TYP_STR, NULL); lq_config_free(); - ck_assert_int_eq(r, ERR_OK); + ck_assert_int_ge(r, 0); } END_TEST @@ -28,33 +28,36 @@ START_TEST(check_set_get) { int r; long v; char *p; + int c; + c = LQ_CFG_LAST; lq_config_init(); - r = lq_config_register(LQ_TYP_NUM); - ck_assert_int_eq(r, ERR_OK); - r = lq_config_register(LQ_TYP_STR); - ck_assert_int_eq(r, ERR_OK); + r = lq_config_register(LQ_TYP_NUM, NULL); + ck_assert_int_ge(r, 0); + r = lq_config_register(LQ_TYP_STR, NULL); + ck_assert_int_ge(r, 0); - r = lq_config_set(2, "foobarbaz"); + // set with the index returned from the last register action + r = lq_config_set(r, "foobarbaz"); ck_assert_int_eq(r, ERR_OK); v = 42; - r = lq_config_set(1, &v); + r = lq_config_set(c, &v); ck_assert_int_eq(r, ERR_OK); - r = lq_config_get(1, (void**)&p); + r = lq_config_get(c, (void**)&p); ck_assert_int_eq(r, ERR_OK); v = *((long*)p); ck_assert_int_eq(v, 42); - r = lq_config_get(2, (void**)&p); + r = lq_config_get(c + 1, (void**)&p); ck_assert_int_eq(r, ERR_OK); ck_assert_str_eq(p, "foobarbaz"); - r = lq_config_set(3, &v); + r = lq_config_set(c + 2, &v); ck_assert_int_eq(r, ERR_OVERFLOW); - r = lq_config_get(3, (void**)&p); + r = lq_config_get(c + 2, (void**)&p); ck_assert_int_eq(r, ERR_OVERFLOW); lq_config_free();