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