From 946611dc64f4fc028f73e30dead13228cf85da21 Mon Sep 17 00:00:00 2001 From: Patrick Date: Mon, 28 Aug 2023 03:53:08 +0200 Subject: [PATCH] read/write nested subcomments --- src/main.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 4c994d9..2747fde 100644 --- a/src/main.c +++ b/src/main.c @@ -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); -- 2.50.1