-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
167 lines (160 loc) · 8.08 KB
/
main.cpp
File metadata and controls
167 lines (160 loc) · 8.08 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <cctype>
#include "filesystem.hpp"
static std::string trim(const std::string& s) {
size_t i = 0, j = s.size();
while (i < j && std::isspace((unsigned char)s[i])) ++i;
while (j > i && std::isspace((unsigned char)s[j-1])) --j;
return s.substr(i, j - i);
}
static void print_help() {
std::cout << "\n---------------------- HELP -------------------------\n";
std::cout << "CREATE <filename> : Create a new file.\n";
std::cout << "INSERT <filename> <text> : Insert text into file.\n";
std::cout << "UPDATE <filename> <text> : Replace entire content of file.\n";
std::cout << "SNAPSHOT <filename> <message> : Create snapshot of current version.\n";
std::cout << "ROLLBACK <filename> <versionID> : Roll back file by given steps.\n";
std::cout << "HISTORY <filename> : Show all snapshots of the file.\n";
std::cout << "RECENT_FILES [num] : Show the most recently modified files.\n";
std::cout << "BIGGEST_TREES [num] : Show files with most versions.\n";
std::cout << "READ <filename> : Show current content of file.\n";
std::cout << "HELP : Show this help message.\n";
std::cout << "EXIT : Quit the program.\n";
std::cout << "-----------------------------------------------\n\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout << "-----------------------------------------------\n";
std::cout << "Type HELP to see available commands.\n";
std::cout << "-----------------------------------------------\n";
FileSystem fs;
std::string line;
while (std::getline(std::cin, line)) {
line = trim(line);
if (line.empty()) continue;
std::istringstream iss(line);
std::string cmd;
iss >> cmd;
for (char& c : cmd) c = std::toupper((unsigned char)c);
if (cmd == "HELP"){
print_help();
} else if (cmd == "CREATE") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
fs.create(filename);
std::cout << "-----------------------------------------------\n";
std::cout << "Created " << filename << "\n";
std::cout << "-----------------------------------------------\n";
} else if (cmd == "READ") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
std::cout << "-----------------------------------------------\n";
std::cout << filename << " is as follows: " << "\n";
std::cout << "-----------------------------------------------\n";
fs.read(filename);
std::cout << "-----------------------------------------------\n";
} else if (cmd == "INSERT") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
std::string rest; std::getline(iss, rest);
rest = trim(rest);
fs.insert(filename, rest);
std::cout << "-----------------------------------------------\n";
std::cout << "Inserted message into " << filename << "\n";
std::cout << "-----------------------------------------------\n";
} else if (cmd == "UPDATE") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
std::string rest; std::getline(iss, rest);
rest = trim(rest);
fs.update(filename, rest);
std::cout << "-----------------------------------------------\n";
std::cout << filename << " updated succesfully."<<"\n";
std::cout << "-----------------------------------------------\n";
} else if (cmd == "SNAPSHOT") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
std::string rest; std::getline(iss, rest);
rest = trim(rest);
fs.snapshot(filename, rest);
int vid = fs.get_active_version_id(filename);
std::cout << "-----------------------------------------------\n";
std::cout << "Snapshot for " << filename << " saved with version ID " << vid << "\n";
std::cout << "-----------------------------------------------\n";
} else if (cmd == "ROLLBACK") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
std::string maybe_id;
iss >> maybe_id;
if (maybe_id.empty()) {
fs.rollback(filename, /*has_id*/false, /*id*/-1);
std::cout << "-----------------------------------------------\n";
std::cout << "Rolled back " << filename << " successfully."<<"\n";
std::cout << "-----------------------------------------------\n";
} else {
bool ok = true;
for (char c : maybe_id) if (!std::isdigit((unsigned char)c) && !(c=='-'||c=='+')) { ok = false; break; }
if (!ok) {
std::cout << "ERROR: Invalid version id\n";
} else {
int id = std::stoi(maybe_id);
fs.rollback(filename, /*has_id*/true, id);
std::cout << "-----------------------------------------------\n";
std::cout << "Rolled back " << filename << " successfully."<<"\n";
std::cout << "-----------------------------------------------\n";
}
}
} else if (cmd == "HISTORY") {
std::string filename; iss >> filename;
if (filename.empty()) { std::cout << "ERROR: Missing filename\n"; continue; }
std::cout << "-----------------------------------------------\n";
fs.history(filename);
std::cout << "-----------------------------------------------\n";
} else if (cmd == "RECENT_FILES" || cmd == "RECENT") {
int limit = -1;
if (cmd == "RECENT") {
std::string next; iss >> next;
for (char& c : next) c = std::toupper((unsigned char)c);
if (next != "FILES") {
std::cout << "ERROR: Unknown command\n";
continue;
}
std::string maybe_n; iss >> maybe_n;
if (!maybe_n.empty()) limit = std::stoi(maybe_n);
} else {
std::string maybe_n; iss >> maybe_n;
if (!maybe_n.empty()) limit = std::stoi(maybe_n);
}
std::cout << "-----------------------------------------------\n";
fs.recent_files(limit);
std::cout << "-----------------------------------------------\n";
} else if (cmd == "BIGGEST_TREES" || cmd == "BIGGEST") {
int limit = -1;
if (cmd == "BIGGEST") {
std::string next; iss >> next;
for (char& c : next) c = std::toupper((unsigned char)c);
if (next != "TREES") {
std::cout << "ERROR: Unknown command\n";
continue;
}
std::string maybe_n; iss >> maybe_n;
if (!maybe_n.empty()) limit = std::stoi(maybe_n);
} else {
std::string maybe_n; iss >> maybe_n;
if (!maybe_n.empty()) limit = std::stoi(maybe_n);
}
std::cout << "-----------------------------------------------\n";
fs.biggest_trees(limit);
std::cout << "-----------------------------------------------\n";
} else if (cmd == "EXIT") {
break;
} else {
std::cout << "ERROR: Unknown command\n";
}
}
return 0;
}