diff --git a/include/mastodont_account.h b/include/mastodont_account.h index 4e9842f..497b67a 100644 --- a/include/mastodont_account.h +++ b/include/mastodont_account.h @@ -24,12 +24,19 @@ #define MSTDNT_LOOKUP_ACCT 0 #define MSTDNT_LOOKUP_ID 1 +// DEPRECATED struct _mstdnt_accounts_args { struct mstdnt_account** acct; size_t* size; }; +struct mstdnt_accounts +{ + struct mstdnt_account* accts; + size_t len; +}; + struct mstdnt_account { char* id; @@ -158,14 +165,23 @@ mstdnt_get_following(mastodont_t* data, char* id, struct mstdnt_account_args args); -int mstdnt_account_json(struct mstdnt_account* acct, cJSON* js); -int mstdnt_account_json_callback(cJSON* json, void* _args); -int mstdnt_accounts_json_callback(cJSON* json, void* _args); +int +mstdnt_account_json(struct mstdnt_account* acct, + cJSON* js); + +int +mstdnt_account_json_callback(cJSON* json, + void* args, + mstdnt_request_cb_data* data); +int +mstdnt_accounts_json_callback(cJSON* json, + void* args, + mstdnt_request_cb_data* data); void _mstdnt_val_account_call(cJSON* v, void* _type); void _mstdnt_val_malloc_account_call(cJSON* v, void* _type); // Cleanup -void mstdnt_cleanup_account(struct mstdnt_account* acct, size_t unused); -void mstdnt_cleanup_accounts(struct mstdnt_account* accts, size_t len); +void mstdnt_cleanup_account(struct mstdnt_account* acct); +void mstdnt_cleanup_accounts(struct mstdnt_accounts* accts); #endif /* MASTODONT_ACCOUNT */ diff --git a/include/mastodont_generate.h b/include/mastodont_generate.h index a4d86af..2f5e3c3 100644 --- a/include/mastodont_generate.h +++ b/include/mastodont_generate.h @@ -31,7 +31,7 @@ if (!arr_len) \ return 0; \ \ - *array = calloc(1, arr_len * sizeof(type)); \ + *array = calloc(arr_len, sizeof(type)); \ \ if (*array == NULL) \ return 1; \ diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 61256b8..4f21985 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -107,13 +107,6 @@ struct mstdnt_status_args int media_ids_len; }; -// Cleanup -void -mstdnt_cleanup_statuses(struct mstdnt_status* statuses, size_t s); - -void -mstdnt_cleanup_status(struct mstdnt_status* status, size_t unused); - int mstdnt_status_json(struct mstdnt_status* status, cJSON* js); @@ -293,4 +286,11 @@ int mstdnt_statuses_json_callback(cJSON* json, void* args, mstdnt_request_cb_dat int mstdnt_status_json_callback(cJSON* json, void* args, mstdnt_request_cb_data* results); int mstdnt_status_context_json_callback(cJSON* json, void* args, mstdnt_request_cb_data* results); +// Cleanup +void +mstdnt_cleanup_statuses(struct mstdnt_statuses* statuses); + +void +mstdnt_cleanup_status(struct mstdnt_status* status); + #endif /* MASTODONT_STATUS */ diff --git a/src/account.c b/src/account.c index b30e86c..0b8943e 100644 --- a/src/account.c +++ b/src/account.c @@ -50,16 +50,23 @@ mstdnt_account_json_callback(cJSON* json, mstdnt_request_cb_data* data) { (void)args; - struct mstdnt_account* acct = malloc(sizeof(struct mstdnt_status)); + struct mstdnt_account* acct = malloc(sizeof(struct mstdnt_account)); + data->data = acct; + data->data_free_cb = (data_free_cb_t)mstdnt_cleanup_account; // Not sure why it expects it to be in the child - return mstdnt_account_json(_args, json->child); + return mstdnt_account_json(acct, json->child); } int -mstdnt_accounts_json_callback(cJSON* json, void* _args) +mstdnt_accounts_json_callback(cJSON* json, + void* args, + mstdnt_request_cb_data* data) { - struct _mstdnt_accounts_args* args = _args; - return mstdnt_accounts_json(args->acct, args->size, json); + (void)args; + struct mstdnt_accounts* accts = malloc(sizeof(struct mstdnt_accounts)); + data->data = accts; + data->data_free_cb = (data_free_cb_t)mstdnt_cleanup_accounts; + return mstdnt_accounts_json(&(accts->accts), &(accts->len), json); } static int @@ -238,12 +245,13 @@ mstdnt_account_json(struct mstdnt_account* acct, cJSON* js) } -int mstdnt_account_action(mastodont_t* data, - struct mstdnt_args* m_args, - mstdnt_request_cb_t cb_request, - void* cb_args, - char* id, - char* url_str) +int +mstdnt_account_action(mastodont_t* data, + struct mstdnt_args* m_args, + mstdnt_request_cb_t cb_request, + void* cb_args, + char* id, + char* url_str) { char url[MSTDNT_URLSIZE]; snprintf(url, MSTDNT_URLSIZE, url_str, id); @@ -288,11 +296,12 @@ void mstdnt_cleanup_account(struct mstdnt_account* acct) mstdnt_cleanup_emojis(acct->emojis); } -void mstdnt_cleanup_accounts(struct mstdnt_account* accts, size_t len) +void mstdnt_cleanup_accounts(struct mstdnt_accounts* accts) { if (!accts) return; - for (int i = 0; i < len; ++i) - mstdnt_cleanup_account(accts + i); - mstdnt_free(accts); + for (size_t i = 0; i < accts->len; ++i) + mstdnt_cleanup_account(accts->accts + i); + + mstdnt_free(accts->accts); } diff --git a/src/status.c b/src/status.c index 98da597..5ce3216 100644 --- a/src/status.c +++ b/src/status.c @@ -550,8 +550,7 @@ mstdnt_status_emoji_react(mastodont_t* api, } void -mstdnt_cleanup_status(struct mstdnt_status* status, - size_t unused) +mstdnt_cleanup_status(struct mstdnt_status* status) { mstdnt_cleanup_attachments(status->media_attachments); mstdnt_cleanup_account(&(status->account)); @@ -559,21 +558,18 @@ mstdnt_cleanup_status(struct mstdnt_status* status, mstdnt_cleanup_emojis(status->emojis); if (status->reblog) { - mstdnt_cleanup_status(status->reblog, 0); + mstdnt_cleanup_status(status->reblog); mstdnt_free(status->reblog); } mstdnt_free(status->application); } void -mstdnt_cleanup_statuses(struct mstdnt_status* statuses, - size_t s) +mstdnt_cleanup_statuses(struct mstdnt_statuses* statuses) { - size_t i; if (!statuses) return; - for (i = 0; i < s; ++i) - { - mstdnt_cleanup_status(statuses + i, 0); - } - mstdnt_free(statuses); + for (size_t i = 0; i < statuses->len; ++i) + mstdnt_cleanup_status(statuses->statuses + i); + + mstdnt_free(statuses->statuses); }