From: Laurent Mazet Date: Tue, 20 Aug 2024 20:39:49 +0000 (+0200) Subject: add recorder X-Git-Tag: v1.0~7 X-Git-Url: https://secure.softndesign.org/git/?a=commitdiff_plain;h=47668fddcbab46f28b8027131d988e9cc465001b;p=pentomino.git add recorder --- diff --git a/pentomino.c b/pentomino.c index 812c464..2032ead 100644 --- a/pentomino.c +++ b/pentomino.c @@ -19,6 +19,7 @@ char *progname = NULL; char *version = "0.1"; char *filename = NULL; +int maxnbrecords = 0; int scale = 1; char *boardname = "6x10"; @@ -40,10 +41,11 @@ char *help = int usage (int ret) { FILE *fd = ret ? stderr : stdout; - fprintf (fd, "usage: %s [-b] [-f file] [-h] [-s int] [-v int]\n", progname); + fprintf (fd, "usage: %s [-b] [-f file] [-h] [-r] [-s int] [-v int]\n", progname); fprintf (fd, " -b: board form [6x10, 5x12, 4x15, 3x20] (%s)\n", boardname); fprintf (fd, " -f: file name (%s)\n", (filename) ? filename : "none"); fprintf (fd, " -h: help message\n"); + fprintf (fd, " -r: max nb records (%d)\n", maxnbrecords); fprintf (fd, " -s: scale [0..3] (%d)\n", scale); fprintf (fd, " -v: verbose level (%d)\n", verbose); fprintf (fd, "%s version %s\n", progname, version); @@ -90,6 +92,14 @@ int main (int argc, char *argv[]) } filename = arg; break; + case 'r': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, fprintf (stderr, "%s: missing max number of records\n", progname)); + return usage (1); + } + maxnbrecords = atoi (arg); + break; case 's': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; if (arg == NULL) { @@ -144,6 +154,14 @@ int main (int argc, char *argv[]) } setscale (board, scale); + /* init recorder */ + int *rec = NULL; + if (maxnbrecords) { + rec = (int *) calloc (maxnbrecords, sizeof (int)); + CHECKALLOC (rec); + } + int nbrecords = 0; + /* gets blocks */ blocks_t *blocks = getblocks ("std"); @@ -249,6 +267,18 @@ int main (int argc, char *argv[]) } int ch = getch (); + if (maxnbrecords) { + if (nbrecords < maxnbrecords) { + rec[nbrecords++] = ch; + } else { + int i; + for (i = 0; i < nbrecords - 1; i++) { + rec[i] = rec[i + 1]; + } + //memmove (rec, rec + 1, nbrecords - 1); + rec[nbrecords - 1] = ch; + } + } /* general controls */ switch (ch) { @@ -343,6 +373,20 @@ int main (int argc, char *argv[]) /* cleaning before quiting */ endwin (); + if (nbrecords) { + int i; + printf ("records: "); + for (i = 0; i < nbrecords; i++) { + if ((rec[i] > 31) && (rec[i] < 128)) { + printf ("%c", rec[i]); + } else { + printf ("0x%x", rec[i]); + } + } + printf ("\n"); + } + + free (rec); freeboard (board); freeblocks (blocks); @@ -356,6 +400,7 @@ int main (int argc, char *argv[]) /* test: pentomino.exe -f nofile.pen 2>&1 | grep "can't read file" */ /* test: pentomino.exe -f bogus.pen 2>&1 | grep 'incorrect file' */ /* test: pentomino.exe -h | grep usage */ +/* test: pentomino.exe -r 2>&1 | grep 'missing max' */ /* test: pentomino.exe -s 2>&1 | grep 'no scale' */ /* test: pentomino.exe -s 4 2>&1 | grep incorrect */ /* test: pentomino.exe -v 2>&1 | grep missing */