-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileHandler.h
98 lines (81 loc) · 1.92 KB
/
FileHandler.h
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
97
98
#ifndef _VM_CODE_HANDLER_H_
#define _VM_CODE_HANDLER_H_
#include "DefConstTrans.h"
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
/**
* \brief The FileHandler class processes a file so it can be accessed one byte at a time.
*/
class FileHandler{
std::vector<byte> code;
unsigned int cc; //code counter
public:
FileHandler():code(std::vector<byte>()), cc(0){}
/**
* Initialize a FileHandler to read from file specified by given path.
*/
FileHandler(const std::string & file_path):code(std::vector<byte>()), cc(0){
feed_from_file(file_path);
}
/**
* Returns size of file (in bytes).
*/
unsigned int size() const {
return code.size();
}
/**
* Returns the position of the next byte.
*/
unsigned int get_cc() const {
return cc;
}
/**
* Returns byte at specified position.
*/
byte & operator[](const unsigned int & i){
if(i >= code.size()){
fprintf(stderr, "Error: Accessing byte out of bound.\n");
}
return code[i];
}
/**
* Returns next byte(s), number of bytes is specified by user as U1, U2 or U4,
* as specified in the JVM specification.
*/
unsigned int fetch(ClassUnit U = U1){
if(cc > code.size()-U){
std::cerr << "Error: Accessing byte out of bound." << std::endl;
}
unsigned int ret = 0;
for(int i = 0; i < U; i++){
ret = ret << 8;
ret |= code[cc++];
}
return ret;
}
/**
* A FileHandler can be fed a stream of bytes to be interpreted
* as a file, one at a time.
*/
inline void feed(const byte & b){
code.push_back(b);
}
/**
* A FileHandler can be initialized from file specified by path.
*/
void feed_from_file(const std::string & file_path){
byte b;
FILE * source_file = fopen(file_path.c_str(), "r");
if(NULL == source_file){
std::string err = "Error while loading '"+file_path+"'";
perror(err.c_str());
}
while(fread(&b, sizeof(byte), 1, source_file)){
feed(b);
}
}
};
#endif
//_VM_CODE_HANDLER_H_