CARDS.DLL loader for SDL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dump.c 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* Copyright (C) 2019 C. McEnroe <june@causal.agency>
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Affero General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <err.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <sysexits.h>
  21. #include "cards.h"
  22. int main(int argc, char *argv[]) {
  23. FILE *file = stdin;
  24. if (argc > 1) {
  25. file = fopen(argv[1], "r");
  26. if (!file) err(EX_NOINPUT, "%s", argv[1]);
  27. }
  28. size_t cap = 4096;
  29. void *ptr = malloc(cap);
  30. if (!ptr) err(EX_OSERR, "malloc");
  31. size_t len = 0, read;
  32. while (0 < (read = fread(&ptr[len], 1, cap - len, file))) {
  33. len += read;
  34. if (len < cap) continue;
  35. cap *= 2;
  36. ptr = realloc(ptr, cap);
  37. if (!ptr) err(EX_OSERR, "realloc");
  38. }
  39. if (ferror(file)) err(EX_IOERR, "fread");
  40. fclose(file);
  41. errno = 0;
  42. int error = cardsLoad(ptr, len);
  43. if (error && errno) err(EX_OSERR, "cardsLoad");
  44. if (error) errx(EX_DATAERR, "cannot load cards");
  45. for (size_t i = 0; i < CardsLen; ++i) {
  46. if (!cardsData[i].ptr) continue;
  47. char name[sizeof("00.bmp")];
  48. snprintf(name, sizeof(name), "%02zd.bmp", i);
  49. FILE *file = fopen(name, "w");
  50. if (!file) err(EX_CANTCREAT, "%s", name);
  51. fwrite(cardsData[i].ptr, cardsData[i].len, 1, file);
  52. if (ferror(file)) err(EX_IOERR, "fwrite");
  53. error = fclose(file);
  54. if (error) err(EX_IOERR, "fclose");
  55. }
  56. }