created basic simulator
This commit is contained in:
parent
8c02a9e950
commit
5a06b2bc0b
12 changed files with 541 additions and 0 deletions
44
ass2/FileDevice.cpp
Normal file
44
ass2/FileDevice.cpp
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include "FileDevice.h"
|
||||
#include <iostream>
|
||||
|
||||
FileDevice::FileDevice(const std::string& fname) : filename(fname) {
|
||||
file.open(filename, std::ios::in | std::ios::out | std::ios::binary);
|
||||
|
||||
if (!file.is_open()) {
|
||||
file.clear();
|
||||
file.open(filename, std::ios::out | std::ios::binary);
|
||||
file.close();
|
||||
file.open(filename, std::ios::in | std::ios::out | std::ios::binary);
|
||||
}
|
||||
}
|
||||
|
||||
FileDevice::~FileDevice() {
|
||||
if (file.is_open()) {
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
bool FileDevice::test() const {
|
||||
return file.is_open() && file.good();
|
||||
}
|
||||
|
||||
uint8_t FileDevice::read() {
|
||||
char c;
|
||||
if (file.is_open()) {
|
||||
if (file.get(c)) {
|
||||
uint8_t byte = (uint8_t)c;
|
||||
return byte;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void FileDevice::write(uint8_t value) {
|
||||
if (file.is_open()) {
|
||||
char c = (char)value;
|
||||
file.put(c);
|
||||
file.flush();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue