-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
245 lines (217 loc) · 7.67 KB
/
Copy pathmain.cpp
File metadata and controls
245 lines (217 loc) · 7.67 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
/* 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
*/
// Main Function --> Handles Input
//#include <nlohmann/json.hpp>
#include <iostream>
#include <fstream>
#include <ostream>
#include <string>
#include <ctime>
#include <stdio.h>
#include <utility>
#include "Task.h"
#include <unordered_map>
#include <nlohmann/json.hpp>
#include <ctime>
using json = nlohmann::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
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);
if (error_checking(argc, argv)) {
std::string action = std::string(argv[1]);
std::string specifier = (argc > 2) ? std::string(argv[2]) : std::string();
int id_specifier = -1;
json data = load_tasks("task-tracking.json");
if (action == "add") { // expects string description
std::cout << "[SUCCESS]: Task added successfully (ID: " << ")" << std::endl;
return 0;
}
if (action == "update") { // expects id, and string description
// To-Do
}
if (action == "delete") { // expects id
// To-Do
}
if (action == "mark-in-progress") {
time_t timestamp;
time(×tamp);
for (auto& task: data["tasks"]) {
if (task.at("id").is_null()) continue;
if (task.at("id").get<int>() == id_specifier) {
task["status"] = "in-progress";
task["updatedAt"] = ctime(×tamp);
}
}
}
if (action == "mark-done") {
time_t timestamp;
time(×tamp);
for (auto& task: data["tasks"]) {
if (task.at("id").is_null()) continue;
if (task.at("id").get<int>() == id_specifier) {
task["status"] = "done";
task["updatedAt"] = ctime(×tamp);
}
}
}
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"];
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) {
tasks.erase(it);
break;
}
}
}
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;
}