diff options
| author | Patrick | 2023-08-28 03:53:08 +0200 |
|---|---|---|
| committer | Patrick | 2023-08-28 03:53:08 +0200 |
| commit | 946611dc64f4fc028f73e30dead13228cf85da21 (patch) | |
| tree | d4bd1445067e0455f16b4f1aae31b7227c534d01 /src | |
| parent | e8417ccf8f4a902e362f7e76af318849d2695d18 (diff) | |
| download | chirp-946611dc64f4fc028f73e30dead13228cf85da21.tar.gz chirp-946611dc64f4fc028f73e30dead13228cf85da21.zip | |
read/write nested subcomments
Diffstat (limited to 'src')
| -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);
|
