From 6526886cd0208e011ddcd65ad0bb5b1be234b4b6 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sun, 10 Mar 2019 22:05:54 -0400 Subject: Add cards.h, cards.c, dump.c --- dump.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 dump.c (limited to 'dump.c') diff --git a/dump.c b/dump.c new file mode 100644 index 0000000..0eefc3c --- /dev/null +++ b/dump.c @@ -0,0 +1,67 @@ +/* Copyright (C) 2019 C. McEnroe + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include "cards.h" + +int main(int argc, char *argv[]) { + FILE *file = stdin; + if (argc > 1) { + file = fopen(argv[1], "r"); + if (!file) err(EX_NOINPUT, "%s", argv[1]); + } + + size_t cap = 4096; + void *ptr = malloc(cap); + if (!ptr) err(EX_OSERR, "malloc"); + + size_t len = 0, read; + while (0 < (read = fread(&ptr[len], 1, cap - len, file))) { + len += read; + if (len < cap) continue; + cap *= 2; + ptr = realloc(ptr, cap); + if (!ptr) err(EX_OSERR, "realloc"); + } + if (ferror(file)) err(EX_IOERR, "fread"); + fclose(file); + + errno = 0; + int error = cardsLoad(ptr, len); + if (error && errno) err(EX_OSERR, "cardsLoad"); + if (error) errx(EX_DATAERR, "cannot load cards"); + + for (size_t i = 0; i < CardsLen; ++i) { + if (!cardsData[i].ptr) continue; + + char name[sizeof("00.bmp")]; + snprintf(name, sizeof(name), "%02zd.bmp", i); + + FILE *file = fopen(name, "w"); + if (!file) err(EX_CANTCREAT, "%s", name); + + fwrite(cardsData[i].ptr, cardsData[i].len, 1, file); + if (ferror(file)) err(EX_IOERR, "fwrite"); + + error = fclose(file); + if (error) err(EX_IOERR, "fclose"); + } +} -- cgit 1.4.1