diff --git a/meson.build b/meson.build index b6b99db..168f37d 100644 --- a/meson.build +++ b/meson.build @@ -10,7 +10,7 @@ libtoml_dep = dependency( exe = executable( 'smeargle', - 'src/main.c', + ['src/main.c', 'src/font.c'], dependencies: libtoml_dep, install: true ) \ No newline at end of file diff --git a/src/font.c b/src/font.c index 6db97f2..d0c887d 100644 --- a/src/font.c +++ b/src/font.c @@ -4,25 +4,29 @@ #include "util.h" #include "font.h" -font_t font_create(char *name, char *filename) { +font_t font_create(char *font_name, char *filename) { FILE *fp; font_t font; + char errbuf[200]; + map_t *map; - printf("Processing %s...\n", name); + printf("Processing %s...\n", font_name); fp = fopen(filename, "r"); if (!fp) { - char errbuf[200]; - snprintf(errbuf, 200, "can't open %s: ", filename); - error(errbuf, strerror(errno)); + char str[200]; + snprintf(str, 200, "can't open %s: ", filename); + error(str, strerror(errno)); } - toml_table_t *table = toml_parse_file(fp); + toml_table_t *table = toml_parse_file(fp, errbuf, 200); 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) {\ - error("key '" (key) "' missing from", filename); \ + char str[200];\ + snprintf(str, 200, "key '%s' missing from", (key)); \ + error(str, filename); \ } TOML_GET_KEY(name, "name", toml_string_in) @@ -31,17 +35,14 @@ font_t font_create(char *name, char *filename) { TOML_GET_KEY(width, "width", toml_int_in) TOML_GET_KEY(height, "height", toml_int_in) #undef TOML_GET_KEY - toml_table_t *mapping = toml_table_in(table, "map") - if (!mapping.ok) { - error("map missing for font ", font); - } + toml_table_t *mapping = toml_table_in(table, "map"); font.name = name.u.s; - font.filename = image_filename.u.s; - font.bits_per_pixel = bpp; - font.width = width; - font.height = height; - font.map_entries = map_create(mapping, *map); + font.toml_filename = image_filename.u.s; + font.bits_per_pixel = bpp.u.i; + font.width = width.u.i; + font.height = height.u.i; + font.map_entries = map_create(mapping, &map); font.map = map; return font; @@ -50,7 +51,7 @@ font_t font_create(char *name, char *filename) { void font_destroy(font_t font) { map_destroy(font.map); free(font.name); - free(font.filename); + free(font.toml_filename); } unsigned int map_create(toml_table_t *table, map_t **map) { @@ -62,15 +63,15 @@ unsigned int map_create(toml_table_t *table, map_t **map) { toml_datum_t spcidx = toml_int_at(space, 0); toml_datum_t spcwid = toml_int_at(space, 1); if (!spcidx.ok) { - error("missing or inappropriate index for glyph: ", key); + error("missing or inappropriate index for glyph: ' '", ""); } if (!spcwid.ok) { - error("missing or inappropriate index for glyph: ", key); + error("missing or inappropriate index for glyph: ' '", ""); } node = root; for (nodes = 1; ; nodes++) { - char *key = toml_key_in(table, nodes); + const char *key = toml_key_in(table, nodes); if (!key) break; toml_array_t *val = toml_array_in(table, key); toml_datum_t index = toml_int_at(val, 0); diff --git a/src/font.h b/src/font.h index 7ebd664..0b6c39f 100644 --- a/src/font.h +++ b/src/font.h @@ -1,7 +1,7 @@ typedef struct map_t { - char *glyph; - unsigned int pos; + const char *glyph; + unsigned int index; unsigned int width; struct map_t *next; } map_t; diff --git a/src/main.c b/src/main.c index 6c0125d..829ed9d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,6 +6,8 @@ #include +#include "util.h" + #define ERRBUFSZ 200 int main(int argc, char **argv) { diff --git a/src/util.h b/src/util.h index 9d554ca..729df3a 100644 --- a/src/util.h +++ b/src/util.h @@ -1,7 +1,11 @@ +#include +#include +#include #include static void error(const char *msg, const char *msg1) { fprintf(stderr, "Error: %s%s\n", msg, msg1 ? msg1 : ""); exit(1); } +