From 8b3770df801196d15a3b95575b803f2ab81d5a84 Mon Sep 17 00:00:00 2001 From: nils polek Date: Sun, 3 Dec 2023 16:10:33 +0100 Subject: [PATCH] added codeReader and consts.c --- codeReader.c | 32 ++++++++++++++++++++++++++++++++ consts.c | 7 +++++++ 2 files changed, 39 insertions(+) create mode 100644 codeReader.c create mode 100644 consts.c diff --git a/codeReader.c b/codeReader.c new file mode 100644 index 0000000..ca66ddb --- /dev/null +++ b/codeReader.c @@ -0,0 +1,32 @@ +#ifndef CODEREADER +#define CODEREADER +#include "consts.c" +#include +#include +#include "program.c" +void fromFile(char *path, struct program program){ + unsigned int countInstructions; + unsigned int staticVars; + FILE *fptr; + fptr = fopen(path, "r+b"); + if(fptr == NULL) { + printf("Error: cannot open code file %s", path); + exit(EXIT_FAILURE); + } + unsigned int buffer[4]; + fread(buffer, 4, 4, fptr); + // Check file type + if(buffer[0] != 0x46424A4E){ + printf("Error: wrong file type"); + exit(EXIT_FAILURE); + } + if(buffer[1] != VERSION){ + printf("Error: wrong version"); + } + countInstructions = buffer[2]; + staticVars = buffer[3]; + unsigned int instBuffer[countInstructions]; + fread(instBuffer, 4, countInstructions, fptr); + copyToProgram(instBuffer,countInstructions,program); +} +#endif /* ifdef CODEREADER */ diff --git a/consts.c b/consts.c new file mode 100644 index 0000000..efb7fc7 --- /dev/null +++ b/consts.c @@ -0,0 +1,7 @@ +#ifndef CONSTS +#define CONSTS +#define VERSION 2 + +#endif /* ifndef CONSTS +#define CONSTS +#define VERSION 2; */