write conf scripts

This commit is contained in:
2025-12-15 18:52:47 +01:00
parent bc0743a72b
commit 9e8f5958c7
2 changed files with 47 additions and 0 deletions

35
src/io/conf.c Normal file
View File

@@ -0,0 +1,35 @@
#include "conf.h"
#include <string.h>
#include "../types.h"
#include "../util/atrb.h"
/* Matches s1 with s2, returns a pointer to s1 where the match stopped. */
static const char *strmat(const char *s1, const char *s2) PURE NONNULL((1, 2));
static const char *strmat(const char *s1, const char *s2)
{
while ((*s1 == *s2) & !!*s1)
s1++, s2++;
return s1;
}
int conf_getkeyval(const char *restrict buf, const char *const restrict *restrict keys, int klen, const char *restrict *restrict out)
{
const char *tmp = NULL;
ASSUME((klen > 0));
int i = 0;
for (; i < klen && !tmp; i++) {
tmp = strmat(buf, keys[i]);
tmp = keys[i][buf - tmp] ? tmp : 0;
}
if (!tmp || *tmp != '=')
return -1;
*out = tmp + 1;
return i;
}
int conf_val(int);

12
src/io/conf.h Normal file
View File

@@ -0,0 +1,12 @@
#pragma once
#include "../types.h"
#include "../util/atrb.h"
/* Gets the key and value, if present. Writes the pointer for the value to `out`.
* Returns the key index, or <0 upon failure. */
int conf_getkeyval(const char *restrict buf, const char *const restrict *restrict keys, int klen,
const char *restrict *restrict out) NONNULL((1, 2, 4));
/* Processes the value of `type` in `val`. Outputs to `out`.
* Returns non-zero on failure. */
int conf_procval(u8 type, const char *restrict val, void *restrict out) NONNULL((2, 3));