-
Notifications
You must be signed in to change notification settings - Fork 1
/
todoc.c
287 lines (245 loc) · 7.11 KB
/
todoc.c
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
// Todo.c: the main file.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <stdbool.h>
#include "task.h"
#include "tasklist.h"
#define VERSION_MAJOR 0
#define VERSION_MINOR 2
#define VERSION_BUILD 3
int add_task(char* filename, char* string){
tasklist* list = tasklist_new();
Task* task = task_new();
FILE* file = fopen(filename, "r");
if (file == NULL){
file = fopen(filename, "w+");
if (file == NULL){
perror("Add task");
goto error;
}
}
if (tasklist_read(list, file) != 0){
puts("ERROR: could not read tasklist.");
goto error;
}
task_append(task, string);
dllist_append(list, task);
freopen(filename, "r+", file);
if (tasklist_dump(list, file) != 0){
puts("ERROR: could not write tasklist.");
goto error;
}
printf("Added: %s\n", string);
tasklist_destroy(list);
return 0;
error:
tasklist_destroy(list);
return 1;
}
/** List the tasks in the file given.
*/
int list_tasks(char* filename){
// Open up the file, bro.
FILE* file = fopen(filename, "r");
if (file == NULL){
perror("Could not open file");
errno = 0;
return -1;
}
tasklist* list = tasklist_new();
// TODO: Error handling for reading file.
tasklist_read(list, file);
fclose(file);
tasklist_display(list);
// TODO: Display the amount of tasks.
printf("---\n");
tasklist_destroy(list);
return 0;
}
/** List the tasks with a match in the string.
*
*/
void list_tasks_matching(char* filename, char* string){
if (string == NULL) string = "";
FILE* file = fopen(filename, "r");
if (file == NULL){
perror("Could not open file");
errno = 0;
return;
}
tasklist* list = tasklist_new();
tasklist_read(list, file);
tasklist* matches = tasklist_search(list, string);
int count = tasklist_display(matches);
if (count == 0){
puts("No matches.");
} else {
printf("Number of matches: %d.\n", count);
}
fclose(file);
// Make sure we don't destroy the data.
tasklist_free(matches);
tasklist_destroy(list);
}
// Removes a task given the file and the index.
int remove_task(char* filename, int number){
FILE* file = fopen(filename, "r");
if (file == NULL){
perror("Cannot remove task");
errno = 0;
goto error;
}
// Open up the tasklist.
tasklist* list = tasklist_new();
if (tasklist_read(list, file) != 0){
puts("ERROR: could not read tasklist.");
goto error;
}
fclose(file);
fopen(filename, "w");
Task* t = tasklist_get(list, number-1);
if (t == NULL){
puts("There is no task with that number.");
goto error;
}
printf("Are you sure you want to remove %d: %s? (y/n)\n", number, task_dump(t));
char answer[5];
char* ans = answer;
fgets(ans, 5, stdin);
if (strcmp(answer, "y\n") == 0){
tasklist_remove(list, number-1);
if (tasklist_dump(list, file) != 0){
puts("ERROR: could not write tasklist.");
}
}
printf("Task %d deleted.\n", number);
task_free(t);
tasklist_free(list);
error:
fclose(file);
return 1;
}
/** Complete a task.
*
*/
void set_complete_task(char* filename, int number, bool comp)
{
tasklist* list = tasklist_new();
FILE* file = fopen(filename, "r");
tasklist_read(list, file);
fclose(file);
Task* task = tasklist_get(list, number-1);
task_set_complete(task, comp);
file = fopen(filename, "w");
tasklist_dump(list, file);
fclose(file);
if (comp == true){
printf("Marked task '%s' (%d) as complete.\n", task->description, number);
} else {
printf("Marked task '%s' (%d) as incomplete.\n", task->description, number);
}
tasklist_free(list);
}
// Help functions.
void print_version(){
printf("todoc version %d.%d.%d by Ben Davenport-Ray and contributors.\n", VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD);
}
void print_short_help(){
puts("todoc [options] <command> [number] [description]");
}
void print_help(){
print_short_help();
puts("Manage tasks in a todo.txt file.\n");
puts("Options:\n"
" -f file \t\tSet the file used.\n"
" -h \t\t\tShow the extended help.\n\n"
"Commands:\n"
" -a|add 'task'\t\tadds the task to the list\n"
" -d|do index\t\tmarks the task at the index as done\n"
" -u|undo index\t\tmarks the task at the index as incomplete\n"
" -r|remove index\tremoves the task at the index\n"
" -s|search 'query'\tsearches for the text in the tasklist\n"
" -l|list\t\tshows all the tasks\n");
}
bool strings_equal(char* subject, char* first, char* sec){
if(strcmp(subject, first) == 0 ||
strcmp(subject, sec) == 0){
return true;
} else {
return false;
}
}
int main(int argc, char* argv[]){
char* taskfilename = "todo.txt";
// char* donefile = "done.txt";
//bool verbose = false;
int status = EXIT_SUCCESS;
// parse the command line arguments.
if (argc < 2){
print_short_help();
exit(status);
}
int i;
//start the loop at 1 because argv[0] refers to the binary file
for (i = 1; i < argc; i++){
// Add a new task.
if (strings_equal(argv[i], "add", "-a")){
if (add_task(taskfilename, argv[i+1]) != 0){
status = EXIT_FAILURE; // We done goofed.
}
i++; // Skip the next argument.
continue;
}
// Remove a task.
if (strings_equal(argv[i], "rm", "-r")){
int index = atoi(argv[i+1]);
if (remove_task(taskfilename, index) != 0){
status = EXIT_FAILURE;
}
i++;
continue;
}
// Complete a task.
if (strings_equal(argv[i], "do", "-d")){
int index = atoi(argv[i+1]);
set_complete_task(taskfilename, index, true);
i++;
continue;
}
// Uncomplete a task.
if (strings_equal(argv[i], "undo", "-u")){
int index = atoi(argv[i+1]);
set_complete_task(taskfilename, index, false);
i++;
continue;
}
// List the tasks matching the string in the file.
if (strings_equal(argv[i], "search", "-s")){
list_tasks_matching(taskfilename, argv[i+1]);
i++;
continue;
}
// Show all the tasks in the file.
if(strings_equal(argv[i], "ls", "-l")){
list_tasks(taskfilename);
continue;
}
// Show the help dialog.
if (strcmp(argv[i], "-h") == 0){
print_help();
}
// Set the taskfilename flag.
if (strcmp(argv[i], "-f") == 0){
taskfilename = argv[i+1];
i++;
continue;
}
// Set the verbosity flag.
if (strcmp(argv[i], "-v") == 0){
// verbose = true;
}
}
return status;
}