diff options
author | Ted Unangst <tedu@cvs.openbsd.org> | 2014-11-06 19:35:14 +0000 |
---|---|---|
committer | Ted Unangst <tedu@cvs.openbsd.org> | 2014-11-06 19:35:14 +0000 |
commit | 67a64c7538638434ca336196b395459418ed8ca2 (patch) | |
tree | c607487b9cff9cfa1b4ad570dd053d671ded4b34 /games/bcd/bcd.c | |
parent | db813f3a5a58545cb030b0659b726b7a9dde9406 (diff) |
basic decode functionality
Diffstat (limited to 'games/bcd/bcd.c')
-rw-r--r-- | games/bcd/bcd.c | 68 |
1 files changed, 66 insertions, 2 deletions
diff --git a/games/bcd/bcd.c b/games/bcd/bcd.c index fd4f3a25972..550d5de9721 100644 --- a/games/bcd/bcd.c +++ b/games/bcd/bcd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bcd.c,v 1.16 2014/11/04 17:58:26 tedu Exp $ */ +/* $OpenBSD: bcd.c,v 1.17 2014/11/06 19:35:13 tedu Exp $ */ /* $NetBSD: bcd.c,v 1.6 1995/04/24 12:22:23 cgd Exp $ */ /* @@ -112,6 +112,7 @@ u_short holes[256] = { void printonecard(char *, size_t); void printcard(char *); +int decode(char *buf); #define COLUMNS 48 @@ -119,18 +120,24 @@ void printcard(char *); int main(int argc, char *argv[]) { + char cardline[1024]; /* * The original bcd prompts with a "%" when reading from stdin, * but this seems kind of silly. So this one doesn't. */ if (argc > 1) { + if (strcmp(argv[1], "-d") == 0) { + while (decode(cardline) == 0) { + printf("%s\n", cardline); + } + return 0; + } while (--argc) { argv++; printcard(*argv); } } else { - char cardline[1024]; while (fgets(cardline, sizeof(cardline), stdin)) printcard(cardline); } @@ -163,6 +170,10 @@ printonecard(char *str, size_t len) for (p = str; p < end; ++p) *p = toupper((unsigned char)*p); + for (p = str; p < end; p++) { + printf("%c: %x\n", *p, holes[(unsigned char)*p]); + } + /* top of card */ putchar(' '); for (i = 1; i <= COLUMNS; ++i) @@ -212,3 +223,56 @@ printonecard(char *str, size_t len) putchar('|'); putchar('\n'); } + +#define LINES 12 + +int +decode(char *buf) +{ + int col, i; + char lines[LINES][1024]; + char tmp[1024]; + + /* top of card; if missing signal no more input */ + if (fgets(tmp, sizeof(tmp), stdin) == NULL) + return 1; + /* text line, ignored */ + if (fgets(tmp, sizeof(tmp), stdin) == NULL) + return -1; + /* twelve lines of data */ + for (i = 0; i < LINES; i++) + if (fgets(lines[i], sizeof(lines[i]), stdin) == NULL) + return -1; + /* bottom of card */ + if (fgets(tmp, sizeof(tmp), stdin) == NULL) + return -1; + + for (i = 0; i < LINES; i++) { + if (strlen(lines[i]) < COLUMNS + 2) + return -1; + if (lines[i][0] != '|' || lines[i][COLUMNS + 1] != '|') + return -1; + memmove(&lines[i][0], &lines[i][1], COLUMNS); + lines[i][COLUMNS] = 0; + } + for (col = 0; col < COLUMNS; col++) { + unsigned int val = 0; + for (i = 0; i < LINES; i++) + if (lines[i][col] == ']') + val |= 1 << (11 - i); + buf[col] = ' '; + for (i = 0; i < 256; i++) + if (holes[i] == val && holes[i]) { + buf[col] = i; + break; + } + } + buf[col] = 0; + for (col = COLUMNS - 1; col >= 0; col--) { + if (buf[col] == ' ') + buf[col] = '\0'; + else + break; + } + return 0; +} |