site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

commit aa225a30a519cf8b92ebe07347b9a599ac14743c
parent 89bd0f69d98c804cd4e3ebb254324f06029b98a4
Author: Henry Unite <unitehenry@gmail.com>
Date:   Sun, 12 Apr 2026 01:03:53 +0000

feat: string map

Diffstat:
Msrc/strings.c | 44++++++++++++++++++++++++++++++++++++++++++++
Msrc/strings.h | 13+++++++++++++
2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/src/strings.c b/src/strings.c @@ -125,3 +125,47 @@ char *trim_whitespace(const char *str) { return result; } + +StringMap *string_map_create(void) { + StringMap *map = malloc(sizeof(StringMap)); + + if (!map) + return NULL; + + map->head = NULL; + + return map; +} + +void string_map_put(StringMap *map, char *key, char *value) { + StringMapEntry *entry = map->head; + + while (entry) { + if (strcmp(entry->key, key) == 0) { + free(entry->value); + entry->value = strdup(value); + return; + } + entry = entry->next; + } + + StringMapEntry *new_entry = malloc(sizeof(StringMapEntry)); + + new_entry->key = strdup(key); + new_entry->value = strdup(value); + new_entry->next = map->head; + map->head = new_entry; +} + +char *string_map_get(StringMap *map, char *key) { + StringMapEntry *entry = map->head; + + while (entry) { + if (strcmp(entry->key, key) == 0) { + return entry->value; + } + entry = entry->next; + } + + return NULL; +} diff --git a/src/strings.h b/src/strings.h @@ -9,9 +9,22 @@ typedef struct { char **strings; } StringList; +typedef struct StringMapEntry { + char *key; + char *value; + struct StringMapEntry *next; +} StringMapEntry; + +typedef struct { + StringMapEntry *head; +} StringMap; + StringList *init_string_list(size_t size); void string_list_append(StringList *string_list, char *string); char *str_replace(const char *str, const char *old_sub, const char *new_sub); char *trim_whitespace(const char *str); +StringMap *string_map_create(void); +void string_map_put(StringMap *map, char *key, char *value); +char *string_map_get(StringMap *map, char *key); #endif