commit d83c51750e18a1336652fc26f69275315740c8e3
parent 1191a56dee53fba421a2f2ee9eb04ef73ae51c68
Author: Frederic Cambus <fred@statdns.com>
Date: Wed, 23 Sep 2020 09:52:10 +0200
Refactor the Binary loader to use a state machine.
Diffstat:
1 file changed, 19 insertions(+), 6 deletions(-)
diff --git a/src/loaders/binary.c b/src/loaders/binary.c
@@ -20,6 +20,9 @@
#include "fonts.h"
#include "output.h"
+#define STATE_CHARACTER 0
+#define STATE_ATTRIBUTE 1
+
int
ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options)
{
@@ -60,20 +63,26 @@ ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options)
}
/* process binary */
- uint32_t character, attribute, background, foreground;
+ uint8_t character, attribute, *cursor, state = STATE_CHARACTER;
+ uint32_t background, foreground;
size_t loop = 0;
int32_t column = 0, row = 0;
while (loop < ctx->length) {
+ cursor = &ctx->buffer[loop];
+
if (column == options->columns) {
column = 0;
row++;
}
- character = ctx->buffer[loop];
-
- if (ctx->length > loop) {
- attribute = ctx->buffer[loop+1];
+ switch (state) {
+ case STATE_CHARACTER:
+ character = *cursor;
+ state = STATE_ATTRIBUTE;
+ break;
+ case STATE_ATTRIBUTE:
+ attribute = *cursor;
background = (attribute & 240) >> 4;
foreground = (attribute & 15);
@@ -86,8 +95,12 @@ ansilove_binary(struct ansilove_ctx *ctx, struct ansilove_options *options)
colors[foreground], character);
column++;
- loop += 2;
+
+ state = STATE_CHARACTER;
+ break;
}
+
+ loop++;
}
/* create output image */