-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.c
206 lines (190 loc) · 5.2 KB
/
post.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
#include "StructHeader.h"
/*
Description: Generates a unique post number. One that is bigger than them all.
Inputs:
1. Dummy of the posts linked list
Output:
The generated post ID
Side Effects:
-
*/
int GeneratePostNumber(Post* dummy)
{
int res = 0;
if(dummy == NULL)
return res;
if(dummy->Next == NULL)
return res;
Post* current = dummy->Next;
while(current != NULL)
{
if(current->PostID >= res)
{
res = current->PostID;
}
current = current->Next;
}
res ++;
return res;
}
/*
Description: Sends a post, adding it to the linked list of posts of the chosen user
Inputs:
1. Dummy of that user's posts linked list
2. ID of the sender user
3. Post's text
Output:
An integer that will be 0 if the operation fails and 1 otherwise
Side Effects:
One post with the given text will be addedd to the linked list of the user
*/
int SendPost(Post* dummy, char* senderID, char* text)
{
if(dummy == NULL)
{
return 0;
}
int id = GeneratePostNumber(dummy);
Post* pst = malloc(sizeof (Post));
pst->Next = NULL;
pst->Likes = 0;
pst->PostID = id;
pst->Text = calloc(strlen(text)+1,sizeof (char));
strcpy(pst->Text,text);
pst->Text[strlen(text)] = '\0';
pst->SenderID = calloc(strlen(senderID)+1,sizeof (char));
strcpy(pst->SenderID,senderID);
pst->SenderID[strlen(senderID)] = '\0';
Post* current = dummy;
while(current->Next != NULL)
{
current = current->Next;
}
pst->PrimaryKey = malloc((strlen(senderID)+DigitCount(id)+2)*sizeof (char));
strcpy(pst->PrimaryKey,senderID);
strcat(pst->PrimaryKey,"_");
char* charid = malloc(DigitCount(id)+1);
itoa(id,charid,10);
strcat(pst->PrimaryKey,charid);
pst->PrimaryKey[strlen(senderID)+DigitCount(id)+1] = '\0';
current->Next = pst;
free(charid);
return 1;
}
/*
Description: Sends a post to list of liked post, making it possible to find out which posts the user has liked
Inputs:
1. Dummy of that user's posts linked list
2. ID of the sender user
3. ID of the post
Output:
An integer that will be 0 if the operation fails and 1 otherwise
Side Effects:
One post with a primary key based on given information will be addedd to liked list of a user's liked posts
*/
int SendToLikedPosts(Post* dummy, char *senderID, int postid)
{
if(dummy == NULL)
{
return 0;
}
int id = postid;
Post* pst = malloc(sizeof (Post));
pst->Next = NULL;
pst->SenderID = calloc(strlen(senderID)+1,sizeof (char));
strcpy(pst->SenderID,senderID);
pst->SenderID[strlen(senderID)] = '\0';
Post* current = dummy;
while(current->Next != NULL)
{
current = current->Next;
}
pst->PrimaryKey = malloc((strlen(senderID)+DigitCount(id)+2)*sizeof (char));
strcpy(pst->PrimaryKey,senderID);
strcat(pst->PrimaryKey,"_");
char* charid = malloc(DigitCount(id)+1);
itoa(id,charid,10);
strcat(pst->PrimaryKey,charid);
pst->PrimaryKey[strlen(senderID)+DigitCount(id)+1] = '\0';
current->Next = pst;
free(charid);
return 1;
}
/*
Description: Deletes a post
Inputs:
1. dummy of posts
2. post id
Output:
1: if operation was successfuly done
0: otherwise
Side Effects:
One post with the given text will be addedd to the linked list of the user
*/
int DeletePost(Post* dummy, int postid)
{
Post* cur = FindPost(dummy,postid,PreviousElement);
if(cur == NULL)
return 0;
Post* deleter = cur->Next;
cur->Next = deleter->Next;
free(deleter);
return 1;
}
/*
Description: Edits a post
Inputs:
1. dummy of posts
2. post id
3. new post text
Output:
1: if operation was successfuly done
0: otherwise
Side Effects:
the post with the given postID will be edited
*/
int EditPost(Post* dummy, int postid, char* text)
{
Post* cur = FindPost(dummy,postid,CurrentElement);
if(cur == NULL)
return 0;
free(cur->Text);
cur->Text = calloc(strlen(text)+1,sizeof (char));
strcpy(cur->Text,text);
cur->Text[strlen(text)] = '\0';
return 1;
}
/*
Description: Prints a post, number of its likes, and its sender's ID
Inputs:
1. Address of the post that is going to be printed
Output:
-
Side Effects:
The mentioned information will be printed
*/
void PrintPost(Post* post)
{
printf("Post_ID: %d\nPrimary Key: %s\nLikes: %d\nText: %s\n",post->PostID,post->PrimaryKey,post->Likes,post->Text);
}
/*
Description: Generates post primary key
Inputs:
1. Sender ID
2. Post ID
Output:
a dynamic char array containing generated PK(Primary Key)
Side Effects:
-
*/
char* GeneratePostPK(char* senderid, int postid)
{
char* result = malloc(strlen(senderid)+DigitCount(postid)+2);
strcpy(result,senderid);
char* num = malloc((DigitCount(postid)+1)*sizeof (char));
itoa(postid,num,10);
strcat(result,"_");
strcat(result,num);
free(num);
return result;
}