-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInvokeBox.h
More file actions
96 lines (78 loc) · 1.53 KB
/
InvokeBox.h
File metadata and controls
96 lines (78 loc) · 1.53 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef INVOKEBOX_H
#define INVOKEBOX_H
#include "BoxInfo.h"
#include "ProgramMonitor.h"
class InvokeBox {
public:
void invoke(char *boxFilePath);
Bcode getPc();
void read(char *buffer, Size size);
protected:
Size fetchSize(char *boxFilePath);
Bcode codeSeg;
Bcode programCounter;
};
Bcode InvokeBox::getPc()
{
return programCounter - 1;
}
void InvokeBox::read(char *buffer, Size size)
{
if (size == 0)
{
strcpy(buffer, programCounter);
programCounter += (strlen(buffer) + 1);
}
else
{
memcpy(buffer, programCounter, size);
programCounter += size;
}
}
Size InvokeBox::fetchSize(char *boxFilePath)
{
Size size;
FILE *fp;
fp = fopen(boxFilePath, "r+b");
if (!fp)
{
SETERR(ERROR_OPEN);
return 0;
}
fseek(fp, 0, SEEK_END);
size = ftell(fp);
fclose(fp);
if (size == 0)
{
SETERR(EMPTY_FILE);
}
if (size > MAXFILESIZE)
{
SETERR(LARGE_FILE_ERR);
}
return size;
}
void InvokeBox::invoke(char *boxFilePath)
{
FILE *fp;
Size readSize = 0;
Size fileSize = fetchSize(boxFilePath);
if (NOK)
{
return;
}
programCounter = codeSeg = (Bcode)calloc(fileSize, sizeof(Byte));
if (!codeSeg)
{
SETERR(ERR_ALLOC_CS);
return;
}
fp = fopen(boxFilePath, "r+b");
readSize = fread(codeSeg, sizeof(Byte), fileSize, fp);
if (readSize != fileSize)
{
SETERR(ERRORFILE_READ_BOX);
}
fclose(fp);
}
#endif