about summary refs log tree commit diff
path: root/dump.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--dump.c67
1 files changed, 67 insertions, 0 deletions
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 <june@causal.agency>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <err.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysexits.h>
+
+#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");
+	}
+}