diff --git a/include/gfx/main.hpp b/include/gfx/main.hpp index 15128aaa..5fb0e94f 100644 --- a/include/gfx/main.hpp +++ b/include/gfx/main.hpp @@ -10,9 +10,9 @@ #define RGBDS_GFX_MAIN_HPP #include -#include #include #include +#include #include #include "helpers.h" @@ -27,7 +27,7 @@ struct Options { bool beVerbose = false; // -v bool columnMajor = false; // -Z, previously -h - std::filesystem::path attrmap{}; // -a, -A + std::string attrmap{}; // -a, -A std::array baseTileIDs{0, 0}; // -b enum { NO_SPEC, @@ -39,14 +39,14 @@ struct Options { std::array inputSlice{0, 0, 0, 0}; // -L std::array 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 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 { diff --git a/src/gfx/convert.cpp b/src/gfx/convert.cpp index 0995dd90..63a3260e 100644 --- a/src/gfx/convert.cpp +++ b/src/gfx/convert.cpp @@ -62,7 +62,7 @@ public: }; 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 @@ public: * 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)); } diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index d2ec1eb5..2b6c2923 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -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 @@ int main(int argc, char *argv[]) { 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 @@ int main(int argc, char *argv[]) { 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); diff --git a/src/gfx/pal_sorting.cpp b/src/gfx/pal_sorting.cpp index 0ce543fa..5ae66295 100644 --- a/src/gfx/pal_sorting.cpp +++ b/src/gfx/pal_sorting.cpp @@ -39,7 +39,8 @@ void indexed(std::vector &palettes, int palSize, png_color const *palRG 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;