Skip to content

Commit

Permalink
I think the file paths are fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Ratstail91 committed Oct 18, 2024
1 parent 1721f3d commit 964572b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 60 deletions.
73 changes: 47 additions & 26 deletions repl/main.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#include "toy.h"
#include "toy_print.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//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) {
Expand Down Expand Up @@ -41,49 +56,55 @@ 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';

return len;
}

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;
Expand Down
7 changes: 0 additions & 7 deletions tests/standalone/gdb_init
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
set breakpoint pending on

break strlen

command 1
bt
continue
end
108 changes: 81 additions & 27 deletions tests/standalone/platform_behaviours.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,108 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

return len;
}

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: ");
Expand All @@ -66,26 +124,22 @@ int main() {
{
char src[256] = "../folder/file.txt";
char dest[256];
FLIPSLASH(src);
getFilePath(dest, src);
printf("Path: %s\n", dest);
}

{
char src[256] = "../folder/file.txt";
char dest[256];
FLIPSLASH(src);
getFileName(dest, src);
printf("Name: %s\n", dest);
}

{
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);
}

Expand Down

0 comments on commit 964572b

Please sign in to comment.