/* 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"); } }