-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCA3_me.c
486 lines (460 loc) · 16.3 KB
/
CA3_me.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define FALSE 0
#define TRUE 1
#define MIN_ARRAY_LEN 2
/*...................
Author : Mahdy Mokhtari
Student's ID : 810101515
file : CA3
Date : 1401 / 10 / 11
....................*/
typedef struct content content;
typedef struct toti_user toti_user;
struct content{
int post_id;
int like;
char* post;
//char** who_liked;
content* next;
};
struct toti_user {
content* my_content;
char* user_name;
char* password;
int num_of_posts;
toti_user* next;
};
char* command_getter(int*);
char* part2_getter(int*);
char* part3_getter(int*);
int decoder (int*, int, char*, char*, char*, toti_user*, toti_user**, toti_user**);
toti_user* signup(char*, char*, toti_user**, toti_user**, toti_user*);
toti_user* login(char*, char*, toti_user**, toti_user**, toti_user*);
toti_user* post(char*, char*, toti_user**, toti_user**, toti_user*);
toti_user* like(char*, char*, toti_user**, toti_user**, toti_user*);
toti_user* logout(toti_user**, toti_user**, toti_user*);
toti_user* delete(char*, toti_user**, toti_user**, toti_user*);
toti_user* info(toti_user**, toti_user**, toti_user*);
toti_user* find_user(char*, toti_user**, toti_user**, toti_user*);
void design(void);
void print_in_file(toti_user*);
int main() {
int flag_login = 0, counter = 0;
design();//////kamel kon
toti_user** tail_users;
tail_users = (toti_user**)malloc(sizeof(toti_user*));
*tail_users = (toti_user*)malloc(sizeof(toti_user));
if(tail_users == NULL) {
printf("coulnt malloc in main\n");
}
toti_user** node_cur_users;
node_cur_users = (toti_user**)malloc(sizeof(toti_user*));
*node_cur_users = *tail_users;
if(node_cur_users == NULL) {
printf("coulnt malloc in main\n");
}
toti_user* head_users;
head_users = *tail_users;
head_users->next = NULL;
char *command, *part2, *part3;
while(TRUE) {
int num_last_part = 0, check_decoder = -1;
command = command_getter(&num_last_part); /*getting the command*/
if(num_last_part == 0) { /*if we have more than one word we will put the second word in part2*/
part2 = part2_getter(&num_last_part);
if(num_last_part == 0) { /*if we have more than 2 words we will get all the rest as a string in part3*/
part3 = part3_getter(&num_last_part);
}
}
printf("command:%s part2:%s part3:%s num_last_part:%d\n", command, part2, part3, num_last_part);
check_decoder = decoder(&flag_login, num_last_part, command, part2, part3, head_users, node_cur_users, tail_users);
if(check_decoder == FALSE) {
printf("not a correct command\n");
}
fflush(stdin);
if(counter != 0) {
if(num_last_part == 3) {
free(command), free(part2), free(part3);
}
else if(num_last_part == 2) {
free(command), free(part2);
}
else {
free(command);
}
}
counter++;
print_in_file(head_users);
}
}
void design(void) {
system("cls");
printf(".........................Hi, Wellcome to UT_TOTI...........................\n\nwrite your command...\n");
}
char* command_getter(int* num_last_part) {
char* command = (char*)malloc(sizeof(char) * MIN_ARRAY_LEN);
if(command == NULL) {
printf("couldnt malloc comand\n");
exit(0);
}
char temp;
int malloc_size = MIN_ARRAY_LEN;
while(((temp = getchar()) != ' ') && (temp != '\n')) {
command = realloc(command, sizeof(char) * malloc_size);
if(command == NULL) {
printf("couldnt realloc part3\n");
exit(0);
}
command[malloc_size - 2] = temp;
malloc_size++;
}
if(temp == '\n') {
(*num_last_part) = 1;
}
command[malloc_size - 2] = '\0';
return command;
}
char* part2_getter(int* num_last_part) {
char* part2 = (char*)malloc(sizeof(char) * MIN_ARRAY_LEN);
char temp;
int malloc_size = MIN_ARRAY_LEN;
if(part2 == NULL) {
printf("couldnt malloc part2\n");
exit(0);
}
while(((temp = getchar()) != ' ') && (temp != '\n')) {
part2 = realloc(part2, sizeof(char) * malloc_size);
if(part2 == NULL) {
printf("couldnt realloc part3\n");
exit(0);
}
part2[malloc_size - 2] = temp;
malloc_size++;
}
if(temp == '\n') {
(*num_last_part) = 2;
}
part2[malloc_size - 2] = '\0';
return part2;
}
char* part3_getter(int* num_last_part) {
char* part3 = (char*)malloc(sizeof(char) * MIN_ARRAY_LEN);
char temp;
int malloc_size = MIN_ARRAY_LEN;
if(part3 == NULL) {
printf("couldnt malloc part3\n");
exit(0);
}
while((temp = getchar()) != '\n') {
part3 = realloc(part3, sizeof(char) * malloc_size);
if(part3 == NULL) {
printf("couldnt realloc part3\n");
exit(0);
}
part3[malloc_size - 2] = temp;
malloc_size++;
}
if(temp == '\n') {
(*num_last_part) = 3;
}
part3[malloc_size - 2] = '\0';
fflush(stdin);
return part3;
}
int decoder(int* flag_login, int num_last_part, char* command, char* part2, char* part3, toti_user* head_users, toti_user** node_cur_users, toti_user** tail_users) { /*finding witch command the user gave us and doing it*/
if(strcmp("signup", command) == 0) {
if(*flag_login == 1 || num_last_part != 3) {
return FALSE;
}
else {
toti_user* check = head_users->next;
while (check != NULL) {
if(strcmp(check->user_name, part2) == 0) {
printf("The username you picked is used\n");
return FALSE;
}
check = check->next;
}
*flag_login = 0;
*node_cur_users = signup(part2, part3, tail_users, node_cur_users, head_users);
}
}
else if(strcmp("login", command) == 0) {
if(*flag_login == 1 || num_last_part != 3) {
return FALSE;
}
else {
*node_cur_users = login(part2, part3, tail_users, node_cur_users, head_users);
if(*node_cur_users == head_users) {
printf("The username or password is incorrect\n");
return FALSE;
}
else {
*flag_login = 1;
}
}
}
else if(strcmp("post", command) == 0) {
if((*flag_login) == 0 || num_last_part < 2) {
return FALSE;
}
else {
*flag_login = 1;
*node_cur_users = post(part2, part3, tail_users, node_cur_users, head_users);
}
}
else if(strcmp("info", command) == 0) {
if( num_last_part != 1) {
return FALSE;
}
else {
*flag_login = 1;
*node_cur_users = info(tail_users, node_cur_users, head_users);
}
}
else if(strcmp("like", command) == 0) {
if(*flag_login == 0 || num_last_part != 3) {
return FALSE;
}
else {
*flag_login = 1;
*node_cur_users = like(part2, part3, tail_users, node_cur_users, head_users);
}
}
else if(strcmp("logout", command) == 0) {
if(*flag_login == 0 || num_last_part != 1) {
return FALSE;
}
else {
*flag_login = 0;
*node_cur_users = logout(tail_users, node_cur_users, head_users);
}
}
else if(strcmp("delete", command) == 0) {
if(*flag_login == 0 || num_last_part != 2) {
return FALSE;
}
else {
*flag_login = 1;
*node_cur_users = delete(part2, tail_users, node_cur_users, head_users);
}
}
else if(strcmp("find_user", command) == 0) {
if(*flag_login == 0 || num_last_part != 2) {
return FALSE;
}
else {
*flag_login = 1;
*node_cur_users = find_user(part2, tail_users, node_cur_users, head_users);
}
}
else {
return FALSE;
}
return TRUE;
}
/*the return of all the bellow functions is the current node that we are logged in to or if we are not its the head of nodes*/
toti_user* signup(char* part2, char* part3, toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
int part2_size, part3_size;
(**tail_users).next = (toti_user*)malloc(sizeof(toti_user));
if((**tail_users).next == NULL) {
printf("couldnt malloc signup\n");
}/*writing the users information and saving it to signup*/
*tail_users = (**tail_users).next;
(**tail_users).next = NULL;
part2_size = strlen(part2);
part3_size = strlen(part3);
(**tail_users).user_name = (char*)malloc(sizeof(char) * part2_size);
(**tail_users).password = (char*)malloc(sizeof(char) * part3_size);
if((**tail_users).password == NULL || (**tail_users).user_name == NULL) {
printf("couldnt malloc in user or pass in signup\n");
exit(0);
}
strcpy((**tail_users).user_name, part2);
strcpy((**tail_users).password, part3);
(**tail_users).num_of_posts = 0;
(**tail_users).my_content = (content*)malloc(sizeof(content));
((**tail_users).my_content)->next = NULL;
if((**tail_users).my_content == NULL) {
printf("couldnt malloc my_content or who_liked in signup\n");
exit(0);
}
printf("signed up successefully\n");
return head_users;
}
toti_user* login(char* part2, char* part3, toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
toti_user *cur = head_users;
toti_user * prev = NULL;
while(cur != NULL) {/*finding the account that we want to signup in*/
if(strcmp(cur->user_name, part2) == 0 && strcmp(cur->password, part3) == 0) {
*node_cur_users = cur;
printf("logged in to: %s\n", (**node_cur_users).user_name);
return *node_cur_users;
}
prev = cur;
cur = cur->next;
}
return head_users;
}
toti_user* post(char* part2, char* part3, toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
content* cur = (**node_cur_users).my_content;
content* prev = NULL;
int flag = 0;
if(cur->next == NULL) {
flag = 1;
}
while (cur != NULL) { //finding the first empty node in the posts linked list
prev = cur;
cur = cur->next;
}/*adding the post to our content list*/
(**node_cur_users).num_of_posts++;
prev->next = (content*)malloc(sizeof(content));
(prev->next)->next = NULL;
(prev->next)->like = 0;
if (flag == 1) { //first post the it posted
(prev->next)->post_id = 1;
}
else {
(prev->next)->post_id = ((prev->post_id) + 1);
}
(prev->next)->post = (char*)malloc(sizeof(char) * (strlen(part2) + strlen(part3) + 1));
strcpy((prev->next)->post, part2);
strcat((prev->next)->post, " ");
strcat((prev->next)->post, part3);
return *node_cur_users;
}
toti_user* like(char* part2, char* part3, toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
toti_user* cur_user = head_users;
toti_user* prev_user = NULL;
int flag = 0; // this flag if it gets 1 means that we liked if 0 we didnt
while(cur_user != NULL) { // finding the user that has the post we want to like
if(strcmp(cur_user->user_name, part2) == 0) {
content* cur_content = cur_user->my_content;
content* prev_content = NULL;
while (cur_content != NULL) { // finding the post that we want to like
if(cur_content->post_id == atoi(part3)) {
cur_content->like++; //we havent strengthened repetitious likes
flag = 1;
break;
}
prev_content = cur_content;
cur_content = cur_content->next;
}
break;
}
prev_user = cur_user;
cur_user = cur_user->next;
}
if(flag == 1) {
printf("post liked successfuly\n");
}
else {
printf("post didnt got liked\n");
}
return *node_cur_users;
}
toti_user* info(toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
int flag = 0;
content* cur = ((**node_cur_users).my_content)->next;
content* prev = (**node_cur_users).my_content;
printf("Username: %s\n",(**node_cur_users).user_name);
printf("password: %s\n", (**node_cur_users).password);
while(cur != NULL) {
printf("post: %s\n", cur->post);
printf("post_id: %d\n", cur->post_id);
printf("like: %d\n", cur->like);
prev = cur;
cur = cur->next;
printf("\n");
flag = 1;
}
if(flag == 0) {
printf("you have no posts yet\n");
}
return *node_cur_users;
}
toti_user* logout(toti_user** tail, toti_user** node_cur_users, toti_user* head_users) {
*node_cur_users = head_users;
printf("You have logged out successfuly\n");
return *node_cur_users;
}
toti_user* delete(char* part2, toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
content* cur = (**node_cur_users).my_content;
content* prev = NULL;
int flag = 0;
while(cur != NULL) {
if(cur->post_id == atoi(part2)) { //the post_id we want is found and deleted
flag = 1;
prev->next = cur->next;
free(cur->post);
free(cur);
break;
}
prev = cur;
cur = cur->next;
}
if(flag == 0) {
printf("You dont have that post_id\n");
}
else {
(**node_cur_users).num_of_posts--;
printf("post deleted successfuly\n");
}
return *node_cur_users;
}
toti_user* find_user(char* part2, toti_user** tail_users, toti_user** node_cur_users, toti_user* head_users) {
toti_user* cur_user = head_users;
toti_user* prev_user = NULL;
while(cur_user != NULL) {
if(strcmp(cur_user->user_name, part2) == 0) { //the username got matched and now we are printing its info
printf("Username: %s\n", cur_user->user_name);
int flag = 0;
content* cur = (cur_user->my_content)->next;
content* prev = cur_user->my_content;
while(cur != NULL) {
printf("post: %s\n", cur->post);
printf("post_id: %d\n", cur->post_id);
printf("like: %d\n", cur->like);
prev = cur;
cur = cur->next;
printf("\n");
flag = 1;
}
if(flag == 0) {
printf("you have no posts yet\n");
}
break;
}
prev_user = cur_user;
cur_user = cur_user->next;
}
return *node_cur_users;
}
void print_in_file(toti_user* head_users) {
FILE* account = fopen("C:\\Users\\MahdyBeast\\OneDrive\\Desktop\\programing\\C1\\.vscode\\CA3\\account.txt", "w");
FILE* post = fopen("C:\\Users\\MahdyBeast\\OneDrive\\Desktop\\programing\\C1\\.vscode\\CA3\\post.txt", "w");
if(post == NULL || account == NULL) {
printf("couldnt fopen\n");
exit(0);
}
toti_user* cur_user = head_users->next;
while(cur_user != NULL) {
int check_1 = fprintf(account, "%s %s %d\n", cur_user->user_name, cur_user->password, cur_user->num_of_posts);
if(check_1 < 0) {
printf("couldnt fprintf account.\n");
}
content* cur_content = (cur_user->my_content)->next;
while(cur_content != NULL) {
int check_2 = fprintf(post, "%s %s %d\n", cur_content->post, cur_user->user_name, cur_content->like);
if(check_2 < 0) {
printf("couldnt fprintf account.\n");
}
cur_content = cur_content->next;
}
cur_user = cur_user->next;
}
fclose(account);
fclose(post);
}