Skip to content

Commit

Permalink
Revert "Switch to using std::filesystem (gbdev#1235)"
Browse files Browse the repository at this point in the history
This reverts commit cf62ff7.
Some functions used by this break on macOS before 10.15,
which we want to keep supporting.
  • Loading branch information
ISSOtm committed Dec 30, 2023
1 parent 93d1d85 commit 696bbae
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 103 deletions.
11 changes: 5 additions & 6 deletions include/file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <assert.h>
#include <cassert>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iostream>
Expand Down Expand Up @@ -42,7 +41,7 @@ class File {
* This should only be called once, and before doing any `->` operations.
* Returns `nullptr` on error, and a non-null pointer otherwise.
*/
File *open(std::filesystem::path const &path, std::ios_base::openmode mode) {
File *open(std::string const &path, std::ios_base::openmode mode) {
if (path != "-") {
return _file.emplace<std::filebuf>().open(path, mode) ? this : nullptr;
} else if (mode & std::ios_base::in) {
Expand Down Expand Up @@ -86,11 +85,11 @@ class File {
: nullptr;
}

std::string string(std::filesystem::path const &path) const {
return std::visit(Visitor{[&path](std::filebuf const &) { return path.string(); },
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 std::string{buf == std::cin.rdbuf()
? "<stdin>" : "<stdout>"};
return buf == std::cin.rdbuf()
? "<stdin>" : "<stdout>";
}},
_file);
}
Expand Down
14 changes: 6 additions & 8 deletions include/gfx/main.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
#define RGBDS_GFX_MAIN_HPP

#include <array>
#include <filesystem>
#include <limits.h>
#include <optional>
#include <stdint.h>
#include <string>
#include <utility>
Expand All @@ -26,7 +24,7 @@ struct Options {
bool columnMajor = false; // -Z, previously -h
uint8_t verbosity = 0; // -v

std::optional<std::filesystem::path> attrmap{}; // -a, -A
std::string attrmap{}; // -a, -A
std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
enum {
NO_SPEC,
Expand All @@ -43,14 +41,14 @@ struct Options {
} inputSlice{0, 0, 0, 0}; // -L (margins in clockwise order, like CSS)
std::array<uint16_t, 2> maxNbTiles{UINT16_MAX, 0}; // -N
uint8_t nbPalettes = 8; // -n
std::optional<std::filesystem::path> output{}; // -o
std::optional<std::filesystem::path> palettes{}; // -p, -P
std::optional<std::filesystem::path> palmap{}; // -q, -Q
std::string output{}; // -o
std::string palettes{}; // -p, -P
std::string palmap{}; // -q, -Q
uint8_t nbColorsPerPal = 0; // -s; 0 means "auto" = 1 << bitDepth;
std::optional<std::filesystem::path> tilemap{}; // -t, -T
std::string tilemap{}; // -t, -T
uint64_t trim = 0; // -x

std::optional<std::filesystem::path> input{}; // positional arg
std::string input{}; // positional arg

static constexpr uint8_t VERB_NONE = 0; // Normal, no extra output
static constexpr uint8_t VERB_CFG = 1; // Print configuration after parsing options
Expand Down
73 changes: 44 additions & 29 deletions src/gfx/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@
#include <cinttypes>
#include <cstdint>
#include <ctype.h>
#include <filesystem>
#include <fstream>
#include <ios>
#include <limits>
#include <numeric>
#include <optional>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -240,31 +238,30 @@ static void skipWhitespace(char *&arg) {
}

static void registerInput(char const *arg) {
if (options.input.has_value()) {
if (!options.input.empty()) {
fprintf(stderr,
"FATAL: input image specified more than once! (first \"%s\", then "
"\"%s\")\n",
options.input->c_str(), arg);
options.input.c_str(), arg);
printUsage();
exit(1);
} else if (arg[0] == '\0') { // Empty input path
fprintf(stderr, "FATAL: input image path cannot be empty\n");
printUsage();
exit(1);
} else {
options.input.emplace(arg);
options.input = arg;
}
}

/*
* Turn an "at-file"'s contents into an argv that `getopt` can handle
* @param argPool Argument characters will be appended to this vector, for storage purposes.
*/
static std::vector<size_t> readAtFile(std::filesystem::path const &path,
std::vector<char> &argPool) {
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", file.string(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!
Expand Down Expand Up @@ -349,8 +346,8 @@ static char *parseArgv(int argc, char **argv) {
break;
case 'a':
localOptions.autoAttrmap = false;
if (options.attrmap.has_value())
warning("Overriding attrmap file %s", options.attrmap->c_str());
if (!options.attrmap.empty())
warning("Overriding attrmap file %s", options.attrmap.c_str());
options.attrmap = musl_optarg;
break;
case 'b':
Expand Down Expand Up @@ -496,8 +493,8 @@ static char *parseArgv(int argc, char **argv) {
localOptions.groupOutputs = true;
break;
case 'o':
if (options.output.has_value())
warning("Overriding tile data file %s", options.output->c_str());
if (!options.output.empty())
warning("Overriding tile data file %s", options.output.c_str());
options.output = musl_optarg;
break;
case -'P':
Expand All @@ -508,8 +505,8 @@ static char *parseArgv(int argc, char **argv) {
break;
case 'p':
localOptions.autoPalettes = false;
if (options.palettes.has_value())
warning("Overriding palettes file %s", options.palettes->c_str());
if (!options.palettes.empty())
warning("Overriding palettes file %s", options.palettes.c_str());
options.palettes = musl_optarg;
break;
case -'Q':
Expand All @@ -520,8 +517,8 @@ static char *parseArgv(int argc, char **argv) {
break;
case 'q':
localOptions.autoPalmap = false;
if (options.palmap.has_value())
warning("Overriding palette map file %s", options.palmap->c_str());
if (!options.palmap.empty())
warning("Overriding palette map file %s", options.palmap.c_str());
options.palmap = musl_optarg;
break;
case 'r':
Expand Down Expand Up @@ -552,8 +549,8 @@ static char *parseArgv(int argc, char **argv) {
break;
case 't':
localOptions.autoTilemap = false;
if (options.tilemap.has_value())
warning("Overriding tilemap file %s", options.tilemap->c_str());
if (!options.tilemap.empty())
warning("Overriding tilemap file %s", options.tilemap.c_str());
options.tilemap = musl_optarg;
break;
case 'V':
Expand Down Expand Up @@ -658,17 +655,36 @@ int main(int argc, char *argv[]) {
1u << options.bitDepth, options.nbColorsPerPal);
}

auto autoOutPath = [](bool autoOptEnabled, std::optional<std::filesystem::path> &path,
char const *extension) {
auto autoOutPath = [](bool autoOptEnabled, std::string &path, char const *extension) {
if (autoOptEnabled) {
auto image = localOptions.groupOutputs ? options.output : options.input;
if (!image.has_value()) {
auto &image = localOptions.groupOutputs ? options.output : options.input;
if (image.empty()) {
fprintf(stderr, "FATAL: No %s specified\n", localOptions.groupOutputs
? "output tile data file" : "input image");
printUsage();
exit(1);
}
path.emplace(*image).replace_extension(extension);

// Manual implementation of std::filesystem::path.replace_extension().
constexpr std::string_view chars =
// Both must start with a dot!
#if defined(_MSC_VER) || defined(__MINGW32__)
"./\\"sv;
#else
"./"sv;
#endif
size_t len = image.npos;
size_t i = image.find_last_of(chars);
if (i != image.npos && image[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(image[i - 1], 1) == chars.npos) {
// We can replace the extension
len = i;
}
}
path.assign(image, 0, len);
path.append(extension);
}
};
autoOutPath(localOptions.autoAttrmap, options.attrmap, ".attrmap");
Expand Down Expand Up @@ -753,10 +769,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::optional<std::filesystem::path> const &path) {
if (path.has_value()) {
fprintf(stderr, "\t%s: %s\n", name, path->c_str());
auto printPath = [](char const *name, std::string const &path) {
if (!path.empty()) {
fprintf(stderr, "\t%s: %s\n", name, path.c_str());
}
};
printPath("Input image", options.input);
Expand All @@ -772,13 +787,13 @@ int main(int argc, char *argv[]) {
giveUp();
}

if (options.input.has_value()) {
if (!options.input.empty()) {
if (options.reverse()) {
reverse();
} else {
process();
}
} else if (options.palettes.has_value() && options.palSpecType == Options::EXPLICIT
} else if (!options.palettes.empty() && options.palSpecType == Options::EXPLICIT
&& !options.reverse()) {
processPalettes();
} else {
Expand Down
Loading

0 comments on commit 696bbae

Please sign in to comment.