mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-04-15 14:54:07 +10:00
After a few hours struggling with the linker, I've got the main.c file running correctly, with caveats: - must be executed from out/ - only building on linux for the moment - no tests written yet I will write some CI jobs to see if the repl works eventually.
53 lines
986 B
C
53 lines
986 B
C
#include "toy_memory.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
unsigned char* readFile(const char* path, int* size) {
|
|
//open the file
|
|
FILE* file = fopen(path, "rb");
|
|
if (file == NULL) {
|
|
return NULL;
|
|
}
|
|
|
|
//determine the file's length
|
|
fseek(file, 0L, SEEK_END);
|
|
*size = ftell(file);
|
|
rewind(file);
|
|
|
|
//make some space
|
|
unsigned char* buffer = TOY_ALLOCATE(unsigned char, *size);
|
|
if (buffer == NULL) {
|
|
fclose(file);
|
|
return NULL;
|
|
}
|
|
|
|
//
|
|
if (fread(buffer, sizeof(unsigned char), *size, file) < *size) {
|
|
fclose(file);
|
|
*size = -1; //singal a read error
|
|
return NULL;
|
|
}
|
|
|
|
fclose(file);
|
|
return buffer;
|
|
}
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
int size = 0;
|
|
unsigned char* buffer = readFile("../repl/main.c", &size); //for now, just grab the main.c file as a test
|
|
|
|
if (buffer == NULL) {
|
|
fprintf(stderr, "Failed to open the file\n");
|
|
}
|
|
|
|
if (size < 0) {
|
|
fprintf(stderr, "Failed to read the file\n");
|
|
}
|
|
|
|
TOY_FREE_ARRAY(unsigned char, buffer, size);
|
|
|
|
printf("All good\n");
|
|
return 0;
|
|
}
|