Malloc functions, fix typos

FossilOrigin-Name: 5a8cbfd0ec89d005190969d0be7cfd2bc2c52fee7f2089620b4108a7143f0bdc
This commit is contained in:
me@ow.nekobit.net 2022-03-20 04:16:40 +00:00
parent 84e9f16c48
commit e77f93dc93
7 changed files with 53 additions and 9 deletions

View file

@ -19,6 +19,8 @@
#include <mastodont_timeline.h>
#include <mastodont_account.h>
#include <mastodont_list.h>
#include <mastodont_notification.h>
#include <mastodont_status.h>
/* Functions required form curl */
void mastodont_global_curl_init();

View file

@ -70,5 +70,6 @@ int mastodont_get_account(mastodont_t* data,
int mstdnt_account_from_json(struct mstdnt_account* status, cJSON* js);
void _mstdnt_val_account_call(cJSON* v, void* _type);
void _mstdnt_val_malloc_account_call(cJSON* v, void* _type);
#endif /* MASTODONT_ACCOUNT */

View file

@ -18,7 +18,7 @@
#include <stdint.h>
/* uint16 */
#define MSTDNT_NOTIFICATION_NOP 0
#define MSTDNT_NOTIFICATION_NOOP 0
#define MSTDNT_NOTIFICATION_FOLLOW (1<<0)
#define MSTDNT_NOTIFICATION_FOLLOW_REQUEST (1<<1)
#define MSTDNT_NOTIFICATION_MENTION (1<<2)

View file

@ -74,8 +74,8 @@ struct mstdnt_status
mstdnt_bool pinned;
};
void cleanup_statuses(struct mstdnt_status* statuses, size_t s);
void cleanup_status(struct mstdnt_status* status);
void mstdnt_cleanup_statuses(struct mstdnt_status* statuses, size_t s);
void mstdnt_cleanup_status(struct mstdnt_status* status);
int mstdnt_statuses_from_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
@ -89,6 +89,7 @@ int mstdnt_status_from_result(struct mstdnt_fetch_results* results,
int mstdnt_status_from_json(struct mstdnt_status* status, cJSON* js);
void _mstdnt_val_status_call(cJSON* v, void* _type);
void _mstdnt_val_malloc_status_call(cJSON* v, void* _type);
int mstdnt_status_context_from_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,

View file

@ -31,6 +31,16 @@ void _mstdnt_val_account_call(cJSON* v, void* _type)
mstdnt_account_from_json(type, v->child);
}
void _mstdnt_val_malloc_account_call(cJSON* v, void* _type)
{
struct mstdnt_account** type = _type;
*type = malloc(sizeof(struct mstdnt_account*));
if (*type)
mstdnt_account_from_json(*type, v->child);
}
int mstdnt_account_from_result(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
struct mstdnt_account* acct,

View file

@ -25,7 +25,6 @@ static void _mstdnt_val_notif_type_call(cJSON* v, void* _type)
{
mstdnt_notification_t* type = _type;
if (strcmp(v->string, "type") != 0)
{
*type = 0;
@ -49,11 +48,13 @@ int mstdnt_notification_from_json(struct mstdnt_notification* notif, cJSON* js)
{
cJSON* v;
/* Allocate optional params */
struct _mstdnt_val_ref vals[] = {
{ "account", &(notif->account), _mstdnt_val_account_call },
{ "account", &(notif->account), _mstdnt_val_malloc_account_call },
{ "created_at", &(notif->created_at), _mstdnt_val_string_call },
{ "id", &(notif->id), _mstdnt_val_string_call },
{ "status", &(notif->status), _mstdnt_val_status_call },
{ "status", &(notif->status), _mstdnt_val_malloc_status_call },
/* { "pleroma", &(notif->pleroma), _mstdnt_val_notif_pleroma_call }, */
{ "type", &(notif->type), _mstdnt_val_notif_type_call },
};
@ -141,3 +142,21 @@ int mastodont_get_notifications(mastodont_t* data,
return mastodont_request(data, &req_args);
}
void mstdnt_cleanup_notifications(struct mstdnt_notification* notifs, size_t notifs_len)
{
size_t i;
if (!notifs) return;
for (i = 0; i < notifs_len; ++i)
mstdnt_cleanup_notification(notifs + i);
free(notifs);
}
void mstdnt_cleanup_notification(struct mstdnt_notification* notif)
{
free(notif->account);
mstdnt_cleanup_status(notif->status);
free(notif->status);
}

View file

@ -29,6 +29,17 @@ void _mstdnt_val_status_call(cJSON* v, void* _type)
mstdnt_status_from_json(type, v->child);
}
void _mstdnt_val_malloc_status_call(cJSON* v, void* _type)
{
struct mstdnt_status** type = _type;
*type = malloc(sizeof(struct mstdnt_status*));
if (*type)
mstdnt_status_from_json(*type, v->child);
}
int mstdnt_status_from_json(struct mstdnt_status* status, cJSON* js)
{
cJSON* v;
@ -375,19 +386,19 @@ int mastodont_get_status_context(mastodont_t* data,
return mastodont_request(data, &req_args);
}
void cleanup_status(struct mstdnt_status* status)
void mstdnt_cleanup_status(struct mstdnt_status* status)
{
cleanup_attachments(status->media_attachments);
cleanup_status_pleroma(&(status->pleroma));
}
void cleanup_statuses(struct mstdnt_status* statuses, size_t s)
void mstdnt_cleanup_statuses(struct mstdnt_status* statuses, size_t s)
{
size_t i;
if (!statuses) return;
for (i = 0; i < s; ++i)
{
cleanup_status(statuses + i);
mstdnt_cleanup_status(statuses + i);
}
free(statuses);
}