mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Simplify fstk_FindFile usage (#1310)
* Simplify `fstk_FindFile` usage * Use `std::string` for `fstk_FindFile`
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
|
||||
#include "asm/lexer.hpp"
|
||||
|
||||
@@ -52,12 +53,9 @@ void fstk_AddIncludePath(char const *s);
|
||||
void fstk_SetPreIncludeFile(char const *s);
|
||||
/*
|
||||
* @param path The user-provided file name
|
||||
* @param fullPath The address of a pointer, which will be made to point at the full path
|
||||
* The pointer's value must be a valid argument to `realloc`, including NULL
|
||||
* @param size Current size of the buffer, or 0 if the pointer is NULL
|
||||
* @return True if the file was found, false if no path worked
|
||||
* @return A pointer to the `new`-allocated full path, or NULL if no path worked
|
||||
*/
|
||||
bool fstk_FindFile(char const *path, char **fullPath, size_t *size);
|
||||
std::string *fstk_FindFile(char const *path);
|
||||
|
||||
bool yywrap(void);
|
||||
void fstk_RunInclude(char const *path);
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <new>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
#include "asm/fstack.hpp"
|
||||
#include "asm/macro.hpp"
|
||||
@@ -159,47 +161,20 @@ static bool isPathValid(char const *path)
|
||||
return !S_ISDIR(statbuf.st_mode);
|
||||
}
|
||||
|
||||
bool fstk_FindFile(char const *path, char **fullPath, size_t *size)
|
||||
std::string *fstk_FindFile(char const *path)
|
||||
{
|
||||
if (!*size) {
|
||||
*size = 64; // This is arbitrary, really
|
||||
*fullPath = (char *)realloc(*fullPath, *size);
|
||||
if (!*fullPath)
|
||||
error("realloc error during include path search: %s\n",
|
||||
strerror(errno));
|
||||
}
|
||||
std::string *fullPath = new(std::nothrow) std::string();
|
||||
|
||||
if (*fullPath) {
|
||||
if (!fullPath) {
|
||||
error("Failed to allocate string during include path search: %s\n", strerror(errno));
|
||||
} else {
|
||||
for (size_t i = 0; i <= nbIncPaths; ++i) {
|
||||
char const *incPath = i ? includePaths[i - 1] : "";
|
||||
int len = snprintf(*fullPath, *size, "%s%s", incPath, path);
|
||||
*fullPath = i ? includePaths[i - 1] : "";
|
||||
*fullPath += path;
|
||||
|
||||
if (len < 0) {
|
||||
error("snprintf error during include path search: %s\n",
|
||||
strerror(errno));
|
||||
break;
|
||||
}
|
||||
|
||||
// Oh how I wish `asnprintf` was standard...
|
||||
if ((size_t)len >= *size) { // `size` includes the terminator, `len` doesn't
|
||||
*size = len + 1;
|
||||
*fullPath = (char *)realloc(*fullPath, *size);
|
||||
if (!*fullPath) {
|
||||
error("realloc error during include path search: %s\n",
|
||||
strerror(errno));
|
||||
break;
|
||||
}
|
||||
len = sprintf(*fullPath, "%s%s", incPath, path);
|
||||
if (len < 0) {
|
||||
error("sprintf error during include path search: %s\n",
|
||||
strerror(errno));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (isPathValid(*fullPath)) {
|
||||
printDep(*fullPath);
|
||||
return true;
|
||||
if (isPathValid(fullPath->c_str())) {
|
||||
printDep(fullPath->c_str());
|
||||
return fullPath;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -207,7 +182,7 @@ bool fstk_FindFile(char const *path, char **fullPath, size_t *size)
|
||||
errno = ENOENT;
|
||||
if (generatedMissingIncludes)
|
||||
printDep(path);
|
||||
return false;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool yywrap(void)
|
||||
@@ -320,11 +295,9 @@ static void newContext(struct FileStackNode *fileInfo)
|
||||
|
||||
void fstk_RunInclude(char const *path)
|
||||
{
|
||||
char *fullPath = NULL;
|
||||
size_t size = 0;
|
||||
std::string *fullPath = fstk_FindFile(path);
|
||||
|
||||
if (!fstk_FindFile(path, &fullPath, &size)) {
|
||||
free(fullPath);
|
||||
if (!fullPath) {
|
||||
if (generatedMissingIncludes) {
|
||||
if (verbose)
|
||||
printf("Aborting (-MG) on INCLUDE file '%s' (%s)\n",
|
||||
@@ -337,15 +310,15 @@ void fstk_RunInclude(char const *path)
|
||||
}
|
||||
|
||||
struct FileStackNamedNode *fileInfo =
|
||||
(struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + size);
|
||||
(struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + fullPath->length() + 1);
|
||||
|
||||
if (!fileInfo) {
|
||||
error("Failed to alloc file info for INCLUDE: %s\n", strerror(errno));
|
||||
return;
|
||||
}
|
||||
fileInfo->node.type = NODE_FILE;
|
||||
strcpy(fileInfo->name, fullPath);
|
||||
free(fullPath);
|
||||
strcpy(fileInfo->name, fullPath->c_str());
|
||||
delete fullPath;
|
||||
|
||||
newContext((struct FileStackNode *)fileInfo);
|
||||
contextStack->lexerState = lexer_OpenFile(fileInfo->name);
|
||||
@@ -365,25 +338,23 @@ static void runPreIncludeFile(void)
|
||||
if (!preIncludeName)
|
||||
return;
|
||||
|
||||
char *fullPath = NULL;
|
||||
size_t size = 0;
|
||||
std::string *fullPath = fstk_FindFile(preIncludeName);
|
||||
|
||||
if (!fstk_FindFile(preIncludeName, &fullPath, &size)) {
|
||||
free(fullPath);
|
||||
if (!fullPath) {
|
||||
error("Unable to open included file '%s': %s\n", preIncludeName, strerror(errno));
|
||||
return;
|
||||
}
|
||||
|
||||
struct FileStackNamedNode *fileInfo =
|
||||
(struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + size);
|
||||
(struct FileStackNamedNode *)malloc(sizeof(*fileInfo) + fullPath->length() + 1);
|
||||
|
||||
if (!fileInfo) {
|
||||
error("Failed to alloc file info for pre-include: %s\n", strerror(errno));
|
||||
return;
|
||||
}
|
||||
fileInfo->node.type = NODE_FILE;
|
||||
strcpy(fileInfo->name, fullPath);
|
||||
free(fullPath);
|
||||
strcpy(fileInfo->name, fullPath->c_str());
|
||||
delete fullPath;
|
||||
|
||||
newContext((struct FileStackNode *)fileInfo);
|
||||
contextStack->lexerState = lexer_OpenFile(fileInfo->name);
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
#include <string.h>
|
||||
|
||||
#include "asm/fstack.hpp"
|
||||
@@ -807,13 +808,10 @@ void sect_BinaryFile(char const *s, int32_t startPos)
|
||||
if (!checkcodesection())
|
||||
return;
|
||||
|
||||
char *fullPath = NULL;
|
||||
size_t size = 0;
|
||||
FILE *f = NULL;
|
||||
std::string *fullPath = fstk_FindFile(s);
|
||||
FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : NULL;
|
||||
|
||||
if (fstk_FindFile(s, &fullPath, &size))
|
||||
f = fopen(fullPath, "rb");
|
||||
free(fullPath);
|
||||
delete fullPath;
|
||||
|
||||
if (!f) {
|
||||
if (generatedMissingIncludes) {
|
||||
@@ -881,13 +879,10 @@ void sect_BinaryFileSlice(char const *s, int32_t start_pos, int32_t length)
|
||||
if (!reserveSpace(length))
|
||||
return;
|
||||
|
||||
char *fullPath = NULL;
|
||||
size_t size = 0;
|
||||
FILE *f = NULL;
|
||||
std::string *fullPath = fstk_FindFile(s);
|
||||
FILE *f = fullPath ? fopen(fullPath->c_str(), "rb") : NULL;
|
||||
|
||||
if (fstk_FindFile(s, &fullPath, &size))
|
||||
f = fopen(fullPath, "rb");
|
||||
free(fullPath);
|
||||
delete fullPath;
|
||||
|
||||
if (!f) {
|
||||
if (generatedMissingIncludes) {
|
||||
|
||||
Reference in New Issue
Block a user