-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha2_functions.c
More file actions
337 lines (251 loc) · 8.68 KB
/
a2_functions.c
File metadata and controls
337 lines (251 loc) · 8.68 KB
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
/*****************
Student Name = Marwan Alakhras
Student Number = 101296201
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <time.h>
#include "a2_nodes.h"
#include "a2_functions.h"
#include <assert.h>
#include <ctype.h>
// Your solution goes here
/*
******** DONT MODIFY THIS FUNCTION ********
Function that reads users from the text file.
IMPORTANT: This function shouldn't be modified and used as is
******** DONT MODIFY THIS FUNCTION ********
*/
user_t *read_CSV_and_create_users(FILE *file, int num_users)
{
srand(time(NULL));
user_t *users = NULL;
char buffer[500];
fgets(buffer, sizeof(buffer), file); // Read and discard the header line
int count = 0;
for (int i = 0; i < num_users; i++)
{
fgets(buffer, sizeof(buffer), file);
buffer[strcspn(buffer, "\r\n")] = 0; // Remove newline characters
char *token = strtok(buffer, ",");
char *token2 = strtok(NULL, ",");
users = add_user(users, token, token2);
char *username = token;
token = strtok(NULL, ",");
user_t *current_user = users;
for (; current_user != NULL && strcmp(current_user->username, username) != 0; current_user = current_user->next)
;
while (token != NULL && strcmp(token, ",") != 0 && count < 3)
{
if (strcmp(token, " ") != 0)
{
add_friend(current_user, token);
}
token = strtok(NULL, ",");
count++;
}
count = 0;
// token = strtok(NULL, ",");
while (token != NULL && strcmp(token, ",") != 0)
{
add_post(current_user, token);
token = strtok(NULL, ",");
}
}
return users;
}
void print_menu(){
printf("***********************************\n");
printf("\t MAIN MENU\n");
printf("***********************************\n");
printf("1. Register a new user\n");
printf("2. Login with an existing user's information\n");
printf("3. Exit\n");
}
user_t *add_user(user_t *users, const char *username, const char *password){
user_t *newuser = malloc(sizeof(user_t));
assert(newuser != NULL);
strcpy(newuser->username, username);
strcpy(newuser->password, password);
newuser->friends = NULL;
newuser->posts = NULL;
newuser->next = NULL;
if (users == NULL || strcmp(username, users->username) < 0){//Inserts at head of list
newuser->next = users;
return newuser;
}
user_t *current_user = users;
while (current_user->next != NULL && strcmp(current_user->next->username, username) < 0){
current_user = current_user->next; //Traverses the list up to the point the newuser must be inserted
}
newuser->next = current_user->next;
current_user->next = newuser;
return users;
}
user_t *find_user(user_t *users, const char *username){
for(user_t *current_user = users ;current_user!=NULL; current_user=current_user->next){
if (strcmp(current_user->username , username) == 0){ //User was found
return current_user;
}
}
return NULL; //User was not found
}
void add_post(user_t *user, const char *text){
assert(user !=NULL);
post_t *newpost = create_post(text);
assert(newpost !=NULL);
newpost->next = user->posts;
user->posts = newpost;
}
post_t *create_post(const char *text){
post_t *newpost = malloc(sizeof(post_t));
assert(newpost!=NULL);
strcpy(newpost->content, text);
newpost->next = NULL;
return newpost;
}
void display_all_user_posts(user_t *user){
post_t *current_user = user->posts;
for(int i = 1 ; current_user != NULL; current_user = current_user->next, i++){
printf("%i - %s\n", i , current_user->content);
}
}
_Bool delete_post(user_t *user){
post_t *to_delete = user->posts;
if(to_delete == NULL){//No posts to delete
return false;
}
user->posts = to_delete->next;
free(to_delete);
to_delete = NULL;
return true;
}
void add_friend(user_t *user, const char *friend){
assert(user != NULL);
friend_t *newfriend = create_friend(friend);
assert(newfriend !=NULL);
newfriend->posts = &(find_user(user, friend)->posts);
if (user->friends == NULL || strcmp(friend, user->friends->username) < 0 ){
newfriend->next = user->friends;
user->friends = newfriend;
return;
}
friend_t *current_user = user->friends;
while(current_user->next != NULL && strcmp(current_user->next->username, friend ) < 0){
current_user = current_user->next; //Traverses the linked list up to the point the newfriend must be inserted.
}
newfriend->next = current_user->next;
current_user->next = newfriend;
}
friend_t *create_friend(const char *username){
friend_t *newfriend = malloc(sizeof(friend_t));
assert(newfriend != NULL);
strcpy(newfriend->username, username);
newfriend->next = NULL;
newfriend->posts = NULL;
return newfriend;
}
_Bool delete_friend(user_t *user, char *friend_name){
friend_t *to_delete = user->friends;
if(to_delete == NULL){//No friend to delete because user has no friends
return false;
}
if (strcmp(to_delete->username , friend_name) == 0){ //If the friend is at the head of the linked list
user->friends = to_delete->next;
free(to_delete);
to_delete = NULL;
return true;
}
for(friend_t *previous = NULL ,*current_user = user->friends ; current_user != NULL ; previous = current_user, current_user= current_user->next){
if (strcmp(current_user->username , friend_name) == 0){
to_delete = current_user;
previous->next = current_user->next;
free(to_delete);
to_delete = NULL;
return true;
}
}
return false;
}
void display_user_friends(user_t *user){
friend_t *current_user = user->friends;
for(int i = 1 ; current_user != NULL; current_user = current_user->next, i++){
printf("%i - %s\n", i , current_user->username);
}
}
_Bool is_friend(user_t *user, char *friend_name){
for (friend_t *current_user = user->friends; current_user != NULL; current_user = current_user->next){
if(strcmp(current_user->username, friend_name) == 0){
return true; //friend found
}
}
return false; //friend not found
}
void display_posts_by_n(user_t *users, int number){
if(users == NULL){ //User not found
printf("User not found.\n");
return;
}
else{ //User found
post_t *current_user = users->posts;
int counter = 1;
char choice;
if(current_user == NULL){
printf("User has no posts.\n");
}
while(current_user!= NULL){
printf("%s's posts:\n", users->username);
for (int i = 1; i<=number && current_user != NULL; i++, current_user= current_user->next){
printf("%i - %s\n", counter++, current_user->content);
}
if(current_user == NULL){
printf("All posts have been displayed.\n");
break;
}
while(true){
printf("Do you want to display more posts? (Y/N/y/n): ");
scanf(" %c", &choice);
if(toupper(choice) == 'Y'){
break; //Continue displaying posts
}
else if (toupper(choice) == 'N'){
return; //Return to main menu
}
else{
printf("Invalid input. ");
}
}
}
}
}
void teardown(user_t *users){
user_t *current_user = users;
while(users!=NULL){ //Freeing each user
friend_t *current_friend; //Freeing the user's friends
while(current_user->friends != NULL){
current_friend = current_user->friends->next;
free(current_user->friends);
current_user->friends = current_friend;
}
current_user->friends = NULL;
post_t *current_post; //Freeing the user's posts
while(current_user->posts != NULL){
current_post = current_user->posts->next;
free(current_user->posts);
current_user->posts = current_post;
}
current_user->posts = NULL;
current_user = users->next;
free(users);
users = current_user;
}
}
char * make_lowercase(char * username){
for (int i = 0; i < strlen(username); i++){
username[i] = tolower(username[i]);
return username;
}
}