-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
76 lines (60 loc) · 1.44 KB
/
Copy pathmain.cpp
File metadata and controls
76 lines (60 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "Stack.h"
#include "CPU.h"
size_t ReadCommands(const char* name, int array[]);
const int MAX_NUM_OF_COMMANDS = 3500;
const int MAX_FILE_NAME = 256;
const int VERSION = 1;
const int NAME = 'PPA';
int main() {
CPU cpu;
CPU_ctor(&cpu);
int com_array[MAX_NUM_OF_COMMANDS] = {};
char fileName[MAX_FILE_NAME] = "";
printf("Enter the program-file location>\n");
scanf("%s", fileName);
size_t size = ReadCommands(fileName, com_array);
if ((size == 0) || errno) {
printf("Some problems with file");
perror("");
return 1;
}
if (com_array[0] != NAME)
{
printf("It isn't a program file");
return 1;
}
if(com_array[1] != VERSION)
{
printf("Versions of program and CPU don't maches");
return 1;
}
if(!startWorking(&cpu, com_array, size))
{
printf("Program has been successfully finished");
}
else perror("Program hasn't been successfully finished");
CPU_dtor(&cpu);
return 0;
}
size_t ReadCommands(const char* name, int array[])
{
FILE* in = fopen(name, "rb");
if (in == NULL)
{
errno = ENOENT;
return 0;
}
fseek(in, 0, SEEK_END);
size_t length = 0;
length = ftell(in);
if (length == 0)
{
errno = EIO;
fclose(in);
return 0;
}
rewind(in);
size_t size = 0;
size = fread(array, sizeof(array[0]), length, in);
return size;
}