added codeReader and consts.c
This commit is contained in:
parent
dc33e46e24
commit
8b3770df80
32
codeReader.c
Normal file
32
codeReader.c
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef CODEREADER
|
||||||
|
#define CODEREADER
|
||||||
|
#include "consts.c"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#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 */
|
||||||
Loading…
x
Reference in New Issue
Block a user