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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user