shithub: rgbds

Download patch

ref: c4361b965c965a1274fc43286c30c8769e8ebdd4
parent: 8d00a61602df064d4fe5c4ea5acc72357f451400
author: ISSOtm <[email protected]>
date: Wed Mar 9 04:35:46 EST 2022

Remove <filesystem>

Should fix compilation with GCC before 9

--- a/include/gfx/main.hpp
+++ b/include/gfx/main.hpp
@@ -10,9 +10,9 @@
 #define RGBDS_GFX_MAIN_HPP
 
 #include <array>
-#include <filesystem>
 #include <limits.h>
 #include <stdint.h>
+#include <string>
 #include <vector>
 
 #include "helpers.h"
@@ -27,7 +27,7 @@
 	bool beVerbose = false; // -v
 	bool columnMajor = false; // -Z, previously -h
 
-	std::filesystem::path attrmap{}; // -a, -A
+	std::string attrmap{}; // -a, -A
 	std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
 	enum {
 		NO_SPEC,
@@ -39,14 +39,14 @@
 	std::array<uint32_t, 4> inputSlice{0, 0, 0, 0}; // -L
 	std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
 	uint8_t nbPalettes = 8; // -n
-	std::filesystem::path output{}; // -o
-	std::filesystem::path palettes{}; // -p, -P
+	std::string output{}; // -o
+	std::string palettes{}; // -p, -P
 	uint8_t nbColorsPerPal = 0; // -s; 0 means "auto" = 1 << bitDepth;
-	std::filesystem::path tilemap{}; // -t, -T
+	std::string tilemap{}; // -t, -T
 	std::array<uint16_t, 2> unitSize{1, 1}; // -U (in tiles)
 	uint64_t trim = 0; // -x
 
-	std::filesystem::path input{}; // positional arg
+	std::string input{}; // positional arg
 
 	format_(printf, 2, 3) void verbosePrint(char const *fmt, ...) const;
 	uint8_t maxPalSize() const {
--- a/src/gfx/convert.cpp
+++ b/src/gfx/convert.cpp
@@ -62,7 +62,7 @@
 };
 
 class Png {
-	std::filesystem::path const &path;
+	std::string const &path;
 	std::filebuf file{};
 	png_structp png = nullptr;
 	png_infop info = nullptr;
@@ -156,7 +156,7 @@
 	 * We also use that occasion to only read the PNG one line at a time, since we store all of
 	 * the pixel data in `pixels`, which saves on memory allocations.
 	 */
-	explicit Png(std::filesystem::path const &filePath) : path(filePath), colors() {
+	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));
 		}
--- a/src/gfx/main.cpp
+++ b/src/gfx/main.cpp
@@ -11,7 +11,6 @@
 #include <algorithm>
 #include <assert.h>
 #include <charconv>
-#include <filesystem>
 #include <inttypes.h>
 #include <limits>
 #include <stdarg.h>
@@ -26,6 +25,8 @@
 
 #include "gfx/convert.hpp"
 
+using namespace std::literals::string_view_literals;
+
 Options options;
 static uintmax_t nbErrors;
 
@@ -287,10 +288,25 @@
 
 	options.input = argv[argc - 1];
 
-	auto autoOutPath = [](bool autoOptEnabled, std::filesystem::path &path, char const *extension) {
+	auto autoOutPath = [](bool autoOptEnabled, std::string &path, char const *extension) {
 		if (autoOptEnabled) {
-			path = options.input;
-			path.replace_extension(extension);
+			constexpr std::string_view chars =
+// Both must start with a dot!
+#if defined(_MSC_VER) || defined(__MINGW32__)
+				"./\\"sv;
+#else
+				"./"sv;
+#endif
+			size_t i = options.input.find_last_of(chars);
+			if (i != options.input.npos && options.input[i] == '.') {
+				// We found the last dot, but check if it's part of a stem
+				// (There must be a non-path separator character before it)
+				if (i != 0 && chars.find(options.input[i - 1], 1) == chars.npos) {
+					// We can replace the extension
+					path.resize(i + 1); // Keep the dot, though
+					path.append(extension);
+				}
+			}
 		}
 	};
 	autoOutPath(autoAttrmap, options.attrmap, ".attrmap");
@@ -337,15 +353,9 @@
 		        options.baseTileIDs[1]);
 		fprintf(stderr, "\tMaximum %" PRIu16 " tiles in bank 0, %" PRIu16 " in bank 1\n",
 		        options.maxNbTiles[0], options.maxNbTiles[1]);
-		auto printPath = [](char const *name, std::filesystem::path const &path) {
+		auto printPath = [](char const *name, std::string const &path) {
 			if (!path.empty()) {
-#ifdef _MSC_VER
-	#define PRIpath "ls"
-#else
-	#define PRIpath "s"
-#endif
-				fprintf(stderr, "\t%s: %" PRIpath "\n", name, path.c_str());
-#undef PRIpath
+				fprintf(stderr, "\t%s: %s\n", name, path.c_str());
 			}
 		};
 		printPath("Input image", options.input);
--- a/src/gfx/pal_sorting.cpp
+++ b/src/gfx/pal_sorting.cpp
@@ -39,7 +39,8 @@
 				return std::find(colors.begin(), colors.end(), color) != colors.end();
 			})) {
 			if (palette.size() != options.maxPalSize()) {
-				warning("Unused color in PNG embedded palette was re-added; please use `-c embedded` to get this in future versions");
+				warning("Unused color in PNG embedded palette was re-added; please use `-c "
+				        "embedded` to get this in future versions");
 			}
 			// Overwrite the palette, and return with that (it's already sorted)
 			palette.colors = colors;