-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToDo.cpp
113 lines (102 loc) · 2.92 KB
/
ToDo.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
#include <iostream>
#include <string>
#include <map>
#include <fstream>
#include <algorithm>
using std::cout;
using std::endl;
using std::string;
using std::find;
char* getArg(char** begin,char** end,const string& opt){
char** itr=find(begin,end,opt);
if(itr!=end && ++itr != end){return *itr;}
return 0;
}
bool argExists(char** begin,char** end,const string& opt){
return find(begin,end,opt) != end;
}
int main(int argc,char** argv){
std::ifstream infile("todo.txt");
std::ofstream outfile;
std::map<int,string[4]> listing;
if (argc>=2){//make sure there's argument
for(string line;getline(infile,line);){//load data
int idx=std::stoi(line.substr(0,1),nullptr,0);//ID
listing[idx][0]=line.substr(2,1);//completion
listing[idx][1]=line.substr(5,1);//priority
listing[idx][2]=line.substr(8,14);//due
listing[idx][3]=line.substr(22);//task
}
infile.close();
if (argExists(argv,argv+2,"list")){//list argument
cout<<"listing"<<std::endl;
if(listing.empty()){//if map is empty there's no task
cout<<"no task available!"<<endl;
}
else{//else print
string result;
for(auto x:listing){
result=std::to_string(x.first)+" ";//get ID key
for(int i=0;i<4;i++){//get the rest of data
result+=x.second[i];
}
cout<<result<<endl;
}
}
}
else if (argExists(argv,argv+2,"add")){//add argument
outfile.open("todo.txt",std::fstream::app);
int taskID=listing.size()+1;
char* task = getArg(argv,argv+argc,"add");
if(!task){//make sure task is specified
cout<<"no task added\n";
return 0;
}else{
outfile<<taskID<<"[.](_) due:========== "<<task<<"\n";
cout<<"Created task: \n|"<<taskID<<"|"<<task<<endl;
}
outfile.close();
}
else if(argExists(argv,argv+argc,"done")){//done argument
outfile.open("todo.txt",std::ofstream::trunc);
int taskID=std::stoi(argv[1],nullptr,0);
listing[taskID][0]="x";
for(auto key:listing){
string completion=listing[key.first][0];
string priority=listing[key.first][1];
string due=listing[key.first][2];
string task=listing[key.first][3];
outfile<<key.first<<"["<<completion<<"]"<<"("<<priority<<") "<<due<<" "<<task<<endl;
}
outfile.close();
cout<<"Marked task "<<taskID<<" as done\n";
}
else if(argExists(argv,argv+2,"prune")){//prune argument
int ctr=1;
outfile.open("todo.txt",std::ofstream::trunc);
for(auto key:listing){
if (key.second[0]=="x"){
listing.erase(key.first);
}
}
for(auto key:listing){
string completion=listing[key.first][0];
string priority=listing[key.first][1];
string due=listing[key.first][2];
string task=listing[key.first][3];
outfile<<ctr<<"["<<completion<<"]"<<"("<<priority<<") "<<due<<" "<<task<<endl;
ctr++;
}
outfile.close();
cout<<"Done cleaning!\n";
}
//TODO:add help
else{
cout<<"Invalid argument(s)!\n";
}
}
else{
cout<<"No arguments specified"<<endl;
}
return 0;
}