Fix the damn text file loading function

This commit is contained in:
Joseph Ferano 2023-10-26 14:49:44 +07:00
parent f548bdebb9
commit 064963c685

View File

@ -25,20 +25,32 @@ void checkShader(unsigned int shader, int statusFlag, char* actionName) {
} }
} }
char* loadText(char* path) { char *loadText(char* path) {
char* buffer = NULL; char *buffer = NULL;
long length; FILE *f = fopen(path, "r");
FILE* f = fopen(path, "rb"); if (f == NULL) {
if (f) { return NULL;
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = calloc(length, sizeof(char));
if (buffer) {
fread(buffer, 1, length, f);
}
fclose(f);
} }
if (fseek(f, 0, SEEK_END) != 0) {
return NULL;
}
i16 length = ftell(f);
if (length == -1) {
return NULL;
}
buffer = malloc((length + 1) * sizeof(char));
if (fseek(f, 0, SEEK_SET) != 0) {
return NULL;
}
usize newLen = fread(buffer, sizeof(char), length, f);
if (ferror(f) != 0) {
return NULL;
} else {
buffer[newLen] = '\0';
}
fclose(f);
return buffer; return buffer;
} }