-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
326 lines (289 loc) · 10.3 KB
/
Copy pathmain.cpp
File metadata and controls
326 lines (289 loc) · 10.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/* Task Properties
* id - a unique identifier for the task
* description - a short description of the task
* status - the status of the task (todo, in-progress, done)
* createdAt - the date and time when the task was created
* updatedAt - the date and time when the task was last updated
*/
/* Commands
* Add
* Update
* Delete
* Mark In Progress
* Mark Finished
* List All Tasks
* List All Finished Tasks
* List All Tasks Not Finsihed
* List All Tasks In Progress
*/
/* Review Diff: data["tasks"] vs data.at("tasks") error handling
*
*
*/
/* Features to Add:
*
* [QUICK FEATURES]
* Due Dates
* Priority Levels
* Tags / Categories
* Search
* Sorting Flags
* Undo Delete
*
* [Quality of Life]
* Colored Terminal Output
* Table Formatting
* Config File
* --help usage text
*
* [CODE QUALITY]
* Extract a Task struct/class
* Centralize ID parsing
* Wire up validate_specifier
* Replace ctime
* Split into files
* Unit tests
*/
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
#include <ctime>
#include <stdio.h>
#include <system_error>
#include <type_traits>
#include <utility>
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <ctime>
using json = nlohmann::ordered_json;
json load_tasks(const std::string& filepath) { // load the json file
std::ifstream in(filepath);
if (!in.is_open()) {
json j;
j["tasks"] = json::array();
j["last_id"] = 0;
return j;
}
json data;
try {
in >> data;
} catch (const json::parse_error& e) {
std::cerr << "Failed to parse " << filepath << ": " << e.what() << std::endl;
}
return data;
}
void save_tasks(const std::string& filepath, const json& data) {
std::ofstream out(filepath);
out << data.dump(4);
}
enum class SpecifierType {
NONE,
FREE_TEXT,
TASK_ID,
STATUS
};
struct Command {
std::string name;
int argc_threshold;
SpecifierType expected_specifier;
};
static const std::unordered_map<std::string, Command> commands = {
{"add", {"add", 3, SpecifierType::FREE_TEXT}},
{"update", {"update", 4, SpecifierType::TASK_ID}},
{"delete", {"delete", 3, SpecifierType::TASK_ID}},
{"mark-in-progress", {"mark-in-progress", 3, SpecifierType::TASK_ID}},
{"mark-done", {"mark-done", 3, SpecifierType::TASK_ID}},
{"list", {"list", 2, SpecifierType::STATUS}},
};
bool validate_specifier(SpecifierType type, const std::string& specifier) { // will be used to check if the sub-specifier is present or not (might not be needed?)
switch (type) {
case SpecifierType::NONE:
return true;
case SpecifierType::FREE_TEXT:
return !specifier.empty();
case SpecifierType::TASK_ID:
// would need: is this parseable as a number? does a task with this ID exist?
return false; // placeholder
case SpecifierType::STATUS:
return (specifier == "done" || specifier == "todo" || specifier == "in-progress");
}
return false;
}
const Command* find_command(const std::string& name) {
auto it = commands.find(name);
if (it != commands.end()) {
return &(it->second);
}
return nullptr;
}
bool error_checking(int argc, char *argv[]) {;
int threshold = -1;
try {
if (argc == 1) { // didn't specify any command (doesn't work for list)
std::cout << "Valid Commands: " << std::endl;
for (const auto& cmd: commands) {
std::cout << cmd.second.name << std::endl;
}
throw "Error, unspecified command. \n";
}
if (argc >=2) {
std::string action = std::string(argv[1]); // check if action is valid
const Command* cmd = find_command(action);
if (cmd == nullptr) {
throw "Error, illegal action. \n";
}
threshold = cmd->argc_threshold;
if (argc < threshold) {
throw "Error, unspecified sub-actions. \n";
}
if (argc > threshold) {
throw "Error, too many args. \n";
}
return true;
}
} catch (const char* msg) {
std::cout << msg;
}
return false;
}
void print_info(int argc, char *argv[]) {
std::cout << "[DEBUGGING INFORMATION]" << std::endl;
std::cout << "[ARG COUNT]: " << argc << std::endl;
std::string action;
for (int i = 0; i < argc; i++) {
std::cout << "[ARG (" << i << ")]: " << argv[i] << std::endl;
if (i == 1) {
action = std::string(argv[i]);
auto it = commands.find(action);
if (it != commands.end()) {
const Command& cmd = it->second;
std::cout << "[COMMAND NAME: " << cmd.name << "]" << std::endl;
std::cout << "[COMMAND ARGC_THRESHOLD: " << cmd.argc_threshold << "]" << std::endl;
//std::cout << "[COMMAND EXPECTED_SPECIFIER " << cmd.expected_specifier << "]" << std::endl; does not work for now
}
}
}
}
int main(int argc, char *argv[]) {
print_info(argc, argv); // debugging information
if (error_checking(argc, argv)) { // if passes error checking, good to go
std::string action = std::string(argv[1]);
std::string specifier = (argc > 2) ? std::string(argv[2]) : std::string();
int id_specifier = -1;
time_t timestamp;
time(×tamp);
json data = load_tasks("task-tracking.json");
const Command* cmd = find_command(action);
// TODO: potentially make this a seperate function?
if (cmd->expected_specifier == SpecifierType::TASK_ID && argc > 2) {
try {
id_specifier = std::stoi(argv[2]);
} catch (...) {
std::cerr << "Error: task ID must be a number. \n";
return 1;
}
}
if (action == "add") { // expects string description
// determine new id from last_id (fallback to 0 if missing/null)
int next_id = 1;
if (data.contains("last_id") && !data["last_id"].is_null()) {
next_id = data["last_id"].get<int>() + 1;
}
json new_task;
new_task["id"] = next_id;
new_task["description"] = specifier;
new_task["status"] = "todo";
new_task["createdAt"] = ctime(×tamp);
new_task["updatedAt"] = ctime(×tamp);
data["tasks"].push_back(new_task);
data["last_id"] = next_id;
save_tasks("task-tracking.json", data);
std::cout << "[SUCCESS]: Task added successfully (ID: " << next_id << ")" << std::endl;
return 0;
}
if (action == "update") { // expects id, and string description
if (argc < 4) {
std::cerr << "Error: update requires an ID and a description. \n";
}
std::string new_description = argv[3];
bool found = false;
for (auto& task: data["tasks"]) {
if (task.at("id").is_null()) continue;
if (task.at("id").get<int>() == id_specifier) {
task["description"] = new_description;
task["updatedAt"] = ctime(×tamp);
found = true;
break;
}
}
if (!found) {
std::cerr << "Error: no task with ID " << id_specifier << "\n.";
return 1;
}
save_tasks("task-tracking.json", data);
std::cout << "[SUCCESS]: Task " << id_specifier << " updated.\n";
}
if (action == "mark-in-progress" || action == "mark-done") {
bool found = false;
for (auto& task: data["tasks"]) {
if (task.at("id").is_null()) continue;
if (task.at("id").get<int>() == id_specifier) {
task["status"] = action.substr(5);
task["updatedAt"] = ctime(×tamp);
found = true;
}
}
if (!found) {
std::cerr << "Error: no task with ID " << id_specifier << "\n.";
return 1;
}
save_tasks("task-tracking.json", data);
std::cout << "[SUCCESS]: Task " << id_specifier << " updated.\n";
}
if (action == "delete" && argc > 2) { // both expect id
try {
id_specifier = std::stoi(argv[2]);
} catch (const std::invalid_argument&) {
std::cerr << "Error: task ID must be a number. \n" << std::endl;
return 1;
} catch (const std::out_of_range&) {
std::cerr << "Error: task ID out of range. \n" << std::endl;
return 1;
}
auto& tasks = data["tasks"];
bool found = false;
for (auto it = tasks.begin(); it != tasks.end(); ++it) {
if (it->at("id").is_null()) continue; // skip bad entries to avoid err
if (it->at("id").get<int>() == id_specifier) {
it = tasks.erase(it);
found = true;
break;
}
}
if (!found) {
std::cerr << "Error: no task with ID " << id_specifier << "\n.";
return 1;
}
save_tasks("task-tracking.json", data);
std::cout << "[SUCCESS]: Task " << id_specifier << " deleted. \n" << std::endl;
}
if (action == "list") {
if (argc > 2) { // sub-specifier exists, match and loop
for (const auto& task: data.at("tasks")) {
if (task.at("status").get<std::string>() != specifier) continue;
std::cout << "[" << task.at("id") << "] "
<< task.at("description").get<std::string>() << std::endl;
}
} else { // no specifier, default behaviour == just list all
for (const auto& task: data.at("tasks")) {
std::cout << "[" << task.at("id") << "] "
<< task.at("description")
<< " (" << task.at("status") << ")"
<< std::endl;
}
}
}
}
return 0;
}