From e21d948a1710e5b83bad3c0d4b21da5b3b7b1b37 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Fri, 13 Oct 2006 13:36:23 +0000 Subject: [PATCH] * Moved I/O functions into a separate module. --- src/Makefile.am | 1 + src/figlet.c | 19 ++++++++++--------- src/io.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++ src/io.h | 26 +++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 9 deletions(-) create mode 100644 src/io.c create mode 100644 src/io.h diff --git a/src/Makefile.am b/src/Makefile.am index 86cf8b7..8555582 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,6 +2,7 @@ bin_PROGRAMS = toilet toilet_SOURCES = main.c \ + io.c io.h \ render.c render.h \ filters.c filter.h \ figlet.c figlet.h diff --git a/src/figlet.c b/src/figlet.c index 6aa5af1..e945713 100644 --- a/src/figlet.c +++ b/src/figlet.c @@ -26,6 +26,7 @@ #include #include "toilet.h" +#include "io.h" #include "figlet.h" #define STD_GLYPHS (127 - 32) @@ -122,18 +123,18 @@ static int open_font(context_t *cx) char buf[2048]; char hardblank[10]; cucul_buffer_t *b; - FILE *f; + TOIFILE *f; unsigned int i, j, size, comment_lines; /* Open font: try .tlf, then .flf */ snprintf(path, 2047, "%s/%s.tlf", cx->dir, cx->font); path[2047] = '\0'; - f = fopen(path, "r"); + f = toiopen(path, "r"); if(!f) { snprintf(path, 2047, "%s/%s.flf", cx->dir, cx->font); path[2047] = '\0'; - f = fopen(path, "r"); + f = toiopen(path, "r"); if(!f) { fprintf(stderr, "font `%s' not found\n", path); @@ -145,14 +146,14 @@ static int open_font(context_t *cx) cx->print_direction = 0; cx->full_layout = 0; cx->codetag_count = 0; - fgets(buf, 2048, f); + toigets(buf, 2048, f); if(sscanf(buf, "%*[ft]lf2a%6s %u %u %u %i %u %u %u %u\n", hardblank, &cx->height, &cx->baseline, &cx->max_length, &cx->old_layout, &comment_lines, &cx->print_direction, &cx->full_layout, &cx->codetag_count) < 6) { fprintf(stderr, "font `%s' has invalid header\n", path); - fclose(f); + toiclose(f); return -1; } @@ -160,7 +161,7 @@ static int open_font(context_t *cx) /* Skip comment lines */ for(i = 0; i < comment_lines; i++) - fgets(buf, 2048, f); + toigets(buf, 2048, f); /* Read mandatory characters (32-127, 196, 214, 220, 228, 246, 252, 223) * then read additional characters. */ @@ -184,7 +185,7 @@ static int open_font(context_t *cx) } else { - if(fgets(buf, 2048, f) == NULL) + if(toigets(buf, 2048, f) == NULL) break; if(!buf[0]) @@ -209,12 +210,12 @@ static int open_font(context_t *cx) if(i + 2048 >= size) data = realloc(data, size += 2048); - fgets(data + i, 2048, f); + toigets(data + i, 2048, f); i = (uintptr_t)strchr(data + i, 0) - (uintptr_t)data; } } - fclose(f); + toiclose(f); if(cx->glyphs < EXT_GLYPHS) { diff --git a/src/io.c b/src/io.c new file mode 100644 index 0000000..b00f796 --- /dev/null +++ b/src/io.c @@ -0,0 +1,50 @@ +/* + * TOIlet The Other Implementation’s letters + * Copyright (c) 2006 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the Do What The Fuck You Want To + * Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * This file contains functions for compressed file I/O. + */ + +#include "config.h" + +#include +#include + +#include "io.h" + +TOIFILE *toiopen(const char *path, const char *mode) +{ + TOIFILE *toif; + FILE *f = fopen(path, mode); + + if(!f) + return NULL; + + toif = malloc(sizeof(*toif)); + toif->f = f; + + return toif; +} + +int toiclose(TOIFILE *toif) +{ + FILE *f = toif->f; + free(toif); + return fclose(f); +} + +char *toigets(char *s, int size, TOIFILE *toif) +{ + return fgets(s, size, toif->f); +} + diff --git a/src/io.h b/src/io.h new file mode 100644 index 0000000..e72ebc8 --- /dev/null +++ b/src/io.h @@ -0,0 +1,26 @@ +/* + * TOIlet The Other Implementation’s letters + * Copyright (c) 2006 Sam Hocevar + * All Rights Reserved + * + * $Id$ + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the Do What The Fuck You Want To + * Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. + */ + +/* + * This header defines functions for compressed file I/O. + */ +typedef struct toifile +{ + FILE *f; +} +TOIFILE; + +TOIFILE *toiopen(const char *, const char *); +int toiclose(TOIFILE *); +char *toigets(char *, int, TOIFILE *); +