-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
332 lines (317 loc) · 8.58 KB
/
main.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
331
332
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <assert.h>
#define MAX_COMMAND_LEN 32
#define NO_DATE_SPECIFIED -1
#define EXPIRY_DATE 7
#define ITEMLIST "items.csv"
#define CALL_UPLOAD_SCRIPT "python uploadCSV.py"
#define CALL_DOWNLOAD_SCRIPT "python downloadCSV.py"
typedef struct item_t
{
char* itemName;
time_t dateOfOpening; //in UNIX-based time representation, defaults to NOW
unsigned char daysToExpire; //default: 7
} item;
item* initItem(char *input, time_t opening)
{
item* i = (item *)malloc(sizeof(item));
if(!i)
{
return NULL;
}
if(opening == NO_DATE_SPECIFIED)
{
time_t now = time(NULL);
i->dateOfOpening = now;
} else
{
i->dateOfOpening = opening;
}
char* itemName = strtok(input, ",");
i->itemName = (char *)malloc(strlen(itemName) + 1);
if(!i->itemName)
{
return NULL;
}
i->itemName = strcpy(i->itemName, itemName);
char* days = strtok(NULL, "\n");
if(days != NULL)
{
i->daysToExpire = atoi(days);
} else
{
i->daysToExpire = EXPIRY_DATE;
}
return i;
}
item* parseItem(char* userInput)
{
if(userInput == NULL)
{
fprintf(stderr, "You have not provided any itemname required by the command.");
return NULL;
}
item* it = initItem(userInput, NO_DATE_SPECIFIED);
return it;
}
void deleteItem(FILE* f, item* it)
{
char fline[1024] = {0};
bool exists = false;
FILE* tmpF = fopen(".tmp", "w");
if(!f)
{
return;
}
while(fgets(fline, 1024, f) != NULL)
{
char* itemName = strtok(fline, ";");
if(strcmp(it->itemName, itemName))
{
fprintf(tmpF, "%s;%s\n", itemName, strtok(NULL, "\n"));
} else
{
exists = true;
}
}
if(rename(".tmp", "items.csv") == -1)
{
fprintf(stderr, "An error has occured during the operation 'delete'. (Temp file renaming)");
}
fclose(tmpF);
if(!exists)
{
fprintf(stdout, "'%s': no such item found.\n", it->itemName);
}
}
void addItem(FILE* f, item* it)
{
char fline[1024] = {0};
while(fgets(fline, 1024, f) != NULL)
{
if(!strcmp(strtok(fline, ";"), it->itemName))
{
fprintf(stdout, "This item already exists! (added %s). \nAre you sure you"
" want to add it to the list? [y/n]\n", strtok(ctime(&it->dateOfOpening), "\n"));
char answer[1];
scanf("%s", answer);
if(strcmp(answer, "y"))
{
return;
}
}
if(*fline == '\n')
{
break;
}
}
fprintf(f, "%s;%ld;%u\n", it->itemName, it->dateOfOpening, it->daysToExpire);
}
unsigned int countDaysOpened(time_t timeOfOpening)
{
timeOfOpening -= timeOfOpening % (24*60*60); //the DAY of opening
return (time(NULL) - timeOfOpening) / (24*60*60);
}
void searchItem(FILE* f, item* it)
{
char fline[1024] = {0};
while(fgets(fline, 1024, f) != NULL)
{
char* itemName = strtok(fline, ";");
if(!strcmp(it->itemName, itemName))
{
fprintf(stdout, "MATCH: %s, opened %d days ago.\n", itemName, countDaysOpened(atoi(strtok(NULL, ";"))));
return;
}
}
fprintf(stdout, "'%s': no such item found.\n", it->itemName);
}
void display(FILE* f)
{
char fline[1024] = {0};
fseek(f, 0, SEEK_SET);
while(fgets(fline, 1024, f) != NULL)
{
if(*fline == '\n')
{
continue;
}
char* itemName = strtok(fline, ";");
if(itemName == NULL)
{
continue;
}
time_t timeAdded = atoi(strtok(NULL, "\n"));
fprintf(stdout, "%s (added: %s, %d days ago).\n", itemName, strtok(ctime(&timeAdded), "\n"), countDaysOpened(timeAdded));
}
fprintf(stdout, "\n");
}
/**
* @brief decodeCommand Decodes a command given by user. IMPORTANT NOTE: the userInput
* is stripped away from the \n character, therefore following parsing doesn't have to
* deal with it.
* @param userInput
* @param isRunning
* @param comm
* @return The false return statement is used when user entered nonexistent command.
*/
bool decodeCommand(char* userInput, char* comm)
{
char* command = NULL; // {help, remove, add, search, display, quit}
command = strtok(strtok(userInput, "\n"), " ");
if(command == NULL || !strcmp(command, "quit")) //the input is '\n'
{
return true;
}
if(!strcmp(command, "help"))
{
static char helpInfo[] = "USER HELP FOR ANTIrot PROGRAM.\n"
"Basic commands you can use:\n"
"\t'help' - prints out this help.\n"
"\t'remove' <itemName> - removes an item from the list.\n"
"\t'add' <itemName> <expiry date>(optional)- adds an item from the list. If you want to specify specific expiry date, you must separate the itemname and the number with a comma!\n"
"\t'search' <itemName>\n"
"\t'display' prints out the whole list of items.\n"
"\t'quit' ends this program.\n"
"Please note that this program (for the time being)\
does not deal with duplicit items.\
\n\n";
fprintf(stdout, "%s", helpInfo);
} else if(!strcmp(command, "remove") || !strcmp(command, "add") \
|| !strcmp(command, "search") || !strcmp(command, "display"))
{
*comm = command[0];
if(*comm != 'd')
{
char* remainder = NULL;
remainder = strtok(NULL, "\n");
if(remainder == NULL)
{
return false;
}
strcpy(userInput, remainder);
}
} else
{
return false;
}
return true;
}
void performRotCheck()
{
FILE* f = fopen(ITEMLIST, "r");
if(f == NULL)
{
perror(ITEMLIST);
return;
}
char fline[1024] = {0};
while(fgets(fline, 1024, f) != NULL)
{
char* name = strtok(strtok(fline, "\n"), ";");
char* date = strtok(NULL, ";");
unsigned int daysToExpire = atoi(strtok(NULL, ";"));
if(date == NULL)
{
continue;
}
time_t timeOpened = atoi(date);
static unsigned int daysOpened;
if ((daysOpened = countDaysOpened(timeOpened)) > daysToExpire)
{
printf("WARNING: %s is more than %d days old! (%d days)\n", name, EXPIRY_DATE, daysOpened);
}
}
fclose(f);
}
bool performCommand(char command, item* it)
{
FILE* f = fopen(ITEMLIST, "a+");
assert(f != NULL);
switch(command)
{
case'r':
{
deleteItem(f, it);
fclose(f);
} //remove command
case 'd': //this part of display is the same as for the remove part
f = fopen(ITEMLIST, "r");
display(f);
break;
case'a':
{
addItem(f, it);
display(f);
break;
} //add command
case's':
{
searchItem(f, it);
break;
} //search command
default:
{
break;
}
}
fclose(f);
return true;
}
void releaseItem(item** it)
{
item* it_aux = *it;
if(*it == NULL)
{
return;
}
if(it_aux->itemName != NULL)
{
free(it_aux->itemName);
}
free(*it);
*it = NULL;
}
int main(void)
{
bool isRunning = true;
char* pUserInput;
char* userInput = NULL;
item* it = NULL;
userInput = malloc(sizeof(char)*1024);
assert(userInput != NULL);
pUserInput = userInput;
performRotCheck();
system(CALL_DOWNLOAD_SCRIPT);
while(isRunning)
{
printf(">>");
char command = 0;
fgets(userInput, MAX_COMMAND_LEN-1, stdin);
if(!decodeCommand(userInput, &command))
{
printf("\tCommand error.\n");
continue;
} else if(command != 0)
{
it = parseItem(userInput);
if(it == NULL)
{
printf("The creation of item you provided was not successful.\n");
continue;
}
if(!performCommand(command, it))
{
fprintf(stderr, "Error.\n");
}
} else {
isRunning = false;
}
releaseItem(&it);
}
free(pUserInput);
}