14#if DETWS_ENABLE_CONFIG_IO
23constexpr size_t VAL_MAX = 128;
24constexpr size_t KEY_MAX = 16;
27bool field_type(
const DetwsCfgField *fields,
size_t n,
const char *key, DetwsCfgType *out)
29 for (
size_t i = 0; i < n; i++)
30 if (fields[i].key && strcmp(fields[i].key, key) == 0)
32 *out = fields[i].type;
39bool append_kv(
char *out,
size_t cap,
size_t *pos,
const char *key,
const char *val)
41 size_t kn = strnlen(key, cap + 1), vn = strnlen(val, cap + 1);
42 size_t need = kn + 1 + vn + 1;
43 if (*pos + need >= cap)
45 memcpy(out + *pos, key, kn);
48 memcpy(out + *pos, val, vn);
56int detws_config_export(
const char *ns,
const DetwsCfgField *fields,
size_t n,
char *out,
size_t cap)
61 if (!fields || !detws_config_begin(ns))
65 for (
size_t i = 0; i < n; i++)
68 if (fields[i].type == DetwsCfgType::DETWS_CFG_U32)
69 snprintf(val,
sizeof(val),
"%u", (
unsigned)detws_config_get_u32(fields[i].key, 0));
71 detws_config_get_str(fields[i].key, val,
sizeof(val),
"");
73 if (!append_kv(out, cap, &pos, fields[i].key, val))
84static bool config_apply_field(
const DetwsCfgField *fields,
size_t n,
const char *key,
const char *val)
87 if (!field_type(fields, n, key, &t))
89 if (t == DetwsCfgType::DETWS_CFG_U32)
90 return detws_config_set_u32(key, (uint32_t)
det_strtoul(val,
nullptr));
91 if (t == DetwsCfgType::DETWS_CFG_STR)
92 return detws_config_set_str(key, val);
96int detws_config_import(
const char *ns,
const DetwsCfgField *fields,
size_t n,
const char *text,
size_t len)
98 if (!text || !fields || !detws_config_begin(ns))
107 while (eol < len && text[eol] !=
'\n')
112 while (eq < eol && text[eq] !=
'=')
120 size_t klen = eq - i;
121 size_t vlen = eol - (eq + 1);
122 if (klen > 0 && klen < KEY_MAX && vlen < VAL_MAX)
126 memcpy(key, text + i, klen);
128 memcpy(val, text + eq + 1, vlen);
130 if (config_apply_field(fields, n, key, val))
Schema-driven config export / restore (DETWS_ENABLE_CONFIG_IO).
Typed NVS configuration store (DETWS_ENABLE_CONFIG_STORE).
Tiny no-stdlib base-10 number parsers (strtol/strtoul/strtof replacements).
unsigned long det_strtoul(const char *s, const char **end)
Parse a base-10 unsigned long; sets end past the digits (or to s).