Use std::optional for fstack paths

This commit is contained in:
Rangi42
2024-03-07 21:41:02 -05:00
parent 563d699394
commit 75cc12bb3d
3 changed files with 13 additions and 32 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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)