From 0cde2418cceda72e678721efa3e8841c66874b39 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Mon, 31 Jan 2022 04:16:26 +0000 Subject: [PATCH] Refactor json code; account info FossilOrigin-Name: 51b7efb786dd9f079897888ade3ce7de78177c52bce7f0e3ec86288b40daa804 --- include/mastodont_account.h | 34 +++++++++++++++++-- include/mastodont_json_helper.h | 40 +++++++++++++++++++++++ include/mastodont_status.h | 1 + src/account.c | 29 +++++++++++++++-- src/json_helper.c | 48 +++++++++++++++++++++++++++ src/status.c | 58 +++++++++++++++++---------------- 6 files changed, 177 insertions(+), 33 deletions(-) create mode 100644 include/mastodont_json_helper.h create mode 100644 src/json_helper.c diff --git a/include/mastodont_account.h b/include/mastodont_account.h index 13e3629..57c6753 100644 --- a/include/mastodont_account.h +++ b/include/mastodont_account.h @@ -15,13 +15,43 @@ #ifndef MASTODONT_ACCOUNT #define MASTODONT_ACCOUNT +#include "mastodont_types.h" +#include struct mstdnt_account { - int i; + char* id; + char* username; + char* acct; + char* url; + + /* Display attributes */ + char* display_name; + char* note; + char* avatar; + char* avatar_static; + char* header; + char* header_static; + mstdnt_bool locked; + mstdnt_bool discoverable; + + /* Statistic attributes */ + char* created_at; + char* last_status_at; + unsigned statuses_count; + unsigned followers_count; + unsigned following_count; + + /* Optional attributes */ + struct mstdnt_account* moved; + /* struct mstdnt_field* field; */ + mstdnt_bool bot; + /* struct mstdnt_source* source */ + mstdnt_bool suspended; + char* mute_expires_at; }; -int mstdnt_load_account_from_json(struct mstdnt_status* status, cJSON* js); +int mstdnt_load_account_from_json(struct mstdnt_account* status, cJSON* js); #endif /* MASTODONT_ACCOUNT */ diff --git a/include/mastodont_json_helper.h b/include/mastodont_json_helper.h new file mode 100644 index 0000000..4b292fc --- /dev/null +++ b/include/mastodont_json_helper.h @@ -0,0 +1,40 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#ifndef MASTODONT_JSON_HELPER_H +#define MASTODONT_JSON_HELPER_H +#include "mastodont_types.h" + +#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0])) + +struct _mstdnt_str_val +{ + const char* key; + char** key_ptr; +}; + +struct _mstdnt_bool_val +{ + const char* key; + mstdnt_bool* bool_ptr; +}; + +int _mstdnt_key_val_iter(cJSON* v, + struct _mstdnt_str_val* str, + size_t str_len, + struct _mstdnt_bool_val* bools, + size_t bool_len); + +#endif /* MASTODONT_JSON_HELPER_H */ diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 3613e67..da7ef85 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -23,6 +23,7 @@ #include "mastodont_account.h" #include "mastodont_emoji.h" #include "mastodont_tag.h" +#include "mastodont_account.h" /* Status: Complete, not implemented */ diff --git a/src/account.c b/src/account.c index fe8a167..6a00211 100644 --- a/src/account.c +++ b/src/account.c @@ -14,12 +14,35 @@ */ #include +#include -int mstdnt_load_account_from_json(struct mstdnt_status* status, cJSON* js) +int mstdnt_load_account_from_json(struct mstdnt_account* acct, cJSON* js) { cJSON* v; - for (v = js; v; v->next) + struct _mstdnt_str_val strings[] = { + { "id", &(acct->id) }, + { "username", &(acct->username) }, + { "acct", &(acct->acct) }, + { "display_name", &(acct->display_name) }, + { "created_at", &(acct->created_at) }, + { "note", &(acct->note) }, + { "url", &(acct->url) }, + { "avatar", &(acct->avatar) }, + { "avatar_static", &(acct->avatar_static) }, + { "header", &(acct->header) }, + { "header_static", &(acct->header_static) }, + { "last_status_at", &(acct->last_status_at) }, + { "mute_expires_at", &(acct->mute_expires_at) } + }; + + struct _mstdnt_bool_val bools[] = { + { "locked", &(acct->locked) }, + { "bot", &(acct->bot) } + }; + + for (v = js; v; v = v->next) { - /* TODO */ + _mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings), + bools, _mstdnt_arr_len(bools)); } } diff --git a/src/json_helper.c b/src/json_helper.c new file mode 100644 index 0000000..9eec902 --- /dev/null +++ b/src/json_helper.c @@ -0,0 +1,48 @@ +/* + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) any + * later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more + * details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +#include + +int _mstdnt_key_val_iter(cJSON* v, + struct _mstdnt_str_val* str, + size_t str_len, + struct _mstdnt_bool_val* bools, + size_t bool_len) +{ + size_t i; + if (str && cJSON_IsString(v)) + { + for (i = 0; i < str_len; ++i) + { + if (strcmp(str[i].key, v->string) == 0) + { + *(str[i].key_ptr) = v->valuestring; + return 0; + } + } + } + else if (bools && cJSON_IsBool(v)) + { + for (i = 0; i < bool_len; ++i) + { + if (strcmp(bools[i].key, v->string) == 0) + { + *(bools[i].bool_ptr) = cJSON_IsTrue(v) ? cJSON_True : cJSON_False; + return 0; + } + } + } + return 1; +} diff --git a/src/status.c b/src/status.c index d2c201e..516f09e 100644 --- a/src/status.c +++ b/src/status.c @@ -14,43 +14,45 @@ */ #include +#include #include #include int mstdnt_load_status_from_json(struct mstdnt_status* status, cJSON* js) { cJSON* v; + struct _mstdnt_str_val strings[] = { + { "id", &(status->id) }, + { "uri", &(status->uri) }, + { "created_at", &(status->created_at) }, + { "content", &(status->content) }, + { "spoiler_text", &(status->spoiler_text) }, + { "in_reply_to_id", &(status->in_reply_to_id) }, + { "language", &(status->language) }, + { "url", &(status->url) }, + { "text", &(status->text) }, + { "in_reply_to_account_id", &(status->in_reply_to_account_id) } + }; + + struct _mstdnt_bool_val bools[] = { + { "sensitive", &(status->sensitive) }, + { "favourited", &(status->favourited) }, + { "reblogged", &(status->reblogged) }, + { "muted", &(status->muted) }, + { "bookmarked", &(status->bookmarked) }, + { "pinned", &(status->pinned) } + }; + for (v = js; v; v = v->next) { - if (cJSON_IsString(v)) + if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings), + bools, _mstdnt_arr_len(bools)) == 1) { - if(strcmp("id", v->string)==0) status->id = v->valuestring; - if(strcmp("uri", v->string)==0) status->uri = v->valuestring; - if(strcmp("created_at", v->string)==0) status->created_at = v->valuestring; - if(strcmp("content", v->string)==0) status->content = v->valuestring; - if(strcmp("spoiler_text", v->string)==0) status->spoiler_text = v->valuestring; - if(strcmp("in_reply_to_id", v->string)==0) status->in_reply_to_id = v->valuestring; - if(strcmp("in_reply_to_account_id", v->string)==0) status->in_reply_to_account_id = v->valuestring; - if(strcmp("in_reply_to_id", v->string)==0) status->in_reply_to_id = v->valuestring; - if(strcmp("language", v->string)==0) status->language = v->valuestring; - if(strcmp("text", v->string)==0) status->text = v->valuestring; - if(strcmp("in_reply_to_id", v->string)==0) status->in_reply_to_id = v->valuestring; - } - else if (cJSON_IsBool(v)) - { - /* It's probably an int 1/0, but for portability reasons I'll use the typedefs */ - const int val = cJSON_IsTrue(v) ? cJSON_True : cJSON_False; - if (strcmp("sensitive", v->string) == 0) status->sensitive = val; - if (strcmp("favourited", v->string) == 0) status->favourited = val; - if (strcmp("reblogged", v->string) == 0) status->reblogged = val; - if (strcmp("muted", v->string) == 0) status->sensitive = val; - if (strcmp("bookmarked", v->string) == 0) status->bookmarked = val; - if (strcmp("pinned", v->string) == 0) status->pinned = val; - } - else if (cJSON_IsObject(v)) - { - if (strcmp("account", v->string) == 0) - mstdnt_load_account_from_json(&(status->account), v->child); + if (cJSON_IsObject(v)) + { + if (strcmp("account", v->string) == 0) + mstdnt_load_account_from_json(&(status->account), v->child); + } } } }