summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2019-07-18 02:28:03 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2019-07-18 02:29:53 (EDT)
commit0b9687372282752f586af875f688d634648a5fda (patch)
tree5cf120d3040c19f38072d9de2d6b3d7f886d8a69
parentcaa24987c48bcdae57d08f70b0c5ee9089c13e54 (diff)
text_new(): Read a file stream
text_render() must now consume the CSI "[" in the if condition, as text_new() does.
-rw-r--r--src/main.c19
-rw-r--r--src/text.c59
-rw-r--r--src/text.h4
3 files changed, 61 insertions, 21 deletions
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 <http://www.gnu.org/licenses/>.
*/
+#include <stdio.h>
+
#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 <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -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 <stdio.h>
+
#include <png.h>
#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));