From 964572b5e93c7cb464686f19ddbe3e9d315f391b Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 18 Oct 2024 11:22:56 +1100 Subject: [PATCH] I think the file paths are fixed --- repl/main.c | 73 +++++++++++------ tests/standalone/gdb_init | 7 -- tests/standalone/platform_behaviours.c | 108 ++++++++++++++++++------- 3 files changed, 128 insertions(+), 60 deletions(-) diff --git a/repl/main.c b/repl/main.c index 509a77b..b0d6424 100644 --- a/repl/main.c +++ b/repl/main.c @@ -1,12 +1,27 @@ #include "toy.h" -#include "toy_print.h" #include #include #include //utilities +#define APPEND(dest, src) \ + strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1); + +#if defined(_WIN32) || defined(_WIN64) + #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '/' ? '\\' : str[i]; +#else + #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '\\' ? '/' : str[i]; +#endif + unsigned char* readFile(char* path, int* size) { + //BUGFIX: fix the path based on platform - it might be slower, but it's better than dealing with platform crap + int pathLength = strlen(path); + char realPath[pathLength + 1]; + strncpy(realPath, path, pathLength); + realPath[pathLength] = '\0'; + FLIPSLASH(realPath); + //open the file FILE* file = fopen(path, "rb"); if (file == NULL) { @@ -41,14 +56,23 @@ unsigned char* readFile(char* path, int* size) { } int getFilePath(char* dest, const char* src) { - //extract the directory from src, and store it in dest -#if defined(_WIN32) || defined(_WIN64) - char* p = strrchr(src, '\\'); -#else - char* p = strrchr(src, '/'); -#endif + char* p = NULL; + + //find the last slash, regardless of platform + p = strrchr(src, '\\'); + if (p == NULL) { + p = strrchr(src, '/'); + } + if (p == NULL) { + int len = strlen(src); + strncpy(dest, src, len); + return len; + } - int len = p != NULL ? p - src + 1 : 0; + //determine length of the path + int len = p - src + 1; + + //copy to the dest strncpy(dest, src, len); dest[len] = '\0'; @@ -56,34 +80,31 @@ int getFilePath(char* dest, const char* src) { } int getFileName(char* dest, const char* src) { - //extract the directory from src, and store it in dest -#if defined(_WIN32) || defined(_WIN64) - char* p = strrchr(src, '\\'); -#else - char* p = strrchr(src, '/'); -#endif + char* p = NULL; - //if we're not at the end of the string, skip the slash - if (*p != '\0') { - p++; + //find the last slash, regardless of platform + p = strrchr(src, '\\'); + if (p == NULL) { + p = strrchr(src, '/'); } + if (p == NULL) { + int len = strlen(src); + strncpy(dest, src, len); + return len; + } + + p++; //skip the slash + //determine length of the file name int len = strlen(p); + + //copy to the dest strncpy(dest, p, len); dest[len] = '\0'; return len; } -#define APPEND(dest, src) \ - strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1); - -#if defined(_WIN32) || defined(_WIN64) - #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '/' ? '\\' : str[i]; -#else - #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '\\' ? '/' : str[i]; -#endif - //handle command line arguments typedef struct CmdLine { bool error; diff --git a/tests/standalone/gdb_init b/tests/standalone/gdb_init index 8475b20..043b1d8 100644 --- a/tests/standalone/gdb_init +++ b/tests/standalone/gdb_init @@ -1,8 +1 @@ set breakpoint pending on - -break strlen - -command 1 - bt - continue -end diff --git a/tests/standalone/platform_behaviours.c b/tests/standalone/platform_behaviours.c index 0237324..70db8e9 100644 --- a/tests/standalone/platform_behaviours.c +++ b/tests/standalone/platform_behaviours.c @@ -1,15 +1,76 @@ #include +#include #include -int getFilePath(char* dest, const char* src) { - //extract the directory from src, and store it in dest +//utilities +#define APPEND(dest, src) \ + strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1); + #if defined(_WIN32) || defined(_WIN64) - char* p = strrchr(src, '\\'); + #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '/' ? '\\' : str[i]; #else - char* p = strrchr(src, '/'); + #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '\\' ? '/' : str[i]; #endif - int len = p != NULL ? p - src + 1 : 0; +unsigned char* readFile(char* path, int* size) { + //BUGFIX: fix the path based on platform - it might be slower, but it's better than dealing with platform crap + int pathLength = strlen(path); + char realPath[pathLength + 1]; + strncpy(realPath, path, pathLength); + realPath[pathLength] = '\0'; + FLIPSLASH(realPath); + + //open the file + FILE* file = fopen(path, "rb"); + if (file == NULL) { + *size = -1; //missing file error + return NULL; + } + + //determine the file's length + fseek(file, 0L, SEEK_END); + *size = ftell(file); + rewind(file); + + //make some space + unsigned char* buffer = malloc(*size + 1); + if (buffer == NULL) { + fclose(file); + return NULL; + } + + //read the file + if (fread(buffer, sizeof(unsigned char), *size, file) < *size) { + fclose(file); + *size = -2; //singal a read error + return NULL; + } + + buffer[(*size)++] = '\0'; + + //clean up and return + fclose(file); + return buffer; +} + +int getFilePath(char* dest, const char* src) { + char* p = NULL; + + //find the last slash, regardless of platform + p = strrchr(src, '\\'); + if (p == NULL) { + p = strrchr(src, '/'); + } + if (p == NULL) { + int len = strlen(src); + strncpy(dest, src, len); + return len; + } + + //determine length of the path + int len = p - src + 1; + + //copy to the dest strncpy(dest, src, len); dest[len] = '\0'; @@ -17,34 +78,31 @@ int getFilePath(char* dest, const char* src) { } int getFileName(char* dest, const char* src) { - //extract the directory from src, and store it in dest -#if defined(_WIN32) || defined(_WIN64) - char* p = strrchr(src, '\\'); -#else - char* p = strrchr(src, '/'); -#endif + char* p = NULL; - //if we're not at the end of the string, skip the slash - if (*p != '\0') { - p++; + //find the last slash, regardless of platform + p = strrchr(src, '\\'); + if (p == NULL) { + p = strrchr(src, '/'); + } + if (p == NULL) { + int len = strlen(src); + strncpy(dest, src, len); + return len; } + p++; //skip the slash + + //determine length of the file name int len = strlen(p); + + //copy to the dest strncpy(dest, p, len); dest[len] = '\0'; return len; } -#define APPEND(dest, src) \ - strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1); - -#if defined(_WIN32) || defined(_WIN64) - #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '/' ? '\\' : str[i]; -#else - #define FLIPSLASH(str) for (int i = 0; str[i]; i++) str[i] = str[i] == '\\' ? '/' : str[i]; -#endif - int main() { //check the platform printf("Platform: "); @@ -66,7 +124,6 @@ int main() { { char src[256] = "../folder/file.txt"; char dest[256]; - FLIPSLASH(src); getFilePath(dest, src); printf("Path: %s\n", dest); } @@ -74,7 +131,6 @@ int main() { { char src[256] = "../folder/file.txt"; char dest[256]; - FLIPSLASH(src); getFileName(dest, src); printf("Name: %s\n", dest); } @@ -82,10 +138,8 @@ int main() { { char src[256] = "../folder/file.txt"; char dest[256]; - FLIPSLASH(src); getFilePath(dest, src); APPEND(dest, "target.txt"); - FLIPSLASH(dest); printf("Target: %s\n", dest); }