* Moved I/O functions into a separate module.

This commit is contained in:
Sam Hocevar 2006-10-13 13:36:23 +00:00 committed by sam
parent 7629afd70b
commit e21d948a17
4 changed files with 87 additions and 9 deletions

View file

@ -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

View file

@ -26,6 +26,7 @@
#include <cucul.h>
#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)
{

50
src/io.c Normal file
View file

@ -0,0 +1,50 @@
/*
* TOIlet The Other Implementations letters
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* 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 <stdio.h>
#include <stdlib.h>
#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);
}

26
src/io.h Normal file
View file

@ -0,0 +1,26 @@
/*
* TOIlet The Other Implementations letters
* Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
* 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 *);