-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
172 lines (160 loc) · 7.57 KB
/
main.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
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
#include <iostream>
#include <string>
#include <cstring>
#include "./include/account.h"
#include "./include/task.h"
#include "./include/reminder.h"
#include "./include/ui.h"
std::mutex filemutex;
vector<Task> t_list;
using namespace std;
extern int userLogin(string username, string password);
extern bool isValidTime(const string& timeString);
extern bool isValidDate(const string& dateString);
extern bool main_doTask(vector<Task>& t_list, int taskId, string filename);
extern bool main_undoTask(vector<Task>& t_list, int taskId, string filename);
void help();
int main(int argc, char* argv[]) {
if(argc == 1){
UI ui;
ui.run();
return 0;
}
else if(argc >= 2){
if(strcmp(argv[1], "-h") == 0){
help();
return 0;
}
string username, passwd, operation;
username = argv[1];
passwd = argv[2];
passwd = toEncrypt(passwd);
int curUser = userLogin(username, passwd);//TODO: Define another userLogin()
if(curUser == -1){
cout << "Incorrect credentials. Check your input." << endl;
exit(401);
}
else{
string filename = to_string(curUser) + ".txt";
int userOption = 0;
operation = argv[3];
if(strcasecmp(operation.c_str(), "addTask") == 0) userOption = 1;
else if(strcasecmp(operation.c_str(), "deleteTask") == 0) userOption = 2;
else if(strcasecmp(operation.c_str(), "viewTask") == 0) userOption = 3;
else if(strcasecmp(operation.c_str(), "viewTaskByDate") == 0) userOption = 4;
else if (strcasecmp(operation.c_str(), "doTask") == 0) userOption = 5;
else if (strcasecmp(operation.c_str(), "undoTask") == 0) userOption = 6;
switch(userOption){
case 0: {cout << "Command unknown. Plese check your input." << endl; exit(404); break;}
case 1: {
string name, startTime, pri, cate, remindTime;
name = argv[4];
startTime = argv[5];
pri = argv[6];
cate = argv[7];
remindTime = argv[8];
if (!isValidTime(startTime) || !isValidTime(remindTime)){
cout << "Incorrect time format. The time format should be MM/DD/hh/mm." <<endl; exit(406); break;
}
Priority pr;
if(strcasecmp(pri.c_str(), "HIGH") == 0) pr = Priority::HIGH;
if(strcasecmp(pri.c_str(), "MEDIUM") == 0) pr = Priority::MEDIUM;
if(strcasecmp(pri.c_str(), "LOW") == 0) pr = Priority::LOW;
if(!(pr == Priority::HIGH || pr == Priority::MEDIUM || pr == Priority::LOW)){cout << "Incorrect priority syntax. " << endl; exit(406); break;}
Category cat;
if(strcasecmp(cate.c_str(), "LIFE") == 0) cat = Category::LIFE;
if(strcasecmp(cate.c_str(), "ENTERTAINMENT") == 0) cat = Category::ENTERTAINMENT;
if(strcasecmp(cate.c_str(), "LEARNING") == 0) cat = Category::LEARNING;
if(!(cat == Category::ENTERTAINMENT || cat == Category::LEARNING || cat == Category::LIFE)){cout << "Incorrect category syntax. " << endl; exit(406); break;}
//TODO: lock the database file
//cout<<"adsf"<<endl;
t_list = loadTasksFromFile(filename);
int cmd_next_id = 1;
if(t_list.empty()){
cout<<"empty"<<endl;
cmd_next_id = 1;
}
else{
cmd_next_id = (*(t_list.end()-1)).getId() + 1;
}
Task temp = Task(cmd_next_id, name, startTime, pr, cat, remindTime);
//cout<<"adsf"<<endl;
addTask(t_list, temp);
saveTasksToFile(t_list, filename);
//TODO: unlock the database file
break;
}
case 2:{
int taskId;
taskId = atoi(argv[4]);
//TODO: lock the database file
t_list = loadTasksFromFile(filename);
bool status = deleteTask(t_list, taskId);
saveTasksToFile(t_list, filename);
//TODO: unlock the database file
if(status) {cout << "Task " << taskId << " deleted successfully." << endl;}
else {cout << "Failed to delete task " << taskId << ". " << endl; exit(404);}
break;
}
case 3:{
//TODO: lock the database file
t_list = loadTasksFromFile(filename);
printTasks(t_list);
//TODO: unlock the database file
break;
}
case 4:{
string date;
date = argv[4];
if(!isValidDate(date)){cout << "Incorrect date format. The date format should be MM/DD."<<endl; exit(406); break;}
//TODO: lock the database file
t_list = loadTasksFromFile(filename);
vector<Task> t_list_date = getTasksByDate(t_list, date);
printTasks(t_list_date);
if (t_list_date.empty()) cout << "No tasks for " << date << ". " << endl;
break;
}
case 5:{
int taskId;
taskId = atoi(argv[4]);
t_list = loadTasksFromFile(filename);
bool flag = main_doTask(t_list, taskId, filename);
saveTasksToFile(t_list, filename);
if(flag) {cout << "Successfully set task " << taskId << " as done. " << endl;}
else {cout << "Failed to set task " << taskId << "as done. Please check your input. "<<endl; exit(404);}
break;
}
case 6:{
int taskId;
taskId = atoi(argv[4]);
t_list = loadTasksFromFile(filename);
bool flag = main_undoTask(t_list, taskId, filename);
saveTasksToFile(t_list, filename);
if(flag) {cout << "Successfully set task " << taskId << " as undone. " << endl;}
else {cout << "Failed to set task " << taskId << "as undone. Please check your input. "<<endl; exit(404);}
break;
}
default:{{cout << "An error occurred. Plese check your input." << endl; exit(404); break;}}
}
}
exit(0);
}
}
void help(){
cout<<"To addTask try: ./todo username password addTask startTime Priority Category reminderTime"<<endl;
cout<<"The time format : MM/DD/hh/mm"<<endl;
cout<<"Priority: HIGH MEDIUM LOW"<<endl;
cout<<"Category: LIFE ENTERTAINMENT LEARNING"<<endl;
cout<<endl;
cout<<"To deleteTask try: ./todo username password deleteTask <int>taskId"<<endl;
cout<<endl;
cout<<"To viewTask try: ./todo username password viewTask"<<endl;
cout<<endl;
cout<<"To viewTaskByDate try: ./todo username password viewTaskByDate reminderTime"<<endl;
cout<<"The time format : MM/DD"<<endl;
cout<<endl;
cout<<"To doTask try: ./todo username password doTask <int>taskId"<<endl;
cout<<endl;
cout<<"To undoTask try: ./todo username password undoTask <int>taskId"<<endl;
cout<<endl;
}