Revert "Switch to using std::filesystem (#1235)"

This reverts commit cf62ff772f.
Some functions used by this break on macOS before 10.15,
which we want to keep supporting.
This commit is contained in:
ISSOtm
2023-12-30 20:31:13 +01:00
committed by Rangi
parent 93d1d85f94
commit 6b559e99b2
5 changed files with 105 additions and 103 deletions

View File

@@ -8,7 +8,6 @@
#include <climits>
#include <cstdio>
#include <errno.h>
#include <filesystem>
#include <fstream>
#include <memory>
#include <optional>
@@ -72,7 +71,7 @@ public:
};
class Png {
std::filesystem::path const &path;
std::string const &path;
File file{};
png_structp png = nullptr;
png_infop info = nullptr;
@@ -89,13 +88,13 @@ class Png {
[[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->string().c_str(), msg);
fatal("Error reading input image (\"%s\"): %s", self->c_str(), 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->string().c_str(), msg);
warning("In input image (\"%s\"): %s", self->c_str(), msg);
}
static void readData(png_structp png, png_bytep data, size_t length) {
@@ -107,7 +106,7 @@ class Png {
if (nbBytesRead != expectedLen) {
fatal("Error reading input image (\"%s\"): file too short (expected at least %zd more "
"bytes after reading %lld)",
self->string().c_str(), length - nbBytesRead,
self->c_str(), length - nbBytesRead,
self->file->pubseekoff(0, std::ios_base::cur));
}
}
@@ -129,7 +128,7 @@ public:
Rgba const &pixel(uint32_t x, uint32_t y) const { return pixels[y * width + x]; }
std::string string() const { return file.string(path); }
char const *c_str() const { return file.c_str(path); }
bool isSuitableForGrayscale() const {
// Check that all of the grays don't fall into the same "bin"
@@ -172,9 +171,9 @@ 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", file.string(path).c_str(),
fatal("Failed to open input image (\"%s\"): %s", file.c_str(path),
strerror(errno));
}
@@ -185,7 +184,7 @@ public:
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!", file.string(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");
@@ -641,10 +640,10 @@ static void outputPalettes(std::vector<Palette> const &palettes) {
options.nbPalettes);
}
if (options.palettes.has_value()) {
if (!options.palettes.empty()) {
File output;
if (!output.open(*options.palettes, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to open \"%s\": %s", output.string(*options.palettes).c_str(),
if (!output.open(options.palettes, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to open \"%s\": %s", output.c_str(options.palettes),
strerror(errno));
}
@@ -774,9 +773,8 @@ static void outputTileData(Png const &png, DefaultInitVec<AttrmapEntry> const &a
std::vector<Palette> const &palettes,
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", output.string(*options.output).c_str(),
strerror(errno));
if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to open \"%s\": %s", output.c_str(options.output), strerror(errno));
}
uint16_t widthTiles = options.inputSlice.width ? options.inputSlice.width : png.getWidth() / 8;
@@ -809,11 +807,11 @@ static void outputTileData(Png const &png, DefaultInitVec<AttrmapEntry> const &a
static void outputMaps(DefaultInitVec<AttrmapEntry> const &attrmap,
DefaultInitVec<size_t> const &mappings) {
std::optional<File> tilemapOutput, attrmapOutput, palmapOutput;
auto autoOpenPath = [](std::optional<std::filesystem::path> &path, std::optional<File> &file) {
if (path.has_value()) {
auto autoOpenPath = [](std::string const &path, std::optional<File> &file) {
if (!path.empty()) {
file.emplace();
if (!file->open(*path, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to open \"%s\": %s", file->string(*path).c_str(),
if (!file->open(path, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to open \"%s\": %s", file->c_str(options.tilemap),
strerror(errno));
}
}
@@ -915,9 +913,8 @@ static UniqueTiles dedupTiles(Png const &png, DefaultInitVec<AttrmapEntry> &attr
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", output.string(*options.output).c_str(),
strerror(errno));
if (!output.open(options.output, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to create \"%s\": %s", output.c_str(options.output), strerror(errno));
}
uint16_t tileID = 0;
@@ -931,9 +928,8 @@ static void outputTileData(UniqueTiles const &tiles) {
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", output.string(*options.tilemap).c_str(),
strerror(errno));
if (!output.open(options.tilemap, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to create \"%s\": %s", output.c_str(options.tilemap), strerror(errno));
}
for (AttrmapEntry const &entry : attrmap) {
@@ -944,9 +940,8 @@ static void outputTilemap(DefaultInitVec<AttrmapEntry> const &attrmap) {
static void outputAttrmap(DefaultInitVec<AttrmapEntry> const &attrmap,
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", output.string(*options.attrmap).c_str(),
strerror(errno));
if (!output.open(options.attrmap, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to create \"%s\": %s", output.c_str(options.attrmap), strerror(errno));
}
for (AttrmapEntry const &entry : attrmap) {
@@ -960,9 +955,8 @@ static void outputAttrmap(DefaultInitVec<AttrmapEntry> const &attrmap,
static void outputPalmap(DefaultInitVec<AttrmapEntry> const &attrmap,
DefaultInitVec<size_t> const &mappings) {
File output;
if (!output.open(*options.palmap, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to create \"%s\": %s", output.string(*options.palmap).c_str(),
strerror(errno));
if (!output.open(options.palmap, std::ios_base::out | std::ios_base::binary)) {
fatal("Failed to create \"%s\": %s", output.c_str(options.palmap), strerror(errno));
}
for (AttrmapEntry const &entry : attrmap) {
@@ -986,7 +980,7 @@ void process() {
options.verbosePrint(Options::VERB_CFG, "Using libpng %s\n", png_get_libpng_ver(nullptr));
options.verbosePrint(Options::VERB_LOG_ACT, "Reading tiles...\n");
Png png(*options.input); // This also sets `hasTransparentPixels` as a side effect
Png png(options.input); // This also sets `hasTransparentPixels` as a side effect
ImagePalette const &colors = png.getColors();
// Now, we have all the image's colors in `colors`
@@ -1108,13 +1102,12 @@ contained:;
nbTilesW * nbTilesH, options.maxNbTiles[0], options.maxNbTiles[1]);
}
if (options.output.has_value()) {
if (!options.output.empty()) {
options.verbosePrint(Options::VERB_LOG_ACT, "Generating unoptimized tile data...\n");
unoptimized::outputTileData(png, attrmap, palettes, mappings);
}
if (options.tilemap.has_value() || options.attrmap.has_value()
|| options.palmap.has_value()) {
if (!options.tilemap.empty() || !options.attrmap.empty() || !options.palmap.empty()) {
options.verbosePrint(
Options::VERB_LOG_ACT,
"Generating unoptimized tilemap and/or attrmap and/or palmap...\n");
@@ -1130,22 +1123,22 @@ contained:;
tiles.size(), options.maxNbTiles[0], options.maxNbTiles[1]);
}
if (options.output.has_value()) {
if (!options.output.empty()) {
options.verbosePrint(Options::VERB_LOG_ACT, "Generating optimized tile data...\n");
optimized::outputTileData(tiles);
}
if (options.tilemap.has_value()) {
if (!options.tilemap.empty()) {
options.verbosePrint(Options::VERB_LOG_ACT, "Generating optimized tilemap...\n");
optimized::outputTilemap(attrmap);
}
if (options.attrmap.has_value()) {
if (!options.attrmap.empty()) {
options.verbosePrint(Options::VERB_LOG_ACT, "Generating optimized attrmap...\n");
optimized::outputAttrmap(attrmap, mappings);
}
if (options.palmap.has_value()) {
if (!options.palmap.empty()) {
options.verbosePrint(Options::VERB_LOG_ACT, "Generating optimized palmap...\n");
optimized::outputPalmap(attrmap, mappings);
}