-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHistory.cpp
More file actions
58 lines (51 loc) · 1.49 KB
/
History.cpp
File metadata and controls
58 lines (51 loc) · 1.49 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
#include "History.h"
using namespace std;
deque<string> HistoryAdapter::history;
string HistoryAdapter::myfilename = "";
void HistoryAdapter::setFile(string filename) {
myfilename = filename;
history.clear(); //clears history when the file is changed
string mystr;
ifstream hfile;
hfile.open(filename.c_str(), ios::in);
while (getline(hfile, mystr)) {
std::size_t myRpos;
myRpos = mystr.find("\r");
if (myRpos != string::npos) {
mystr = mystr.substr(0, myRpos); //offers windows compatibility, removing the \r at the end of the line
}
history.push_back(mystr);
}
hfile.close();
}
void HistoryAdapter::addOperation(string mystr) {
history.push_front(mystr);
syncHistoryFile();
}
void HistoryAdapter::printRecentHistory(int items) {
int i=0;
cout << "========== System History ==========" << endl;
if (items <= history.size()) {
for (;i<items;i++) {
cout << history[i] << endl;
}
cout << "====================================" << endl << endl;
return;
}
else {
for (;i<history.size();i++) {
cout << history[i] << endl;
}
cout << "====================================" << endl << endl;
return;
}
}
void HistoryAdapter::syncHistoryFile() {
ofstream hfile;
hfile.open(myfilename.c_str(), ios::out | ios::trunc);
int i;
for (i=0;i<history.size();i++) {
hfile << history[i] << endl;
}
hfile.close();
}