mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 18:22:07 +00:00
Use std::optional for fstack paths
This commit is contained in:
@@ -59,11 +59,7 @@ char const *fstk_GetFileName();
|
|||||||
|
|
||||||
void fstk_AddIncludePath(char const *s);
|
void fstk_AddIncludePath(char const *s);
|
||||||
void fstk_SetPreIncludeFile(char const *s);
|
void fstk_SetPreIncludeFile(char const *s);
|
||||||
/*
|
std::optional<std::string> fstk_FindFile(char const *path);
|
||||||
* @param path The user-provided file name
|
|
||||||
* @return A pointer to the `new`-allocated full path, or `nullptr` if no path worked
|
|
||||||
*/
|
|
||||||
std::string *fstk_FindFile(char const *path);
|
|
||||||
|
|
||||||
bool yywrap();
|
bool yywrap();
|
||||||
void fstk_RunInclude(char const *path);
|
void fstk_RunInclude(char const *path);
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
#include <optional>
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -159,25 +160,19 @@ static bool isPathValid(char const *path) {
|
|||||||
return !S_ISDIR(statbuf.st_mode);
|
return !S_ISDIR(statbuf.st_mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string *fstk_FindFile(char const *path) {
|
std::optional<std::string> fstk_FindFile(char const *path) {
|
||||||
std::string *fullPath = new (std::nothrow) std::string();
|
for (std::string &str : includePaths) {
|
||||||
|
std::string fullPath = str + path;
|
||||||
if (!fullPath) {
|
if (isPathValid(fullPath.c_str())) {
|
||||||
error("Failed to allocate string during include path search: %s\n", strerror(errno));
|
printDep(fullPath.c_str());
|
||||||
} else {
|
return fullPath;
|
||||||
for (std::string &str : includePaths) {
|
|
||||||
*fullPath = str + path;
|
|
||||||
if (isPathValid(fullPath->c_str())) {
|
|
||||||
printDep(fullPath->c_str());
|
|
||||||
return fullPath;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
errno = ENOENT;
|
errno = ENOENT;
|
||||||
if (generatedMissingIncludes)
|
if (generatedMissingIncludes)
|
||||||
printDep(path);
|
printDep(path);
|
||||||
return nullptr;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool yywrap() {
|
bool yywrap() {
|
||||||
@@ -265,8 +260,7 @@ static Context &newContext(FileStackNode &fileInfo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void fstk_RunInclude(char const *path) {
|
void fstk_RunInclude(char const *path) {
|
||||||
std::string *fullPath = fstk_FindFile(path);
|
std::optional<std::string> fullPath = fstk_FindFile(path);
|
||||||
|
|
||||||
if (!fullPath) {
|
if (!fullPath) {
|
||||||
if (generatedMissingIncludes) {
|
if (generatedMissingIncludes) {
|
||||||
if (verbose)
|
if (verbose)
|
||||||
@@ -279,7 +273,6 @@ void fstk_RunInclude(char const *path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FileStackNode *fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, *fullPath);
|
FileStackNode *fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, *fullPath);
|
||||||
delete fullPath;
|
|
||||||
if (!fileInfo) {
|
if (!fileInfo) {
|
||||||
error("Failed to alloc file info for INCLUDE: %s\n", strerror(errno));
|
error("Failed to alloc file info for INCLUDE: %s\n", strerror(errno));
|
||||||
return;
|
return;
|
||||||
@@ -303,15 +296,13 @@ static void runPreIncludeFile() {
|
|||||||
if (!preIncludeName)
|
if (!preIncludeName)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::string *fullPath = fstk_FindFile(preIncludeName);
|
std::optional<std::string> fullPath = fstk_FindFile(preIncludeName);
|
||||||
|
|
||||||
if (!fullPath) {
|
if (!fullPath) {
|
||||||
error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno));
|
error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
FileStackNode *fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, *fullPath);
|
FileStackNode *fileInfo = new (std::nothrow) FileStackNode(NODE_FILE, *fullPath);
|
||||||
delete fullPath;
|
|
||||||
if (!fileInfo) {
|
if (!fileInfo) {
|
||||||
error("Failed to alloc file info for pre-include: %s\n", strerror(errno));
|
error("Failed to alloc file info for pre-include: %s\n", strerror(errno));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -846,11 +846,8 @@ void sect_BinaryFile(char const *s, int32_t startPos) {
|
|||||||
if (!checkcodesection())
|
if (!checkcodesection())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::string *fullPath = fstk_FindFile(s);
|
std::optional<std::string> fullPath = fstk_FindFile(s);
|
||||||
FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr;
|
FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr;
|
||||||
|
|
||||||
delete fullPath;
|
|
||||||
|
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (generatedMissingIncludes) {
|
if (generatedMissingIncludes) {
|
||||||
if (verbose)
|
if (verbose)
|
||||||
@@ -915,11 +912,8 @@ void sect_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length) {
|
|||||||
if (!reserveSpace(length))
|
if (!reserveSpace(length))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::string *fullPath = fstk_FindFile(s);
|
std::optional<std::string> fullPath = fstk_FindFile(s);
|
||||||
FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr;
|
FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : nullptr;
|
||||||
|
|
||||||
delete fullPath;
|
|
||||||
|
|
||||||
if (!f) {
|
if (!f) {
|
||||||
if (generatedMissingIncludes) {
|
if (generatedMissingIncludes) {
|
||||||
if (verbose)
|
if (verbose)
|
||||||
|
|||||||
Reference in New Issue
Block a user