diff options
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 21 |
1 files changed, 17 insertions, 4 deletions
@@ -19,9 +19,9 @@ struct Post { time_t timestamp;
User * user;
char content[CONTENT_LEN];
- int likes;
+ uint32_t likes;
Post * comments;
- Post * parent;
+ //Post * parent;
};
struct User {
@@ -42,6 +42,12 @@ PostWrite(Post * post, FILE * f) { fwrite(&post->timestamp, 8, 1, f);
fwrite(post->user->name, 1, sizeof(post->user->name), f);
fwrite(post->content, 1, sizeof(post->content), f);
+ fwrite(&post->likes, 4, 1, f);
+ uint32_t numComments = arrlen(post->comments);
+ fwrite(&numComments, 4, 1, f);
+ for (int i = 0; i < numComments; i++) {
+ PostWrite(&post->comments[i], f);
+ }
}
void
@@ -51,6 +57,15 @@ PostRead(Post * post, FILE * f) { fread(name, 1, sizeof(name), f);
post->user = UsersFind(mg_str(name));
fread(post->content, 1, sizeof(post->content), f);
+ fread(&post->likes, 4, 1, f);
+ uint32_t numComments;
+ fread(&numComments, 4, 1, f);
+ post->comments = NULL;
+ for (int i = 0; i < numComments; i++) {
+ Post newComment = PostNew();
+ PostRead(&newComment, f);
+ arrput(post->comments, newComment);
+ }
}
User
@@ -339,7 +354,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) Post newPost;
newPost.likes = 0;
newPost.comments = NULL;
- newPost.parent = NULL;
newPost.timestamp = time(NULL);
newPost.user = user;
mg_http_get_var(&hm->body, "content", newPost.content, sizeof(newPost.content)-1);
@@ -355,7 +369,6 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) Post newComment;
newComment.likes = 0;
newComment.comments = NULL;
- newComment.parent = parent;
newComment.timestamp = time(NULL);
newComment.user = user;
mg_http_get_var(&hm->body, "content", newComment.content, sizeof(newComment.content)-1);
|
