From 0c6c3571614b69a925ea832adecd87a7673d4602 Mon Sep 17 00:00:00 2001 From: "william.valenduc" Date: Sat, 17 Jan 2026 22:46:10 +0000 Subject: [PATCH] feat(vars): vars --- src/utils/vars/vars.c | 32 ++++++++++++++++++++++++++++++++ src/utils/vars/vars.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/utils/vars/vars.c create mode 100644 src/utils/vars/vars.h diff --git a/src/utils/vars/vars.c b/src/utils/vars/vars.c new file mode 100644 index 0000000..70ab328 --- /dev/null +++ b/src/utils/vars/vars.c @@ -0,0 +1,32 @@ +#define _POSIX_C_SOURCE 200809L +#include "vars.h" + +#include +#include +#include + +#include "../hash_map/hash_map.h" + +#define VARS_INITIAL_SIZE 16 + +struct hash_map *vars_init(void) +{ + return hash_map_init(VARS_INITIAL_SIZE); +} + +char *get_var(const struct hash_map *vars, const char *key) +{ + return (char *)hash_map_get(vars, key); +} + +bool set_var(struct hash_map *vars, const char *key, const char *value) +{ + if (key == NULL || value == NULL) + return false; + return hash_map_insert(vars, key, (void *)value, NULL); +} + +bool set_var_copy(struct hash_map *vars, const char *key, const char *value) +{ + return set_var(vars, strdup(key), strdup(value)); +} diff --git a/src/utils/vars/vars.h b/src/utils/vars/vars.h new file mode 100644 index 0000000..97db704 --- /dev/null +++ b/src/utils/vars/vars.h @@ -0,0 +1,31 @@ +#ifndef VARS_H +#define VARS_H + +#include + +#include "../hash_map/hash_map.h" + +/** + * Initialize a new variables hash map. + */ +struct hash_map *vars_init(void); + +/** + * Get the value of a variable, NULL if not found. + */ +char *get_var(const struct hash_map *vars, const char *key); + +/** + * Set the value of a variable. Key and value ownership are transferred to + * the hash_map and need to be on the heap. Returns true on success, false on + * failure. + */ +bool set_var(struct hash_map *vars, const char *key, const char *value); + +/** + * Same as set_var, but makes copies of key and value so you don't have to worry + * about their memory management. Returns true on success, false on failure. + */ +bool set_var_copy(struct hash_map *vars, const char *key, const char *value); + +#endif /* ! VARS_H */