/
driver.cpp
144 lines (128 loc) · 3.8 KB
/
driver.cpp
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
#include <random>
#include <chrono>
#include <iostream>
#include <cstring>
#include <sstream>
#include <array>
#include <string>
#include <cstdint>
#include <stdexcept>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include "myMemory.h"
#include "pageTable.h"
#include "memMan.h"
#include "simDisk.h"
#include "change.h"
#include "status.h"
#include "driver.h"
using std::knuth_b; using std::uniform_int_distribution;
using std::cout; using std::endl; using std::cerr; using std::ostream;
using std::flush; using std::streambuf; using std::ios_base; using std::clog;
using std::strcpy;
using std::ostringstream;
using std::array;
using std::string;
using std::range_error;
using std::strtoul;
using std::isdigit;
using namespace std::chrono;
using namespace MemModel;
using namespace Setup;
/////////////////////////////////
// Public member functions
/////////////////////////////////
class MemMan;
Driver::Driver(int ac, char *av[])
{
init(ac, av);
}
Driver::~Driver()
{
clog << flush;
clog.rdbuf(backupBuf);
if (logfile.is_open())
logfile.close();
}
void Driver::init(int ac, char *av[])
{
rdCLArgs(ac, av);
if (help) {
displayHelp();
exit(0);
}
logfile.open("output.txt", ios_base::out);
streambuf *fileStrmBuf = logfile.rdbuf();
backupBuf = clog.rdbuf();
clog.rdbuf(fileStrmBuf);
wrtHeader(ac, av, "OUTPUT", clog);
wrtHeader(ac, av, "ERROR OUTPUT", cerr);
}
/////////////////////////////////
// Private member functions
/////////////////////////////////
// Called by: main()
void Driver::rdCLArgs(int ac, char *av[])
{
while (--ac && !help) {
char cla = '\0';
if ((*++av)[0] == '-') {
cla = (*av)[1];
switch (cla) {
case 'v':
verbose = true;
break;
case 't': // use default seed, print block number of each change
test = true;
break;
case 's': // use subsequent arg for seed
if (!isdigit((*++av)[0])) { // input error
help = true;
--ac;
break;
}
theSeed = strtoul(*av, nullptr, 0);
--ac;
break;
case 'L':
case 'l':
longRun = true;
break;
case 'h':
help = true;
break;
default:
cerr << "ERROR: Bad command line argument" << '\n';
}
}
}
}
// Write header line to output
// Called by: main()
void Driver::wrtHeader(int ac, char *av[], const char *tag, ostream &os) const
{
os << tabs(0) << tag << ": ";
for (int i = 0; i != ac; ++i) {
os << *av++ << ' ';
}
time_t tt = system_clock::to_time_t(system_clock::now());
os << ": " << ctime(&tt);
}
void Driver::displayHelp() const
{
cout << "\n\tmemModel options:\n\n";
cout << "\t-h Help Print this help and exit.\n\n"
<< "\t-l\n"
<< "\t-L Long run Run the program long enough to make the\n"
<< "\t journal file wrap around.\n\n"
<< "\t-s Seed Seed the random number generator with the\n"
<< "\t non-negative integer that appears as the next\n"
<< "\t command line argument.\n\n"
<< "\t-t Test Use the default seed (or the argument to -s,\n"
<< "\t if given) for the random number generator.\n"
<< "\t Make the first line of each output change hold\n"
<< "\t its block number.\n\n"
<< "\t-v Verbose Send some extra debugging information to stdout."
<< "\n" << endl;
}