65 lines
1.7 KiB
C
65 lines
1.7 KiB
C
/**
|
|
* Copyright (c) 2025 Dominic Masters
|
|
*
|
|
* This software is released under the MIT License.
|
|
* https://opensource.org/licenses/MIT
|
|
*/
|
|
|
|
#include "consolevar.h"
|
|
#include "assert/assert.h"
|
|
#include "util/memory.h"
|
|
#include "util/string.h"
|
|
|
|
void consoleVarInit(
|
|
consolevar_t *var,
|
|
const char_t *name,
|
|
const char_t *value
|
|
) {
|
|
assertNotNull(var, "var must not be NULL");
|
|
assertNotNull(name, "name must not be NULL");
|
|
assertNotNull(value, "value must not be NULL");
|
|
|
|
assertStrLenMin(name, 1, "name must not be empty");
|
|
assertStrLenMax(name, CONSOLE_VAR_NAME_MAX, "name is too long");
|
|
assertStrLenMax(value, CONSOLE_VAR_VALUE_MAX, "value is too long");
|
|
|
|
memoryZero(var, sizeof(consolevar_t));
|
|
stringCopy(var->name, name, CONSOLE_VAR_NAME_MAX);
|
|
stringCopy(var->value, value, CONSOLE_VAR_VALUE_MAX);
|
|
}
|
|
|
|
void consoleVarInitListener(
|
|
consolevar_t *var,
|
|
const char_t *name,
|
|
const char_t *value,
|
|
consolevarchanged_t event
|
|
) {
|
|
consoleVarInit(var, name, value);
|
|
if(event) consoleVarListen(var, event);
|
|
}
|
|
|
|
void consoleVarSetValue(consolevar_t *var, const char_t *value) {
|
|
assertNotNull(var, "var must not be NULL");
|
|
assertNotNull(value, "value must not be NULL");
|
|
assertStrLenMax(value, CONSOLE_VAR_VALUE_MAX, "value is too long");
|
|
|
|
stringCopy(var->value, value, CONSOLE_VAR_VALUE_MAX);
|
|
|
|
uint8_t i = 0;
|
|
while (i < var->eventCount) {
|
|
var->events[i](var);
|
|
i++;
|
|
}
|
|
}
|
|
|
|
void consoleVarListen(consolevar_t *var, consolevarchanged_t event) {
|
|
assertNotNull(var, "var must not be NULL");
|
|
assertNotNull(event, "event must not be NULL");
|
|
assertTrue(
|
|
var->eventCount < CONSOLE_VAR_EVENTS_MAX,
|
|
"Event count is too high"
|
|
);
|
|
var->events[var->eventCount++] = event;
|
|
}
|
|
|