Skip to content

KvConfig

KvConfig

Description

Key-value configuration map.

Duplicate keys are resolved with last-write-wins semantics during parsing.

Usage example (from documentation)

  KvConfig cfg = KvConfigInit();
  Str text = StrInitFromZstr("host = localhost\nport = 8080\n");
  StrIter si = KvConfigParse(StrIterFromStr(text), &cfg);

  Str *host_ptr = KvConfigGetPtr(&cfg, "host");
  Str  host     = KvConfigGet(&cfg, "host");
  i64  port = 0;
  KvConfigGetI64(&cfg, "port", &port);
  StrDeinit(&host);

Usage example (Cross-references)

Usage examples (Cross-references)
    #include <Misra/Parsers/KvConfig.h>
    #include <Misra/Std/Container/Map/Private.h>
    #include <Misra/Std/Memory.h>
    }
    
    StrIter KvConfigParse(StrIter si, KvConfig *cfg) {
        StrIter saved_si = si;
    
        if (!cfg) {
            LOG_ERROR("Expected valid KvConfig object");
            return si;
        }
    }
    
    Str *KvConfigGetPtr(KvConfig *cfg, const char *key) {
        Str  lookup = {0};
        Str *value  = NULL;
    }
    
    Str KvConfigGet(KvConfig *cfg, const char *key) {
        Str *value = KvConfigGetPtr(cfg, key);
    }
    
    bool KvConfigContains(KvConfig *cfg, const char *key) {
        return KvConfigGetPtr(cfg, key) != NULL;
    }
    }
    
    bool KvConfigGetBool(KvConfig *cfg, const char *key, bool *value) {
        Str *str = KvConfigGetPtr(cfg, key);
    }
    
    bool KvConfigGetI64(KvConfig *cfg, const char *key, i64 *value) {
        Str *str = KvConfigGetPtr(cfg, key);
    }
    
    bool KvConfigGetF64(KvConfig *cfg, const char *key, f64 *value) {
        Str *str = KvConfigGetPtr(cfg, key);
    #include <Misra/Parsers/KvConfig.h>
    #include <Misra/Std/Io.h>
    
    static bool test_kvconfig_basic_parse(void) {
        KvConfig cfg = KvConfigInit();
        Str      src = StrInitFromZstr(
            "host = localhost\n"
    
    static bool test_kvconfig_comments_quotes_and_duplicates(void) {
        KvConfig cfg = KvConfigInit();
        Str      src = StrInitFromZstr(
            "# comment line\n"
    
    static bool test_kvconfig_get_returns_copy(void) {
        KvConfig cfg         = KvConfigInit();
        Str      src         = StrInitFromZstr("host = localhost\n");
        StrIter  input       = StrIterFromStr(src);
    
    static bool test_kvconfig_numeric_and_bool_accessors(void) {
        KvConfig cfg = KvConfigInit();
        Str      src = StrInitFromZstr(
            "workers = 16\n"
    
    static bool test_kvconfig_invalid_line_fails(void) {
        KvConfig cfg = KvConfigInit();
        Str      src = StrInitFromZstr(
            "valid = yes\n"
        };
    
        WriteFmt("[INFO] Starting KvConfig.Parse tests\n\n");
        return run_test_suite(tests, (int)(sizeof(tests) / sizeof(tests[0])), NULL, 0, "KvConfig.Parse");
    }
    
        WriteFmt("[INFO] Starting KvConfig.Parse tests\n\n");
        return run_test_suite(tests, (int)(sizeof(tests) / sizeof(tests[0])), NULL, 0, "KvConfig.Parse");
    }
    
    #include <Misra/Parsers/JSON.h>
    #include <Misra/Parsers/KvConfig.h>
    #include <Misra/Std.h>
    #include <Misra/Sys.h>
    /// FAILURE : Returns original iterator on first invalid line.
    ///
    StrIter KvConfigParse(StrIter si, KvConfig *cfg);
    
    ///
    /// FAILURE : Empty `Str` if key does not exist.
    ///
    Str KvConfigGet(KvConfig *cfg, const char *key);
    
    ///
    /// FAILURE : `NULL` if key does not exist.
    ///
    Str *KvConfigGetPtr(KvConfig *cfg, const char *key);
    
    ///
    /// FAILURE : `false`
    ///
    bool KvConfigContains(KvConfig *cfg, const char *key);
    
    ///
    /// FAILURE : `false`
    ///
    bool KvConfigGetBool(KvConfig *cfg, const char *key, bool *value);
    
    ///
    /// FAILURE : `false`
    ///
    bool KvConfigGetI64(KvConfig *cfg, const char *key, i64 *value);
    
    ///
    /// FAILURE : `false`
    ///
    bool KvConfigGetF64(KvConfig *cfg, const char *key, f64 *value);
    
    #endif // MISRA_PARSERS_KVCONFIG_H
Last updated on