-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexsimio.hpp
More file actions
57 lines (48 loc) · 1.3 KB
/
hexsimio.hpp
File metadata and controls
57 lines (48 loc) · 1.3 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
#ifndef HEX_SIM_IO_HPP
#define HEX_SIM_IO_HPP
#include <fstream>
#include <istream>
#include <ostream>
#include <string>
#include <vector>
namespace hex {
class HexSimIO {
std::istream ∈
std::ostream &out;
std::array<std::fstream, 8> fileIO;
std::array<bool, 8> connected;
public:
HexSimIO(std::istream &in, std::ostream &out)
: in(in), out(out),
connected({false, false, false, false, false, false, false, false}) {}
/// Output a character to ostream or a file.
void output(char value, int stream) {
if (stream < 256) {
out << value;
} else {
size_t index = (stream >> 8) & 7;
if (!connected[index]) {
fileIO[index].open(std::string("simout") + std::to_string(index),
std::fstream::out);
connected[index] = true;
}
fileIO[index].put(value);
}
}
/// Input a character from stdin or a file.
char input(int stream) {
if (stream < 256) {
return in.get();
} else {
size_t index = (stream >> 8) & 7;
if (!connected[index]) {
fileIO[index].open(std::string("simin") + std::to_string(index),
std::fstream::in);
connected[index] = true;
}
return fileIO[index].get();
}
}
};
} // End namespace hex.
#endif // HEX_SIM_IO_HPP