shithub: pokecrystal

Download patch

ref: c8f06f45d59bf1008e98497481629edc77993300
parent: 0d1a029e818c295b7cd04bef6527a20f3178f439
author: Rangi <[email protected]>
date: Wed Sep 1 23:04:40 EDT 2021

Use an `error_exit` macro for tools

--- a/tools/common.h
+++ b/tools/common.h
@@ -11,6 +11,8 @@
 #include <unistd.h>
 #include <getopt.h>
 
+#define error_exit(...) exit((fprintf(stderr, __VA_ARGS__), 1))
+
 int getopt_long_index;
 #define getopt_long(argc, argv, optstring, longopts) getopt_long(argc, argv, optstring, longopts, &getopt_long_index)
 
@@ -18,8 +20,7 @@
 	errno = 0;
 	void *m = malloc(size);
 	if (!m) {
-		fprintf(stderr, "Could not allocate %zu bytes: %s\n", size, strerror(errno));
-		exit(1);
+		error_exit("Could not allocate %zu bytes: %s\n", size, strerror(errno));
 	}
 	return m;
 }
@@ -29,8 +30,7 @@
 	errno = 0;
 	FILE *f = fopen(filename, mode);
 	if (!f) {
-		fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno));
-		exit(1);
+		error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
 	}
 	return f;
 }
@@ -38,9 +38,8 @@
 void fread_verbose(uint8_t *data, size_t size, const char *filename, FILE *f) {
 	errno = 0;
 	if (fread(data, 1, size, f) != size) {
-		fprintf(stderr, "Could not read from file \"%s\": %s\n", filename, strerror(errno));
 		fclose(f);
-		exit(1);
+		error_exit("Could not read from file \"%s\": %s\n", filename, strerror(errno));
 	}
 }
 
@@ -47,9 +46,8 @@
 void fwrite_verbose(const uint8_t *data, size_t size, const char *filename, FILE *f) {
 	errno = 0;
 	if (fwrite(data, 1, size, f) != size) {
-		fprintf(stderr, "Could not write to file \"%s\": %s\n", filename, strerror(errno));
 		fclose(f);
-		exit(1);
+		error_exit("Could not write to file \"%s\": %s\n", filename, strerror(errno));
 	}
 }
 
@@ -63,8 +61,7 @@
 		}
 	}
 	if (size == -1) {
-		fprintf(stderr, "Could not measure file \"%s\": %s\n", filename, strerror(errno));
-		exit(1);
+		error_exit("Could not measure file \"%s\": %s\n", filename, strerror(errno));
 	}
 	return size;
 }
@@ -94,9 +91,8 @@
 		'I', 'H', 'D', 'R',                          // IHDR chunk type
 	};
 	if (memcmp(header, expected_header, sizeof(header))) {
-		fprintf(stderr, "Not a valid PNG file: \"%s\"\n", filename);
 		fclose(f);
-		exit(1);
+		error_exit("Not a valid PNG file: \"%s\"\n", filename);
 	}
 	uint8_t bytes[4] = {0};
 	fread_verbose(bytes, sizeof(bytes), filename, f);
--- a/tools/png_dimensions.c
+++ b/tools/png_dimensions.c
@@ -7,8 +7,7 @@
 uint8_t read_dimensions(const char *filename) {
 	uint32_t width_px = read_png_width_verbose(filename);
 	if (width_px != 40 && width_px != 48 && width_px != 56) {
-		fprintf(stderr, "Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px);
-		exit(1);
+		error_exit("Not a valid width for \"%s\": %" PRIu32 " px\n", filename, width_px);
 	}
 	uint8_t width_tiles = (uint8_t)(width_px / 8);
 	return (width_tiles << 4) | width_tiles;
--- a/tools/scan_includes.c
+++ b/tools/scan_includes.c
@@ -9,8 +9,7 @@
 	FILE *f = fopen(filename, "rb");
 	if (!f) {
 		if (strict) {
-			fprintf(stderr, "Could not open file \"%s\": %s\n", filename, strerror(errno));
-			exit(1);
+			error_exit("Could not open file \"%s\": %s\n", filename, strerror(errno));
 		} else {
 			return;
 		}