about summary refs log tree commit diff
path: root/dump.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--dump.c59
1 files changed, 21 insertions, 38 deletions
diff --git a/dump.c b/dump.c
index 0eefc3c..1d3b6dc 100644
--- a/dump.c
+++ b/dump.c
@@ -14,54 +14,37 @@
  * 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 <SDL.h>
 
 #include "cards.h"
 
+static int fail(const char *prefix) {
+	fprintf(stderr, "%s: %s\n", prefix, SDL_GetError());
+	return EXIT_FAILURE;
+}
+
 int main(int argc, char *argv[]) {
-	FILE *file = stdin;
+	struct SDL_RWops *rw;
 	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");
+		rw = SDL_RWFromFile(argv[1], "rb");
+	} else {
+		rw = SDL_RWFromFP(stdin, SDL_FALSE);
 	}
-	if (ferror(file)) err(EX_IOERR, "fread");
-	fclose(file);
+	if (!rw) return fail("SDL_RWFromFile");
 
-	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;
+	struct Cards *cards = Cards_Load(rw);
+	if (!cards) return fail("Cards_Load");
+	SDL_RWclose(rw);
 
+	for (int i = 0; i < Cards_CardLen; ++i) {
+		if (!cards->surfaces[i]) 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");
+		snprintf(name, sizeof(name), "%02d.bmp", i);
+		SDL_SaveBMP(cards->surfaces[i], name);
 	}
+
+	Cards_Free(cards);
 }