-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathTheGreatTodoApp.c
196 lines (180 loc) · 4 KB
/
TheGreatTodoApp.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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
int tLength = 0;
FILE *fp;
struct Todo
{
char title[50];
char createdAt[50];
_Bool isCompleted;
} todos[20];
void saveToFile()
{
fp = fopen("todos.bin", "w");
if (!fp)
{
printf("Can't save your todo\n");
}
else
{
for (size_t i = 0; i < tLength; i++)
{
fwrite(&todos[i], sizeof(struct Todo), 1, fp);
}
fclose(fp);
}
}
void getFileSize()
{
fseek(fp, 0L, SEEK_END);
unsigned int long size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
tLength = size / sizeof(struct Todo);
}
void readFromFile()
{
fp = fopen("todos.bin", "r");
if (!fp)
{
printf("We are not able to find any todos file\n");
}
else
{
getFileSize();
for (size_t i = 0; i < tLength; i++)
{
fread(&todos[i], sizeof(struct Todo), 1, fp);
}
fclose(fp);
}
}
void addTodo()
{
// for todo title
char userInput[50];
printf("Type your todo \n>> ");
scanf("%[^\n]s", userInput);
strncpy(todos[tLength].title, userInput, 50);
// add time
char todoTime[50];
struct tm cTime;
time_t timeS = time(NULL);
localtime_r(&timeS, &cTime);
// 2/12 1:21
snprintf(todoTime, 50, "%i/%i %i:%i", cTime.tm_mday, cTime.tm_mon + 1, cTime.tm_hour, cTime.tm_min);
strcpy(todos[tLength].createdAt, todoTime);
//set boolean to false
todos[tLength].isCompleted = false;
tLength++;
}
void printAllTodo()
{
printf("+----+-------------------------------------+--------------+-------------+\n");
printf("| ID | \t \t Todo Title | Created at | Completed |\n");
printf("+----+-------------------------------------+--------------+-------------+\n");
for (int i = 0; i < tLength; i++)
{
if (todos[i].isCompleted)
{
printf("\033[10m");
}
else
{
printf("\033[1m");
}
printf("|%3d | %-35s | %-12s | %-13s |\n", i + 1, todos[i].title, todos[i].createdAt, (!todos[i].isCompleted) ? " ❌ No " : " ✅ Yes ");
printf("+----+-------------------------------------+--------------+-------------+\n");
}
}
void markAsComplete()
{
int todoId;
printf("Enter the ID of todo \n>>");
scanf("%d", &todoId);
todoId--;
if (todoId < 0 || todoId > tLength)
{
printf("Invalid todo id 😑\n");
}
else
{
todos[todoId].isCompleted = true;
printf("Todo marked as completed \n");
}
}
void deleteTodo()
{
int todoId;
printf("Enter the ID of todo \n>>");
scanf("%d", &todoId);
if (todoId < 0 || todoId > tLength)
{
printf("Invalid todo id 😑\n");
}
else
{
todoId--;
memmove(todos + todoId, todos + todoId + 1, (tLength - todoId - 1) * sizeof(*todos));
tLength--;
printf("Your todo has been deleted 😵\n");
}
}
void ShowOptions()
{
char userChoice;
printf("Type 'A' to add, 'D' to delete & 'C' to mark complete or 'Q' to quit\n>>");
userChoice = getchar();
userChoice = toupper(userChoice);
getchar();
switch (userChoice)
{
case 'A':
addTodo();
break;
case 'D':
deleteTodo();
break;
case 'C':
markAsComplete();
break;
case 'Q':
exit(0);
break;
default:
printf("Command not found 😓\n");
ShowOptions();
break;
}
saveToFile();
printAllTodo();
getchar();
ShowOptions();
}
void isThisFirstTime()
{
if (access("todos.bin", F_OK) != -1)
{
readFromFile();
printAllTodo();
ShowOptions();
}
else
{
printf("Welcome to the Great Todo App\n");
addTodo();
saveToFile();
printAllTodo();
ShowOptions();
}
}
int main()
{
system("clear||@cls");
printf("\033[32;1m");
isThisFirstTime();
}