-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean.cpp
118 lines (108 loc) · 3.32 KB
/
clean.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
#include <iostream>
#include <string>
#include <ctime>
#include <vector>
#include <fstream>
using namespace std;
#define NUMBER "0123456789"
#define FILENAME "/home/adam/bin/clean_conf.txt"
void usage()
{
cerr << "Usage: clean [item]" << endl;
cerr << "Running with no argument displays what needs cleaning." << endl;
cerr << "Running with an argument indicates that thing has been cleaned." << endl;
cerr << "Running:" << endl;
cerr << "clean all" << endl;
cerr << "indicates that everything which needed cleaning has been cleaned." << endl;
exit(1);
}
void parseLine(string line, string& task, int& freq, int& lastDayDone)
{
string freqStr, lastDayDoneStr;
size_t freqLoc = line.find_first_of(NUMBER);
if (freqLoc == string::npos) {
cerr << "Error at " << line << endl;
exit(2);
}
size_t lastDayDoneLoc = line.find_first_of(" \t", freqLoc);
if (lastDayDoneLoc == string::npos) {
cerr << "Error at " << line << endl;
exit(3);
}
lastDayDoneLoc = line.find_first_of(NUMBER, lastDayDoneLoc);
freqStr = line.substr(freqLoc, lastDayDoneLoc - freqLoc);
lastDayDoneStr = line.substr(lastDayDoneLoc);
task = line.substr(0, freqLoc);
freq = stoi(freqStr);
lastDayDone = stoi(lastDayDoneStr);
}
int getDayOfYear()
{
time_t timer;
time(&timer);
struct tm * timeinfo = localtime(&timer);
return timeinfo->tm_yday;
}
bool needsCleaning(int dayOfYear, int freq, int lastDayDone)
{
int effectiveDayOfYear;
if (lastDayDone > dayOfYear) {
effectiveDayOfYear = dayOfYear + 365;
} else {
effectiveDayOfYear = dayOfYear;
}
return lastDayDone + freq < effectiveDayOfYear;
}
int main(int argc, char** argv)
{
if (argc > 2) {
usage();
}
string arg = "";
if (argc == 2) {
arg = argv[1];
}
ifstream file(FILENAME);
//Since we want to save the output back to the same file, keep the output in memory until the end
vector<string> output;
std::string line = "";
int dayOfYear = getDayOfYear();
while (std::getline(file, line)) {
if (line.find_first_of(NUMBER) == string::npos) {
//Perhaps a comment or an otherwise invalid line in the input
if (arg != "") {
output.push_back(line);
}
} else {
string task;
int freq, lastDayDone;
parseLine(line, task, freq, lastDayDone);
if (arg == "") {
if (needsCleaning(dayOfYear, freq, lastDayDone)) {
cout << task << std::endl;
}
} else {
if (arg == "all") {
if (needsCleaning(dayOfYear, freq, lastDayDone)) {
//Indicate that the task has been done and save it to the file
lastDayDone = dayOfYear;
}
} else {
if (arg == task.substr(0, arg.length())) {
//Indicate that the task has been done and save it to the file
lastDayDone = dayOfYear;
}
}
string toOutput = task + to_string(freq) + " " + to_string(lastDayDone);
output.push_back(toOutput);
}
}
}
file.close();
if (arg != "") {
ofstream outFile(FILENAME);
for (string line : output) {
outFile << line << endl;
}
}
}