feat(vars): vars
This commit is contained in:
parent
b7b6a6a8d5
commit
d377b80803
2 changed files with 63 additions and 0 deletions
32
src/utils/vars/vars.c
Normal file
32
src/utils/vars/vars.c
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "vars.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#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));
|
||||
}
|
||||
31
src/utils/vars/vars.h
Normal file
31
src/utils/vars/vars.h
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
#ifndef VARS_H
|
||||
#define VARS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#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 */
|
||||
Loading…
Add table
Add a link
Reference in a new issue