-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.cpp
239 lines (205 loc) · 6.8 KB
/
parse.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
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
/* parse.cpp: utilities for parsing input (outlined in parse.h) */
#include "parse.h"
/** getHelp
* prints out usage for the app to the user
*/
void getHelp() {
cout << "pritority-list: quick CLI to-do list utility" << endl;
cout << "Usage: > [command] [args...]" << endl;
cout << endl;
cout << " add [task] [priority] [tags...]" << endl;
cout << " - adds task with given priority and tags to list" << endl;
cout << " remove [task]" << endl;
cout << " - removes task from list if it exists" << endl;
cout << " show [tags...]" << endl;
cout << " - prints out all tasks filtered by 'tag' (leave blank for all tasks)" << endl;
cout << " tag [task] [tags...]" << endl;
cout << " - adds tags to given task" << endl;
cout << " untag [task] [tags...]" << endl;
cout << " - removes tags from given task if they exist" << endl;
cout << " file [filename]" << endl;
cout << " - sets current working file to one specified (used for loading/saving)" << endl;
cout << " load" << endl;
cout << " - reads tasks from set file and populates current tasks" << endl;
cout << " save" << endl;
cout << " - writes current tasks out to set file (overwrites file!)" << endl;
cout << " quit" << endl;
cout << " - exits the application" << endl;
cout << " help" << endl;
cout << " - displays this message" << endl;
cout << endl;
cout << "Priority values range from 0 (highest priority) to 9 (least priority)." << endl;
cout << "If no value is given, default priority of 9 is chosen." << endl;
}
/** parseCommand
* Input: a string argument for the command issued, a reference to the task list
* Output: 0 if success, other if failure (error code)
* 0 -- success
* 1 -- quit
* 2 -- other error
* parses the command and delegates task to other functions
*/
int parseCommand( const string& cmd, vector<Task>& tasks, string& file ) {
string arg;
stringstream ss {cmd};
ss >> arg;
/* just check to see if it's in a the list of valid args */
if ( arg == "help" || arg == "h" ) {
getHelp();
}
else if ( arg == "quit" || arg == "q" ) {
return 1;
}
else if ( arg == "add" || arg == "a" ) {
addTask( ss, tasks );
}
else if ( arg == "show" || arg == "print" || arg == "s" || arg == "p" ) {
showTasks( ss, tasks );
}
else if ( arg == "remove" || arg == "r" ) {
removeTask( ss, tasks );
}
else if ( arg == "tag" || arg == "t" ) {
addTags( ss, tasks );
}
else if ( arg == "untag" || arg == "u" ) {
removeTags( ss, tasks );
}
else if ( arg == "file" || arg == "f" ) {
ss >> file;
}
else if ( arg == "save" || arg == "s" ) {
int rval = save( file, tasks );
return rval;
}
else if ( arg == "load" || arg == "l" ) {
int rval = load( file, tasks );
return rval;
}
return 0;
}
void addTask( stringstream& ss, vector<Task>& tasks ) {
string name, next;
int priority;
ss >> name >> next; // fetch name and first arg
// figure out the priority
try {
priority = stoi(next); // convert to priority
if ( !(ss >> next) ) // fetch the next string
next = "";
} catch( ... ) {
priority = 9;
}
Task t ( name, priority ); // make the task object
// read in the tags
do {
if ( next.size() )
t.addTag(next);
} while ( ss >> next );
// add the task to the task list if not there
bool in = false;
for ( const Task& task : tasks ) {
if ( task.getName() == t.getName() )
in = true;
}
if ( !in ) {
tasks.push_back(t);
cout << "Added task " << t.getName() << " with priority " << t.getPriority() << endl;
} else {
cout << "Task " << t.getName() << " already exists" << endl;
}
}
void showTasks( stringstream& ss, const vector<Task>& tasks ) {
// get the first tag
string tag = "";
ss >> tag;
// check if any tags requested
if ( tag.size() == 0 ) {
ss.clear(); // reset the stream
ss.str("all");
showTasks( ss, tasks );
return;
}
// if we have tags, make a priority queue
priority_queue<Task> tasklist;
unordered_set<string> names; // keeps the tasks we've added
do {
// search for items with this tag
for ( const Task& t : tasks ) {
if ( t.hasTag(tag) && (names.count(t.getName()) == 0) ) {
if ( t.active() || tag == "finished" ) {
tasklist.push(t);
names.insert( t.getName() );
}
}
}
} while ( ss >> tag );
// print out the priority queue
while ( !(tasklist.empty()) ) {
Task t = tasklist.top(); // get the element
cout << "(" << t.getPriority() << ") " << t.getName() << endl;
cout << " ";
for ( const string& tag : t.getTags() ) {
if ( !hiddenTag(tag) )
cout << tag << " ";
}
cout << endl;
tasklist.pop(); // remove the element
}
}
void removeTask( stringstream& ss, vector<Task>& tasks ) {
string name;
ss >> name;
vector<Task>::iterator iter;
for ( iter = tasks.begin(); iter != tasks.end(); ++iter ) {
if ( iter->getName() == name ) {
tasks.erase(iter);
cout << "Removed task: " << iter->getName() << endl;
break;
}
}
if ( iter == tasks.end() ) {
cout << "Task " << name << " doesn't exist" << endl;
}
}
void addTags( stringstream& ss, vector<Task>& tasks ) {
string name, tag;
ss >> name;
vector<Task>::iterator modtask;
for ( modtask = tasks.begin(); modtask != tasks.end(); ++modtask ) {
if ( modtask->getName() == name ) {
break;
}
}
if ( modtask == tasks.end() ) return;
while ( ss >> tag ) {
modtask->addTag( tag );
cout << "Tagged task " << modtask->getName() << " with " << tag << endl;
}
return;
}
void removeTags( stringstream& ss, vector<Task>& tasks ) {
string name, tag;
ss >> name;
vector<Task>::iterator modtask;
for ( modtask = tasks.begin(); modtask != tasks.end(); ++modtask ) {
if ( modtask->getName() == name ) {
break;
}
}
if ( modtask == tasks.end() ) return;
while ( ss >> tag ) {
modtask->removeTag( tag );
cout << "Removed tag " << tag << " from task " << modtask->getName() << endl;
}
return;
}
bool hiddenTag( const string& tag ) {
if ( tag == "all" ) {
return true;
} else if ( tag == "finished" ) {
return true;
} else {
return false;
}
}