shithub: rgbds

Download patch

ref: 3e5cd8ce1ab3294217c14b05ccd4ee6caa28e280
parent: 6902387991696a8ef067981823e790daa2a87774
author: ISSOtm <[email protected]>
date: Sat Nov 12 07:29:28 EST 2022

Use a special name for stdin/stdout in diagnostics

--- a/include/file.hpp
+++ b/include/file.hpp
@@ -78,6 +78,7 @@
 		// See the `operator*` equivalent.
 		return const_cast<File *>(this)->operator->();
 	}
+
 	File *close() {
 		return std::visit(Visitor{[this](std::filebuf &file) {
 			                          // This is called by the destructor, and an explicit `close`
@@ -89,6 +90,14 @@
 		                  _file)
 		           ? this
 		           : nullptr;
+	}
+
+	char const *c_str(std::string const &path) const {
+		return std::visit(Visitor{[&path](std::filebuf const &) { return path.c_str(); },
+		                          [](std::streambuf const *buf) {
+			                          return buf == std::cin.rdbuf() ? "<stdin>" : "<stdout>";
+		                          }},
+		                  _file);
 	}
 };
 
--- a/src/gfx/main.cpp
+++ b/src/gfx/main.cpp
@@ -257,7 +257,7 @@
 static std::vector<size_t> readAtFile(std::string const &path, std::vector<char> &argPool) {
 	File file;
 	if (!file.open(path, std::ios_base::in)) {
-		fatal("Error reading @%s: %s", path.c_str(), strerror(errno));
+		fatal("Error reading @%s: %s", file.c_str(path), strerror(errno));
 	}
 
 	// We only filter out `EOF`, but calling `isblank()` on anything else is UB!
--- a/src/gfx/process.cpp
+++ b/src/gfx/process.cpp
@@ -94,13 +94,13 @@
 	[[noreturn]] static void handleError(png_structp png, char const *msg) {
 		Png *self = reinterpret_cast<Png *>(png_get_error_ptr(png));
 
-		fatal("Error reading input image (\"%s\"): %s", self->path.c_str(), msg);
+		fatal("Error reading input image (\"%s\"): %s", self->file.c_str(self->path), msg);
 	}
 
 	static void handleWarning(png_structp png, char const *msg) {
 		Png *self = reinterpret_cast<Png *>(png_get_error_ptr(png));
 
-		warning("In input image (\"%s\"): %s", self->path.c_str(), msg);
+		warning("In input image (\"%s\"): %s", self->file.c_str(self->path), msg);
 	}
 
 	static void readData(png_structp png, png_bytep data, size_t length) {
@@ -112,7 +112,7 @@
 		if (nbBytesRead != expectedLen) {
 			fatal("Error reading input image (\"%s\"): file too short (expected at least %zd more "
 			      "bytes after reading %lld)",
-			      self->path.c_str(), length - nbBytesRead,
+			      self->file.c_str(self->path), length - nbBytesRead,
 			      self->file->pubseekoff(0, std::ios_base::cur));
 		}
 	}
@@ -177,7 +177,7 @@
 	 */
 	explicit Png(std::string const &filePath) : path(filePath), colors() {
 		if (file.open(path, std::ios_base::in | std::ios_base::binary) == nullptr) {
-			fatal("Failed to open input image (\"%s\"): %s", path.c_str(), strerror(errno));
+			fatal("Failed to open input image (\"%s\"): %s", file.c_str(path), strerror(errno));
 		}
 
 		options.verbosePrint(Options::VERB_LOG_ACT, "Opened input file\n");
@@ -187,7 +187,7 @@
 		if (file->sgetn(reinterpret_cast<char *>(pngHeader.data()), pngHeader.size())
 		        != static_cast<std::streamsize>(pngHeader.size()) // Not enough bytes?
 		    || png_sig_cmp(pngHeader.data(), 0, pngHeader.size()) != 0) {
-			fatal("Input file (\"%s\") is not a PNG image!", path.c_str());
+			fatal("Input file (\"%s\") is not a PNG image!", file.c_str(path));
 		}
 
 		options.verbosePrint(Options::VERB_INTERM, "PNG header signature is OK\n");
@@ -628,7 +628,7 @@
 static void outputPalettes(std::vector<Palette> const &palettes) {
 	File output;
 	if (!output.open(options.palettes, std::ios_base::out | std::ios_base::binary)) {
-		fatal("Failed to open \"%s\": %s", options.palettes.c_str(), strerror(errno));
+		fatal("Failed to open \"%s\": %s", output.c_str(options.palettes), strerror(errno));
 	}
 
 	for (Palette const &palette : palettes) {
@@ -756,7 +756,7 @@
                            DefaultInitVec<size_t> const &mappings) {
 	File output;
 	if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
-		fatal("Failed to open \"%s\": %s", options.output.c_str(), strerror(errno));
+		fatal("Failed to open \"%s\": %s", output.c_str(options.output), strerror(errno));
 	}
 
 	uint64_t remainingTiles = (png.getWidth() / 8) * (png.getHeight() / 8);
@@ -790,19 +790,22 @@
 	if (!options.tilemap.empty()) {
 		tilemapOutput.emplace();
 		if (!tilemapOutput->open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
-			fatal("Failed to open \"%s\": %s", options.tilemap.c_str(), strerror(errno));
+			fatal("Failed to open \"%s\": %s", tilemapOutput->c_str(options.tilemap),
+			      strerror(errno));
 		}
 	}
 	if (!options.attrmap.empty()) {
 		attrmapOutput.emplace();
 		if (!attrmapOutput->open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
-			fatal("Failed to open \"%s\": %s", options.attrmap.c_str(), strerror(errno));
+			fatal("Failed to open \"%s\": %s", attrmapOutput->c_str(options.attrmap),
+			      strerror(errno));
 		}
 	}
 	if (!options.palmap.empty()) {
 		palmapOutput.emplace();
 		if (!palmapOutput->open(options.palmap, std::ios_base::out | std::ios_base::binary)) {
-			fatal("Failed to open \"%s\": %s", options.palmap.c_str(), strerror(errno));
+			fatal("Failed to open \"%s\": %s", palmapOutput->c_str(options.palmap),
+			      strerror(errno));
 		}
 	}
 
@@ -900,7 +903,7 @@
 static void outputTileData(UniqueTiles const &tiles) {
 	File output;
 	if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
-		fatal("Failed to create \"%s\": %s", options.output.c_str(), strerror(errno));
+		fatal("Failed to create \"%s\": %s", output.c_str(options.output), strerror(errno));
 	}
 
 	uint16_t tileID = 0;
@@ -915,7 +918,7 @@
 static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) {
 	File output;
 	if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
-		fatal("Failed to create \"%s\": %s", options.tilemap.c_str(), strerror(errno));
+		fatal("Failed to create \"%s\": %s", output.c_str(options.tilemap), strerror(errno));
 	}
 
 	for (AttrmapEntry const &entry : attrmap) {
@@ -927,7 +930,7 @@
                           DefaultInitVec<size_t> const &mappings) {
 	File output;
 	if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
-		fatal("Failed to create \"%s\": %s", options.attrmap.c_str(), strerror(errno));
+		fatal("Failed to create \"%s\": %s", output.c_str(options.attrmap), strerror(errno));
 	}
 
 	for (AttrmapEntry const &entry : attrmap) {
@@ -942,7 +945,7 @@
                          DefaultInitVec<size_t> const &mappings) {
 	File output;
 	if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
-		fatal("Failed to create \"%s\": %s", options.attrmap.c_str(), strerror(errno));
+		fatal("Failed to create \"%s\": %s", output.c_str(options.attrmap), strerror(errno));
 	}
 
 	for (AttrmapEntry const &entry : attrmap) {
--- a/src/gfx/reverse.cpp
+++ b/src/gfx/reverse.cpp
@@ -30,7 +30,7 @@
 static DefaultInitVec<uint8_t> readInto(std::string path) {
 	File file;
 	if (!file.open(path, std::ios::in | std::ios::binary)) {
-		fatal("Failed to open \"%s\": %s", path.c_str(), strerror(errno));
+		fatal("Failed to open \"%s\": %s", file.c_str(path), strerror(errno));
 	}
 	DefaultInitVec<uint8_t> data(128 * 16); // Begin with some room pre-allocated
 
@@ -149,7 +149,7 @@
 	if (!options.palettes.empty()) {
 		File file;
 		if (!file.open(options.palettes, std::ios::in | std::ios::binary)) {
-			fatal("Failed to open \"%s\": %s", options.palettes.c_str(), strerror(errno));
+			fatal("Failed to open \"%s\": %s", file.c_str(options.palettes), strerror(errno));
 		}
 
 		palettes.clear();
@@ -236,11 +236,11 @@
 	options.verbosePrint(Options::VERB_LOG_ACT, "Writing image...\n");
 	File pngFile;
 	if (!pngFile.open(options.input, std::ios::out | std::ios::binary)) {
-		fatal("Failed to create \"%s\": %s", options.input.c_str(), strerror(errno));
+		fatal("Failed to create \"%s\": %s", pngFile.c_str(options.input), strerror(errno));
 	}
 	png_structp png = png_create_write_struct(
 	    PNG_LIBPNG_VER_STRING,
-	    const_cast<png_voidp>(static_cast<void const *>(options.input.c_str())), pngError,
+	    const_cast<png_voidp>(static_cast<void const *>(pngFile.c_str(options.input))), pngError,
 	    pngWarning);
 	if (!png) {
 		fatal("Couldn't create PNG write struct: %s", strerror(errno));
--- a/test/gfx/test.sh
+++ b/test/gfx/test.sh
@@ -16,7 +16,7 @@
 
 rc=0
 new_test() {
-	cmdline="${*@Q}"
+	cmdline="$*"
 	echo "$bold${green}Testing: $cmdline$rescolors$resbold" >&2
 }
 test() {
@@ -44,11 +44,19 @@
 
 for f in *.png; do
 	flags="$([[ -e "${f%.png}.flags" ]] && echo "@${f%.png}.flags")"
-	new_test "$RGBGFX" $flags "$f"
 
+	new_test "$RGBGFX" $flags "$f"
 	if [[ -e "${f%.png}.err" ]]; then
 		test 2>"$errtmp"
 		diff -u --strip-trailing-cr "${f%.png}.err" "$errtmp" || fail
+	else
+		test || fail $?
+	fi
+
+	new_test "$RGBGFX" $flags - "<$f"
+	if [[ -e "${f%.png}.err" ]]; then
+		test 2>"$errtmp"
+		diff -u --strip-trailing-cr <(sed "s/$f/<stdin>/g" "${f%.png}.err") "$errtmp" || fail
 	else
 		test || fail $?
 	fi