-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.C
More file actions
305 lines (274 loc) · 9.37 KB
/
Loader.C
File metadata and controls
305 lines (274 loc) · 9.37 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <fstream>
#include <string.h>
#include <ctype.h>
#include <stdint.h>
#include "Loader.h"
#include "Memory.h"
#define ADDRBEGIN 2
#define ADDREND 4
#define DATABEGIN 7
#define COMMENT 28
/*
* Loader
* opens up the file named in argv[0] and loads the
* contents into memory. If the file is able to be loaded,
* then loaded is set to true.
*/
//This method is complete and does not need to be modified.
Loader::Loader(int argc, char * argv[]) {
std::ifstream inf; //input file stream for reading from file
int lineNumber = 1;
lastAddress = -1;
loaded = false;
//if no file name given or filename badly formed, return without loading
if (argc < 2 || badFile(argv[1])) return;
inf.open(argv[1]);
//if file can't be opened, return without loading
if (!inf.is_open()) return;
std::string line;
while (getline(inf, line))
{
if (hasErrors(line))
{
std::cout << "Error on line " << std::dec << lineNumber
<< ": " << line << std::endl;
return;
}
if (hasAddress(line) && hasData(line)) loadLine(line);
lineNumber++;
}
loaded = true;
}
/*
* hasAddress
* returns true if the line passed in has an address on it.
* A line that has an address has a '0' in column 0.
* It is assumed that the address has already been checked to
* make sure it is properly formed.
*
* @param line - a string containing a line of valid input from
* a .yo file
* @return true, if the line has an address on it
* false, otherwise
*/
bool Loader::hasAddress(std::string line) {
return line[0] == '0';
}
/*
* hasData
* returns true if the line passed in has data on it.
* A line that has data does not contain a space
* at index DATABEGIN.
* It is assumed that the data has already been checked to
* make sure it is properly formed.
*
* @param line - a string containing a line of valid input from
* a .yo file
* @return true, if the line has data in it
* false, otherwise
*/
bool Loader::hasData(std::string line) {
return line[DATABEGIN] != ' ';
}
/*
* hasComment
* returns true if line is at least COMMENT in length and
* line has a | at index COMMENT.
*
* @param line - a string containing a line from a .yo file
* @return true, if the line is long enough and has a | in index COMMENT
* false, otherwise
*/
bool Loader::hasComment(std::string line) {
if (line.length() >= COMMENT &&
line.std::string::at(COMMENT) == '|') {
return true;
}
return false;
}
/*
* loadLine
* The line that is passed in contains an address and data.
* This method loads that data into memory byte by byte
* using the Memory::getInstance->putByte method.
*
* @param line - a string containing a line of valid input from
* a .yo file. The line contains an address and
* a variable number of bytes of data (at least one)
*/
void Loader::loadLine(std::string line) {
bool error = false;
int i = DATABEGIN;
int address = convert(line, ADDRBEGIN, ADDREND - ADDRBEGIN + 1);
while (line.std::string::at(i) != ' ') {
int value = convert(line, i, 2);
Memory::getInstance()->putByte(value, address, error);
i += 2;
address++;
}
}
/*
* convert
* takes "len" characters from the line starting at character "start"
* and converts them to a number, assuming they represent hex characters.
* For example, if len is 2 and line[start] is '1' and
* line[start + 1] is 'a' then this function returns 26.
* This function assumes that the line is long enough to hold the desired
* characters and that the characters represent hex values.
*
* @param line - string of characters
* @param start - starting index in line
* @param len - represents the number of characters to retrieve
*/
int32_t Loader::convert(std::string line, int32_t start, int32_t len) {
std::string subString = line.std::string::substr(start, len);
int32_t out = stoul(subString, NULL, 16);
return out;
}
/*
* hasErrors
* Returns true if the line file has errors in it and false
* otherwise.
*
* @param line - a string that contains a line from a .yo file
* @return true, if the line has errors
* false, otherwise
*/
bool Loader::hasErrors(std::string line) {
//checking for errors in a particular order can significantly
//simplify your code
//1) line is at least COMMENT characters long and contains a '|' in
// column COMMENT. If not, return true
// Hint: use hasComment
if (!hasComment(line)) return true;
//2) check whether line has an address. If it doesn't,
// return result of isSpaces (line must be all spaces up
// to the | character)
// Hint: use hasAddress and isSpaces
if (!hasAddress(line)) {
if (isSpaces(line, 0, COMMENT) == false) return true;
}
//3) return true if the address is invalid
// Hint: use errorAddress
if (errorAddr(line)) return true;
//4) check whether the line has data. If it doesn't
// return result of isSpaces (line must be all spaces from
// after the address up to the | character)
// Hint: use hasData and isSpaces
if (!hasData(line) && !isSpaces(line, DATABEGIN, COMMENT)) return true;
//5) if you get past 4), line has an address and data. Check to
// make sure the data is valid using errorData
// Hint: use errorData
int32_t numDBytes = 0;
if (errorData(line, numDBytes)) return true;
//6) if you get past 5), line has a valid address and valid data.
// Make sure that the address on this line is > the last address
// stored to (lastAddress is a private data member)
// Hint: use convert to convert address to a number and compare
// to lastAddress
if (!isSpaces(line, 0, COMMENT)) {
int address = convert(line, ADDRBEGIN, ADDREND - ADDRBEGIN + 1);
if (lastAddress >= address) return true;
lastAddress = address;
lastAddress += numDBytes - 1;
if (address + numDBytes > MEMSIZE) return true;
}
//7) Make sure that the last address of the data to be stored
// by this line doesn't exceed the memory size
// Hint: use numDBytes as set by errorData, MEMSIZE in Memory.h,
// and addr returned by convert
// if control reaches here, no errors found
return false;
}
/*
* errorData
* Called when the line contains data. It returns true if the data
* in the line is invalid.
*
* Valid data consists of characters in the range
* '0' .. '9','a' ... 'f', and 'A' .. 'F' (valid hex digits).
* The data digits start at index DATABEGIN.
* The hex digits come in pairs, thus there must be an even number of them.
* In addition, the characters after the last hex digit up to the
* '|' character at index COMMENT must be spaces.
* If these conditions are met, errorData returns false, else errorData
* returns true.
*
* @param line - input line from the .yo file
* @return numDBytes is set to the number of data bytes on the line
*/
bool Loader::errorData(std::string line, int32_t & numDBytes) {
int length = 0;
for (int i = DATABEGIN; i < COMMENT; i++) {
if (line[i] != ' ') length++;
}
if (length % 2 != 0) return true;
for (int i = DATABEGIN; i < DATABEGIN + length; i++) {
if (!isxdigit(line[i])) return true;
}
if (!isSpaces(line, DATABEGIN + length, COMMENT)) return true;
numDBytes = length / 2;
return false;
}
/*
* errorAddr
* This function is called when the line contains an address in order
* to check whether the address is properly formed. An address must be of
* this format: 0xHHH: where HHH are valid hex digits.
*
* @param line - input line from a .yo input file
* @return true if the address is not properly formed and false otherwise
*/
bool Loader::errorAddr(std::string line) {
if (isSpaces(line, 0, COMMENT)) return false;
if (line[0] != '0') return true;
if (line[1] != 'x') return true;
if (line[5] != ':') return true;
if (line[6] != ' ') return true;
for (int i = 2; i < 5; i++) {
if (isxdigit(line[i]) == false) return true;
}
return false;
}
/*
* isSpaces
* This function checks that characters in the line starting at
* index start and ending at index end are all spaces.
* This can be used to check for errors
*
* @param line - string containing a line from a .yo file
* @param start - starting index
* @param end - ending index
* @return true, if the characters in index from start to end are spaces
* false, otherwise
*/
bool Loader::isSpaces(std::string line, int32_t start, int32_t end) {
for (int i = start; i < end; i++) {
if (line[i] != ' ') return false;
}
return true;
}
/*
* isLoaded
* getter for the private loaded data member
*/
bool Loader::isLoaded() {
return loaded;
}
/*
* badFile
* returns true if the name of the file passed in is an improperly
* formed .yo filename. A properly formed .yo file name is at least
* four characters in length and ends with a .yo extension.
*
* @return true - if the filename is improperly formed
* false - otherwise
*/
bool Loader::badFile(std::string filename) {
int strLen = filename.length();
std::string ext = ".yo";
std::string subString = filename.std::string::substr(strLen - 3, 3);
if ((ext.compare(subString) == 0) && (strLen >= 4)) return false;
return true;
}