From 0b9687372282752f586af875f688d634648a5fda Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Thu, 18 Jul 2019 02:28:03 -0400 Subject: text_new(): Read a file stream text_render() must now consume the CSI "[" in the if condition, as text_new() does. --- diff --git a/src/main.c b/src/main.c index d16197a..86c2f95 100644 --- a/src/main.c +++ b/src/main.c @@ -19,7 +19,10 @@ * along with fbcon2png. If not, see . */ +#include + #include "font.h" +#include "i18n.h" #include "image.h" #include "text.h" @@ -27,6 +30,7 @@ int main(int argc, char **argv) { struct font *font; + FILE *fp; struct text *text; struct image *image; @@ -35,7 +39,20 @@ main(int argc, char **argv) } font = font_find(argv[1]); - text = text_new(argv[2]); + if (strcmp(argv[2], "-") != 0) { + fp = fopen(argv[2], "r"); + if (!fp) { + printf(_("Error: Cannot open \"%s\" for reading\n"), + argv[2]); + return 1; + } + } else { + fp = stdin; + } + text = text_new(fp); + if (fp != stdin) { + fclose(fp); + } text_set_width(text, 80); text_set_height(text, 25); image = image_new(argv[3]); diff --git a/src/text.c b/src/text.c index dea3259..dda6de1 100644 --- a/src/text.c +++ b/src/text.c @@ -21,6 +21,7 @@ #include "text.h" +#include #include #include @@ -32,19 +33,39 @@ #define MAX(a, b) (((a) > (b)) ? (a) : (b)) struct text { - const char *string; - size_t width; - size_t height; + size_t size; + size_t read; + char *string; + size_t width; + size_t height; }; +static char +_getc(struct text *text, FILE *stream, char *c) +{ + int i_c; + + if (text->read == text->size) { + text->size += 8192; + text->string = realloc(text->string, text->size); + } + + i_c = fgetc(stream); + if (i_c == EOF) { + i_c = 0; + } + return text->string[text->read++] = *c = i_c; +} + struct text * -text_new(const char *string) +text_new(FILE *stream) { struct text *text; size_t width; int eol; + char c; - if (!string) { + if (!stream) { return NULL; } text = calloc(1, sizeof(*text)); @@ -52,28 +73,27 @@ text_new(const char *string) return NULL; } - text->string = string; - width = 0; eol = 0; - while (string && *string) { - if (*string == 0x1B && *(string + 1) == '[') { /* CSI */ - string += 2; - for (; *string >= 0x30 && *string <= 0x3F; ++string); - for (; *string >= 0x20 && *string <= 0x2F; ++string); - if (*string >= 0x40 && *string <= 0x7E) { - ++string; + _getc(text, stream, &c); + while (c) { + if (c == 0x1B && _getc(text, stream, &c) == '[') { /* CSI */ + _getc(text, stream, &c); + for (; c >= 0x30 && c <= 0x3F; _getc(text, stream, &c)); + for (; c >= 0x20 && c <= 0x2F; _getc(text, stream, &c)); + if (c >= 0x40 && c <= 0x7E) { + _getc(text, stream, &c); } eol = 0; - } else if (*string == '\n') { + } else if (c == '\n') { text->width = MAX(text->width, width); width = 0; ++text->height; - ++string; + _getc(text, stream, &c); eol = 1; } else { ++width; - ++string; + _getc(text, stream, &c); eol = 0; } } @@ -129,8 +149,8 @@ text_render(struct text *text, struct font *font, png_bytepp rows) row = 0; col = 0; while (string && *string) { - if (*string == 0x1B && *(string + 1) == '[') { /* CSI */ - string += 2; + if (*string == 0x1B && *(++string) == '[') { /* CSI */ + ++string; parameter = string; for (; *string >= 0x30 && *string <= 0x3F; ++string); for (; *string >= 0x20 && *string <= 0x2F; ++string); @@ -171,6 +191,7 @@ text_destroy(struct text **text_p) text = *text_p; + free(text->string); free(text); return text = NULL; } diff --git a/src/text.h b/src/text.h index 9c581d6..8ee0245 100644 --- a/src/text.h +++ b/src/text.h @@ -22,6 +22,8 @@ #ifndef TEXT_H #define TEXT_H +#include + #include #include "font.h" @@ -29,7 +31,7 @@ struct text; struct text * -text_new(const char *string); +text_new(FILE *stream); size_t text_get_width(struct text *text) __attribute__((pure)); -- cgit v0.9.1