New val_ref function

FossilOrigin-Name: 32a9e862657c9011b052a5da1fb56fd245653752a4429a0fb45c86fc026493a6
This commit is contained in:
me@ow.nekobit.net 2022-02-24 16:10:51 +00:00
parent ffe0a504c8
commit 99df5eaf51
3 changed files with 69 additions and 36 deletions

View file

@ -18,22 +18,38 @@
#include "mastodont_types.h"
#include "mastodont_fetch.h"
struct _mstdnt_str_val
struct _mstdnt_val_ref
{
const char* key;
char** key_ptr;
};
struct _mstdnt_bool_val
{
const char* key;
mstdnt_bool* bool_ptr;
void* val;
void (*handle)(cJSON*, void*);
};
int _mstdnt_json_init(cJSON** root,
struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage);
int _mstdnt_key_val_ref(cJSON* v, struct _mstdnt_val_ref* refs,
size_t refs_len);
void _mstdnt_val_string_call(cJSON* v, void* _type);
void _mstdnt_val_bool_call(cJSON* v, void* _type);
/* DEPRECATED */
struct _mstdnt_str_val
{
const char* key;
char** key_ptr;
};
/* DEPRECATED */
struct _mstdnt_bool_val
{
const char* key;
mstdnt_bool* bool_ptr;
};
/* DEPRECATED */
int _mstdnt_key_val_iter(cJSON* v,
struct _mstdnt_str_val* str,
size_t str_len,

View file

@ -27,6 +27,33 @@ int _mstdnt_json_init(cJSON** root,
return 0;
}
int _mstdnt_key_val_ref(cJSON* v, struct _mstdnt_val_ref* refs,
size_t refs_len)
{
size_t i;
for (i = 0; i < refs_len; ++i)
{
if (strcmp(refs[i].key, v->string) == 0)
{
refs[i].handle(v, refs[i].val);
return 0;
}
}
return 1;
}
void _mstdnt_val_string_call(cJSON* v, void* _type)
{
char** type = _type;
*type = v->valuestring;
}
void _mstdnt_val_bool_call(cJSON* v, void* _type)
{
mstdnt_bool* type = _type;
*type = cJSON_IsTrue(v);
}
int _mstdnt_key_val_iter(cJSON* v,
struct _mstdnt_str_val* str,
size_t str_len,

View file

@ -23,40 +23,30 @@
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) }
struct _mstdnt_val_ref vals[] = {
{ "id", &(status->id), _mstdnt_val_string_call, NULL },
{ "uri", &(status->uri), _mstdnt_val_string_call, NULL },
{ "created_at", &(status->created_at), _mstdnt_val_string_call, NULL },
{ "content", &(status->content), _mstdnt_val_string_call, NULL },
{ "spoiler_text", &(status->spoiler_text), _mstdnt_val_string_call, NULL },
{ "in_reply_to_id", &(status->in_reply_to_id), _mstdnt_val_string_call, NULL },
{ "language", &(status->language), _mstdnt_val_string_call, NULL },
{ "url", &(status->url), _mstdnt_val_string_call, NULL },
{ "text", &(status->text), _mstdnt_val_string_call, NULL },
{ "in_reply_to_account_id", &(status->in_reply_to_account_id), _mstdnt_val_string_call, NULL },
{ "sensitive", &(status->sensitive), _mstdnt_val_bool_call, NULL },
{ "favourited", &(status->favourited), _mstdnt_val_bool_call, NULL },
{ "reblogged", &(status->reblogged), _mstdnt_val_bool_call, NULL },
{ "muted", &(status->muted), _mstdnt_val_bool_call, NULL },
{ "bookmarked", &(status->bookmarked), _mstdnt_val_bool_call, NULL },
{ "pinned", &(status->pinned), _mstdnt_val_bool_call, NULL },
};
for (v = js; v; v = v->next)
{
if (_mstdnt_key_val_iter(v, strings, _mstdnt_arr_len(strings),
bools, _mstdnt_arr_len(bools)) == 1)
{
if (_mstdnt_key_val_ref(v, vals, _mstdnt_arr_len(vals)) == 1)
if (cJSON_IsObject(v))
{
if (strcmp("account", v->string) == 0)
mstdnt_load_account_from_json(&(status->account), v->child);
}
}
}
return 0;
}