-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
286 lines (265 loc) · 6.29 KB
/
user.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
#include "StructHeader.h"
/*
Description: Finds address of last node in a linked list
Input:
1. any element in the list
Output:
Address of the last element
Side effects:
-
*/
User* FindLastUser(User* first)
{
while (first->Next != NULL)
{
first = first->Next;
}
return first;
}
/*
Description: Adds a user with given features and 0 posts to Users List
Input:
1. dummy of users list
2. number of users already signed up
3. username of the user that's going to be added
4. password of the user that's going to be added
Output:
-
Side effects:
one user will be added to user list
*/
int SignUpUser(User* dummy, char* username, char* password)
{
User* current = dummy;
if(dummy->Next != NULL)
{
do
{
current = current->Next;
if(strcmp(current->Username,username) == 0)
{
return 0;
}
}
while(current->Next != NULL);
}
current->Next = (User*)malloc(sizeof (User));
current = current->Next;
if(current == NULL)
{
return 0;
}
int un_size = 0, pw_size = 0;
while (username[un_size] != '\0')
{
un_size ++;
}
while (password[pw_size] != '\0')
{
pw_size ++;
}
current->Username = (char*)calloc(un_size+1, sizeof (char));
current->Password = (char*)calloc(pw_size+1, sizeof (char));
for(int i=0;i<=un_size;i++)
{
current->Username[i] = username[i];
}
for(int i=0;i<=pw_size;i++)
{
current->Password[i] = password[i];
}
current->PostsDummy = malloc(sizeof (Post));
current->PostsDummy->Next = NULL;
current->Next = NULL;
current->LikedPostsDummy = malloc(sizeof (Post));
current->LikedPostsDummy->Next = NULL;
return 1;
}
/*
Description: Deletes all posts of the user
Inputs:
pointer to the user
Output:
-
Side Effects:
-
*/
void DeletePosts(User* user)
{
if(user == NULL)
return;
Post* posts = user->PostsDummy;
if(posts == NULL)
return;
posts = posts->Next;
while(posts != NULL)
{
Post* nxt = posts->Next;
free(posts);
posts = nxt;
}
posts = user->LikedPostsDummy;
if(posts == NULL)
return;
posts = posts->Next;
while(posts != NULL)
{
Post* nxt = posts->Next;
free(posts);
posts = nxt;
}
}
/*
Description: Deletes a user's account and all its belongings including its posts
Inputs:
1. dummy of the users linked list
2. username of the user about to get deleted
Output:
1: if the operation was done successfully
0: otherwise
Side Effects:
-
*/
int DeleteAccount(User* dummy, char* username)
{
if(dummy == NULL)
return 0;
if(dummy->Next == NULL)
return 0;
User* cur = dummy;
while(cur->Next != NULL)
{
if(strcmp(cur->Next->Username,username) == 0 )
{
User* deleter = cur->Next;
cur->Next = deleter->Next;
DeletePosts(deleter);
free(deleter);
return 1;
}
cur = cur->Next;
}
return 0;
}
/*
Description: Finds user based on username
Inputs:
1. dummy of the users linked list
2. username of the user about to get deleted
3. Search Flag
Output:
NULL if the user doesn't exist
address of the user if exists and flag is CurrentElement
address of the previous user if exists and flag is PrevoiusElement
Side Effects:
-
*/
User* FindUser(User* dummy,char* username , SearchFlag flag)
{
if(dummy == NULL)
return NULL;
if(dummy->Next == NULL)
return NULL;
User* cur = dummy;
while(cur->Next != NULL)
{
if(strcmp(cur->Next->Username,username) == 0)
{
if(flag == PreviousElement)
return cur;
return cur->Next;
}
cur = cur->Next;
}
return NULL;
}
/*
Description: Finds a post using the same searching flag algorithm as FindUser
Inputs:
1. dummy of posts
2. id of the post
3. searching flag
Output:
same as FindUser, only post instead of user
Side Effects:
-
*/
Post* FindPost(Post* dummy, int id, SearchFlag flag)
{
if(dummy == NULL)
return NULL;
if(dummy->Next == NULL)
return NULL;
Post* cur = dummy;
while(cur->Next != NULL)
{
if( cur->Next->PostID == id)
{
if(flag == PreviousElement)
return cur;
return cur->Next;
}
cur = cur->Next;
}
return NULL;
}
/*
Description: Finds address of last node in a linked list
Input:
1. dummy of the users linked list
2. ID of the post's sender
3. post's ID
Output:
1 if operation was performed successfully, 0 otherwise
Side effects:
the number of likes a post has will increase by 1
*/
int LikePost(User* dummy, char* senderId, int postId)
{
User* user = FindUser(dummy,senderId,CurrentElement);
if(user == NULL)
return 0;
Post* post = FindPost(user->PostsDummy,postId,CurrentElement);
if(post == NULL)
return 0;
post->Likes++;
return 1;
}
/*
Description: Prints a user's information
Input:
1. address of the mentioned user
Output:
-
Side effects:
information of the user might be printed
*/
void PrintUser(User* user, UserPrintFlag flag)
{
if(user == NULL)
return;
printf("Username: %s\n",user->Username);
if(flag == ShowPassword)
{
printf("Password: %s\n",user->Password);
}
else if(flag == ShowStars)
{
printf("Password: ");
for(unsigned int i=0;i<strlen(user->Password);i++)
{
printf("%c",'*');
}
puts("");
}
Post* dum = user->PostsDummy;
if(dum == NULL)
return;
printf("%30s%c%c\n","Posts",25,25);
while(dum->Next != NULL)
{
PrintPost(dum->Next);
dum = dum->Next;
puts("=========================================================");
}
}