-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtodo.c
243 lines (206 loc) · 5.03 KB
/
todo.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
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <regex.h>
#include "todo.h"
const char* colors[] = {"\033[0m", "\033[32m"};
#define MORA_LENGTH 45
const char* mora[] = {"wa", "wi", "wu", "we", "wo",
"ra", "ri", "ru", "re", "ro",
"ya", "yi", "yu", "ye", "yo",
"ma", "mi", "mu", "me", "mo",
"ha", "hi", "hu", "he", "ho",
"na", "ni", "nu", "ne", "no",
"ta", "ti", "tu", "te", "to",
"sa", "si", "su", "se", "so",
"ka", "ki", "ku", "ke", "ko"};
/* generates an unused id according to the id scheme */
char* generate_id(){
FILE *fp;
fp = open_list("r");
char line[4096]; // todo: define line buffer somewhere
char* id = malloc(6*sizeof(char));
int done =0;
/* we just keep guessing at an unused id until we strike gold.
* todo: do this in a way that doesn't potentially run forever.
*/
while(!done){
sprintf(id, "%s%s\t", mora[rand()%MORA_LENGTH], mora[rand()%MORA_LENGTH]);
fseek(fp, 0, SEEK_SET);
done = 1;
while(fp && fgets(line, sizeof(line), fp)){
if(strstr(line,id)){
done = 0;
}
}
}
fclose(fp);
id[5]='\0';
return id;
}
int main(int argc, char *argv[])
{
int c;
unsigned int iseed = (unsigned int)time(NULL);
srand(iseed);
if(argc == 1){
list_all();
return 0;
}
while (1)
{
static struct option long_options[] =
{
{"help", no_argument, NULL, 'h'},
{"add", required_argument, NULL, 'a'},
{"show", required_argument, NULL, 's'},
{"done", required_argument, NULL, 'd'},
{"list", required_argument, NULL, 'l'},
{0, 0, 0, 0}
};
int option_index = 0;
c = getopt_long(argc, argv, "ha:s:d:l:", long_options, &option_index);
if(c == -1)
break;
switch(c)
{
case 'h':
help();
break;
case 'a':
add_todo(optarg);
break;
case 's':
list_id(optarg); //todo: fix better casting
break;
case 'd':
remove_todo(optarg); // todo: fix better casting
break;
case 'l':
search(optarg);
break;
}
}
return 0;
}
void help()
{
printf("Usage:\n");
printf("todo\n");
printf("todo (-a|--add) <task>\n");
printf("todo (-d|--done) <id>\n");
printf("todo (-s|--show) <id>\n");
printf("todo (-l|--list) <regex>\n");
}
void add_todo(char string[])
{
char* id = generate_id();
FILE *fp;
fp = open_list("ar");
fprintf(fp, "%s\t%s\n", id, string);
fclose(fp);
printf("Added task with id %s%s%s:\t%s\n",colors[COLOR_GREEN], id, colors[COLOR_RESET], string);
free(id);
}
void remove_todo(char* id)
{
FILE *fp;
char *list_buffer;
fp = open_list("r");
fseek(fp, 0L, SEEK_END);
list_buffer = malloc(ftell(fp)*sizeof(char));
fseek(fp, 0L, SEEK_SET);
char line [4096]; // todo: define line buffer somewhere
while(fgets(line, sizeof(line), fp)){
if(!strstr(line, id)) {
if(list_buffer[0] == '\0') {
sprintf(list_buffer, "%s", line);
} else {
sprintf(list_buffer, "%s%s", list_buffer, line);
}
} else {
printf("Removed task ");
print_line_colored(line);
}
}
fclose(fp);
fp = open_list("w");
fprintf(fp, "%s", list_buffer);
fclose(fp);
free(list_buffer);
}
void print_line_colored(char*line){
printf("%s",colors[COLOR_GREEN]);
int i = 0;
char c;
while((c = line[i++]) != '\0'){
if(c=='\t'){
printf("%s",colors[COLOR_RESET]);
}
printf("%c",c);
}
}
void list_all()
{
FILE *fp;
fp = open_list("r");
char line[4096]; // todo: define line buffer somewhere
int counter = 0;
while(fp && fgets(line, sizeof(line), fp)){
print_line_colored(line);
counter++;
}
if(!counter){
printf("No tasks.\n");
help();
}
fclose(fp);
}
// show task with given id. too naive now.
void list_id(char* id)
{
FILE *fp;
fp = open_list("r");
char line[4096]; // todo: define line buffer somewhere
while(fp && fgets(line, sizeof(line), fp)){
if(strstr(line,id)){
print_line_colored(line);
}
}
fclose(fp);
}
FILE *open_list(char *mode)
{
FILE *fp;
char todoFilePath[256];
sprintf(todoFilePath,"%s/%s", getenv("HOME"), FILENAME);
fp = fopen(todoFilePath, mode);
if (fp == NULL) {
fp = fopen(todoFilePath, "w");
fclose(fp);
return open_list(mode);
}
// file opened properly; return FILE pointer
return fp;
}
/* search each line in todofile for pattern*/
void search(char string[]) {
regex_t reg;
int err;
char err_msg[80];
FILE *fp;
char line[4096];
if((err = regcomp(®, string, REG_NOSUB|REG_EXTENDED)) != 0){
regerror(err, ®, err_msg, 80);
printf("Error analyzing regular expression '%s': %s.\n", string, err_msg);
}
fp = open_list("r");
while(fgets(line, sizeof(line), fp)){
if(regexec(®, line + 4, 0, NULL, 0) == 0)
print_line_colored(line);
}
fclose(fp);
}