src/font.c: finish implementations

current
Síle Ekaterin Liszka 2023-11-27 15:21:09 -08:00
parent e549ce7332
commit 5c83c9e89e
2 changed files with 29 additions and 3 deletions

View File

@ -20,9 +20,9 @@ font_t font_create(char *name, char *filename) {
toml_table_t *table = toml_parse_file(fp); toml_table_t *table = toml_parse_file(fp);
fclose(fp); fclose(fp);
#define TOML_GET_KEY((var, key, func)) toml_datum_t *(var) = func(table, key);\ #define TOML_GET_KEY((var, key, func)) toml_datum_t *(var) = func(table, (key));\
if (!var.ok) {\ if (!(var).ok) {\
error("key '" key "' missing from", filename); \ error("key '" (key) "' missing from", filename); \
} }
TOML_GET_KEY(name, "name", toml_string_in) TOML_GET_KEY(name, "name", toml_string_in)
@ -42,10 +42,17 @@ font_t font_create(char *name, char *filename) {
font.width = width; font.width = width;
font.height = height; font.height = height;
font.map_entries = map_create(mapping, *map); font.map_entries = map_create(mapping, *map);
font.map = map;
return font; return font;
} }
void font_destroy(font_t font) {
map_destroy(font.map);
free(font.name);
free(font.filename);
}
unsigned int map_create(toml_table_t *table, map_t **map) { unsigned int map_create(toml_table_t *table, map_t **map) {
map_t *node; map_t *node;
map_t *next; map_t *next;
@ -82,4 +89,18 @@ unsigned int map_create(toml_table_t *table, map_t **map) {
node->next = next; node->next = next;
node = next; node = next;
} }
node->next = NULL;
*map = root;
return nodes;
}
void map_destroy(map_t *map) {
map_t *node = map;
map_t *next;
while ((next = node->next) != NULL) {
free(node);
node = next;
}
} }

View File

@ -15,3 +15,8 @@ typedef struct {
unsigned int map_entries; unsigned int map_entries;
map_t *map; map_t *map;
} font_t; } font_t;
font_t font_create(char *name, char *filename);
void font_destroy(font_t font);
unsigned int map_create(toml_table_t *table, map_t **map);
void map_destroy(map_t *map);