commit 4969c44faeabff37612f41638e5d0e53e6a91b63
parent d885e0cd0c382fef04f3eecedb917bdc11e7ee62
Author: Louis Burda <quent.burda@gmail.com>
Date: Sun, 25 Sep 2022 18:13:57 +0200
Add help and debug flags
Diffstat:
M | pietr.c | | | 53 | +++++++++++++++++++++++++++++++++++++++-------------- |
1 file changed, 39 insertions(+), 14 deletions(-)
diff --git a/pietr.c b/pietr.c
@@ -84,6 +84,7 @@ static int inst_lut[18] = {
};
static const size_t stacksize = 2048;
+static int debug = 0;
static uint32_t colors[20] = {
0xffc0c0, 0xffffc0, 0xc0ffc0, 0xc0ffff, 0xc0c0ff, 0xffc0ff,
@@ -139,7 +140,7 @@ dirstr(int dx, int dy)
void
rotate(int *dx, int *dy)
{
- printf("ROTATE %s ->", dirstr(*dx, *dy));
+ if (debug) printf("ROTATE %s ->", dirstr(*dx, *dy));
if (*dx != 0) {
*dy = *dx;
*dx = 0;
@@ -147,7 +148,7 @@ rotate(int *dx, int *dy)
*dx = -*dy;
*dy = 0;
}
- printf(" %s\n", dirstr(*dx, *dy));
+ if (debug) printf(" %s\n", dirstr(*dx, *dy));
}
void
@@ -189,6 +190,7 @@ int
main(int argc, const char **argv)
{
uint8_t *pix, *vismap;
+ const char *inpath;
int *stack;
int width, height;
int inst, x, y;
@@ -198,12 +200,22 @@ main(int argc, const char **argv)
int stacktop;
int edgedir;
int i, tmp;
- char c;
- if (argc != 2)
- errx(0, "Supply an image");
+ inpath = NULL;
+ for (i = 1; i < argc; i++) {
+ if (!strcmp(argv[i], "-d"))
+ debug = 1;
+ else if (!strcmp(argv[i], "-h"))
+ errx(0, "pietr [-h] [-d] IMAGE");
+ else if (inpath)
+ errx(1, "Multiple image specified");
+ else
+ inpath = argv[i];
+ }
+ if (!inpath)
+ errx(1, "No image specified");
- pix = stbi_load(argv[1], &width, &height, NULL, STBI_rgb);
+ pix = stbi_load(inpath, &width, &height, NULL, STBI_rgb);
if (!pix) errx(1, "Image load failed");
stack = calloc(stacksize, sizeof(int));
@@ -235,7 +247,7 @@ main(int argc, const char **argv)
y += dy;
if (vismap[y * width + x] == (dy + 1) * 2 + (dx + 1)) {
- printf("Loop detected. Stopping..\n");
+ if (debug) printf("Loop detected. Stopping..\n");
break;
}
vismap[y * width + x] = (dy + 1) * 2 + (dx + 1);
@@ -244,11 +256,14 @@ main(int argc, const char **argv)
ASSERT(nextc != BLACK);
inst = getinst(prevc, nextc);
- printf("[%3i,%3i]: %06X -> %06X | %4s | ", x, y,
- colors[prevc], colors[nextc], inst_names[inst]);
- for (i = 0; i < stacktop; i++)
- printf("%i ", stack[i]);
- printf("\n");
+
+ if (debug) {
+ printf("[%3i,%3i]: %06X -> %06X | %4s | ", x, y,
+ colors[prevc], colors[nextc], inst_names[inst]);
+ for (i = 0; i < stacktop; i++)
+ printf("%i ", stack[i]);
+ printf("\n");
+ }
switch (inst) {
case PUSH:
@@ -334,11 +349,21 @@ main(int argc, const char **argv)
break;
case OUTN:
ASSERTI(OUTN, stacktop >= 1);
- printf("%i", stack[--stacktop]);
+ if (debug) {
+ printf("OUTNUM %i\n", stack[stacktop-1]);
+ } else {
+ putchar(stack[stacktop-1]);
+ }
+ stacktop--;
break;
case OUTC:
ASSERTI(OUTC, stacktop >= 1);
- putchar(stack[--stacktop]);
+ if (debug) {
+ printf("OUTCHAR %c\n", stack[stacktop-1]);
+ } else {
+ putchar(stack[stacktop-1]);
+ }
+ stacktop--;
break;
case NONE:
break;