-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.c
330 lines (283 loc) · 7.48 KB
/
controller.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include "card.h"
#include "collection.h"
void help(){
printf("░█▀▀█ ░░▀ █░░█ █▀▀▄ █▀▀█ \n");
printf("▒█▄▄█ ░░█ █░░█ █░░█ █▄▄█ \n");
printf("▒█░▒█ █▄█ ░▀▀▀ ▀▀▀░ ▀░░▀ \n");
printf("\n");
printf("Comandos para o treino:\n");
printf("%-30s\t\t%s", "-playall", "Treina todas cartas atrasadas\n");
printf("%-30s\t\t%s", "-playonly (colecao)", "Treina todas as cartas atrasadas de uma colecao\n");
printf("%-30s\t\t%s", "-play (colecao)", "Treina todas as cartas de uma colecao\n");
printf("\n");
printf("Comandos para cartas:\n");
printf("%-30s\t\t%s", "-new (colecao)", "Criar uma carta\n");
printf("%-30s\t\t%s", "-edit (colecao)", "Editar uma carta\n");
printf("%-30s\t\t%s", "-del (colecao)", "Apagar uma carta\n");
printf("\n");
printf("Comandos para colecoes:\n");
printf("%-30s\t\t%s", "-create (colecao)", "Criar uma colecao\n");
printf("%-30s\t\t%s", "-view (colecao)", "Visualizar todas as cartas de uma colecao\n");
printf("%-30s\t\t%s", "-remove (colecao)", "Remove uma colecao\n");
}
void make_directory(const char* name) {
mkdir(name, 0700);
}
void init(){
struct stat * s;
if(stat("data", s) == -1){
make_directory("data");
}
}
char verifyFile(char * str){
DIR * d = opendir("./data");
struct dirent * dir;
if (d) {
while ((dir = readdir(d)) != NULL) {
if(!strcmp(dir->d_name, str)){
return 1;
}
}
closedir(d);
}
return 0;
}
int countFile(){
int i = -2;
DIR * d = opendir("./data");
struct dirent * dir;
if (d) {
while ((dir = readdir(d)) != NULL) {
i++;
}
closedir(d);
}
return i;
}
FILE * open_database(char * str, char * format){
FILE * f = NULL;
if(strlen(str) > 50){
printf("Nome da colecao excede o limite de 50 caracteres! Tente outro nome..\n");
return 0;
}
if(!verifyFile(str)){
printf("Colecao invalida!\n");
return f;
}
char dest[56] = "data/";
strcat(dest, str);
f = fopen(dest, format);
return f;
}
void home(){
int total_due = 0, qtt_due = 0;
DIR * d = opendir("./data");
struct dirent * dir;
collection * c = NULL;
FILE * f = NULL;
printf("▒█▀▀▀ █░░ █▀▀█ █▀▀ █░░█ ▒█▀▀█ █▀▀█ █▀▀█ █▀▀▄\n");
printf("▒█▀▀▀ █░░ █▄▄█ ▀▀█ █▀▀█ ▒█░░░ █▄▄█ █▄▄▀ █░░█\n");
printf("▒█░░░ ▀▀▀ ▀░░▀ ▀▀▀ ▀░░▀ ▒█▄▄█ ▀░░▀ ▀░▀▀ ▀▀▀░\n");
printf("\n\n");
printf("Coleções existentes:\n");
if (d) {
while ((dir = readdir(d)) != NULL) {
if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")){
f = open_database(dir->d_name, "r+");
c = collection_fread(1, dir->d_name, f);
qtt_due = collection_count_overdue(c);
total_due += qtt_due;
printf("- %-20s %d cartas atrasadas!!\n", dir->d_name, qtt_due);
}
}
closedir(d);
}
printf("\n");
printf("Total de cartas atrasadas: %d\n\n", total_due);
printf("Utilize \"-h\" para visualizar todas as opcoes disponiveis\n");
}
void del_card(char * str){
FILE * f = ( FILE *) open_database(str, "r+");
if(f == NULL){
fclose(f);
return;
}
collection * c = collection_create(str, 1);
int number;
c = collection_fread(1, str, f);
collection_print(c);
printf("Digite um numero 1-%d para deletar a carta\n", c->size);
printf("Digite 0 para sair\n");
scanf("%d", &number);
while(number != 0){
if(number > c->size || number < 0){
printf("Numero Invalido, digite novamente\n");
scanf("%d", &number);
continue;
}
printf("Digite 0 para sair\n");
collection_del_card(c, number - 1);
scanf("%d", &number);
}
f = ( FILE *) open_database(str, "w+");
collection_fprint(c, f);
}
void edit_card(char * str){
FILE * f = ( FILE *) open_database(str, "r+");
if(f == NULL){
fclose(f);
return;
}
collection * c = collection_create(str, 1);
card * d = NULL;
int number;
char front[150], back[150];
c = collection_fread(1, str, f);
collection_print(c);
printf("Digite um numero 1-%d para editar a carta\n", c->size);
printf("Digite 0 para sair\n");
scanf("%d", &number);
while(number != 0){
if(number > c->size || number < 0){
printf("Numero Invalido, digite novamente\n");
scanf("%d", &number);
continue;
}
scanf("%*c");
d = card_scanf(generate_id(c));
collection_edit_card(c, d, number - 1);
collection_print(c);
printf("Digite um numero 1-%d para editar a carta\n", c->size);
printf("Digite 0 para sair\n");
scanf("%d", &number);
}
f = ( FILE *) open_database(str, "w+");
collection_fprint(c, f);
fclose(f);
collection_free(c);
}
void create_card(char * name){
FILE * f = ( FILE *) open_database(name, "r+");
if(f == NULL){
fclose(f);
return;
}
collection * c = collection_fread(1, name, f);
int boolean;
rewind(f);
collection_append(c, card_scanf(generate_id(c)));
while(1){
printf("Deseja visualizar a carta?\n");
printf("0 - nao\n1 - sim\n");
scanf("%d", &boolean);
if(boolean == 1){
card_view(c->vector[c->size - 1]);
break;
} else if(boolean == 0) break;
else{
printf("Opcao invalida, tente novamente!\n");
}
}
collection_fprint(c, f);
collection_free(c);
fclose(f);
}
void create_collection(char * name){
if(strlen(name) > 50){
printf("Nome da colecao excede o limite de 50 caracteres! Tente outro nome..\n");
return;
}
if(verifyFile(name)){
printf("Colecao existente! Tente outro nome..\n");
return;
}
char dest[56] = "data/";
strcat(dest, name);
FILE * f = fopen(dest, "w");
collection * c = collection_create(name, 0);
collection_fprint(c, f);
fclose(f);
collection_free(c);
}
void delete_collection(char * name){
if(!verifyFile(name) || strlen(name) > 50){
printf("Colecao invalida!\n");
return;
}
char dest[56] = "data/";
strcat(dest, name);
remove(dest);
}
void view_collection(char * name){
FILE * f = ( FILE *) open_database(name, "r");
if(f == NULL) return;
int number;
collection * c = collection_create(name, 1);
c = collection_fread(1, name, f);
if(c->size <= 0){
printf("Colecao sem cartas\n");
fclose(f);
collection_free(c);
return;
}
collection_print(c);
if(c->size > 0){
printf("Selecione um numero de 1-%d para visualizar a carta\n", c->size);
printf("Digite 0 para sair\n");
scanf("%d", &number);
while(number != 0){
if(number > c->size || number < 0){
printf("Numero Invalido, digite novamente\n");
scanf("%d", &number);
continue;
}
printf("Digite 0 para sair\n");
card_view(c->vector[number-1]);
scanf("%d", &number);
}
}
fclose(f);
collection_free(c);
}
void playonly(char * string){
FILE * f = open_database(string, "r+");
collection * c = collection_fread(0, string, f);
collection * d = collection_overdue(c);
if(d->size <= 0){
printf("Não há cartas atrasadas em %s ainda!\n", string);
fclose(f);
return;
}
collection_print(d);
rewind(f);
collection_play(d);
collection_fprint(c, f);
collection_free(c);
fclose(f);
}
void playall(){
DIR * d = opendir("./data");
struct dirent * dir;
if (d) {
while ((dir = readdir(d)) != NULL) {
if(strcmp(dir->d_name, ".") && strcmp(dir->d_name, "..")){
playonly(dir->d_name);
}
}
closedir(d);
}
}
void play(char * string){
FILE * f = open_database(string, "r+");
collection * c = collection_fread(0, string, f);
rewind(f);
collection_play(c);
collection_fprint(c, f);
collection_free(c);
fclose(f);
}