mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-24 03:52:08 +00:00
Switch to using std::filesystem (#1235)
Allows better platform-agnostic path manipulation. Also, using `std::optional` rather than empty strings allows correctly handling empty arguments (treating them as such, instead of acting as they were never passed).
This commit is contained in:
@@ -7,11 +7,11 @@
|
||||
#include <assert.h>
|
||||
#include <cassert>
|
||||
#include <fcntl.h>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <streambuf>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
#include <string_view>
|
||||
#include <variant>
|
||||
@@ -41,7 +41,7 @@ public:
|
||||
* This should only be called once, and before doing any `->` operations.
|
||||
* Returns `nullptr` on error, and a non-null pointer otherwise.
|
||||
*/
|
||||
File *open(std::string const &path, std::ios_base::openmode mode) {
|
||||
File *open(std::filesystem::path 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) {
|
||||
@@ -85,8 +85,15 @@ public:
|
||||
: nullptr;
|
||||
}
|
||||
|
||||
char const *c_str(std::string const &path) const {
|
||||
return std::visit(Visitor{[&path](std::filebuf const &) { return path.c_str(); },
|
||||
char const *c_str(std::filesystem::path const &path) const {
|
||||
// FIXME: This is a hack to prevent the path string from being destroyed until
|
||||
// `.c_str(path)` is called again. It's necessary because just `return path.c_str()`
|
||||
// fails on Windows, where paths use `wchar_t`.
|
||||
static std::string path_string;
|
||||
return std::visit(Visitor{[&path](std::filebuf const &) {
|
||||
path_string = path.string();
|
||||
return path_string.c_str();
|
||||
},
|
||||
[](std::streambuf const *buf) {
|
||||
return buf == std::cin.rdbuf() ? "<stdin>" : "<stdout>";
|
||||
}},
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
#define RGBDS_GFX_MAIN_HPP
|
||||
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <limits.h>
|
||||
#include <optional>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
@@ -24,7 +26,7 @@ struct Options {
|
||||
bool columnMajor = false; // -Z, previously -h
|
||||
uint8_t verbosity = 0; // -v
|
||||
|
||||
std::string attrmap{}; // -a, -A
|
||||
std::optional<std::filesystem::path> attrmap{}; // -a, -A
|
||||
std::array<uint8_t, 2> baseTileIDs{0, 0}; // -b
|
||||
enum {
|
||||
NO_SPEC,
|
||||
@@ -41,14 +43,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::string output{}; // -o
|
||||
std::string palettes{}; // -p, -P
|
||||
std::string palmap{}; // -q, -Q
|
||||
std::optional<std::filesystem::path> output{}; // -o
|
||||
std::optional<std::filesystem::path> palettes{}; // -p, -P
|
||||
std::optional<std::filesystem::path> palmap{}; // -q, -Q
|
||||
uint8_t nbColorsPerPal = 0; // -s; 0 means "auto" = 1 << bitDepth;
|
||||
std::string tilemap{}; // -t, -T
|
||||
std::optional<std::filesystem::path> tilemap{}; // -t, -T
|
||||
uint64_t trim = 0; // -x
|
||||
|
||||
std::string input{}; // positional arg
|
||||
std::optional<std::filesystem::path> 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
|
||||
|
||||
Reference in New Issue
Block a user