diff --git a/src/account.c b/src/account.c index 631e21b..578e12e 100644 --- a/src/account.c +++ b/src/account.c @@ -489,19 +489,7 @@ void content_account_favourites(PATH_ARGS) content_timeline(req, ssn, api, &storage, statuses, statuses_len, BASE_CAT_BOOKMARKS, "Favorites", 0, 1); } -AV* perlify_accounts(const struct mstdnt_account* accounts, size_t len) -{ - if (!(accounts && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_account(accounts + i))); - } - - return av; -} +PERLIFY_MULTI(account, accounts, mstdnt_account) HV* perlify_account(const struct mstdnt_account* acct) { diff --git a/src/attachments.c b/src/attachments.c index a623e43..233e937 100644 --- a/src/attachments.c +++ b/src/attachments.c @@ -116,7 +116,7 @@ void cleanup_media_ids(struct session* ssn, char** media_ids) free(media_ids); } -HV* perlify_attachment(struct mstdnt_attachment* const attachment) +HV* perlify_attachment(const struct mstdnt_attachment* const attachment) { if (!attachment) return NULL; HV* attach_hv = newHV(); @@ -131,19 +131,7 @@ HV* perlify_attachment(struct mstdnt_attachment* const attachment) return attach_hv; } -AV* perlify_attachments(struct mstdnt_attachment* const attachments, size_t len) -{ - if (!(attachments && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_attachment(attachments + i))); - } - - return av; -} +PERLIFY_MULTI(attachment, attachments, mstdnt_attachment) void api_attachment_create(PATH_ARGS) { diff --git a/src/attachments.h b/src/attachments.h index e576a5c..131b3da 100644 --- a/src/attachments.h +++ b/src/attachments.h @@ -37,7 +37,7 @@ void cleanup_media_ids(struct session* ssn, char** media_ids); void api_attachment_create(PATH_ARGS); // Perl -HV* perlify_attachment(struct mstdnt_attachment* const attachment); -AV* perlify_attachments(struct mstdnt_attachment* const attachments, size_t len); +HV* perlify_attachment(const struct mstdnt_attachment* const attachment); +AV* perlify_attachments(const struct mstdnt_attachment* const attachments, size_t len); #endif // ATTACHMENTS_H diff --git a/src/conversations.c b/src/conversations.c index f585c66..24f6ed8 100644 --- a/src/conversations.c +++ b/src/conversations.c @@ -70,7 +70,7 @@ void content_chats(PATH_ARGS) // Cleanup mastodont_storage_cleanup(&storage); - mstdnt_cleanup_chats(chats); + mstdnt_cleanup_chats(chats, chats_len); Safefree(dup); } @@ -125,6 +125,7 @@ void content_chat_view(PATH_ARGS) mastodont_storage_cleanup(&storage); mastodont_storage_cleanup(&storage_chat); + mstdnt_cleanup_chat(&chat); mstdnt_cleanup_messages(messages); Safefree(dup); } @@ -141,19 +142,7 @@ HV* perlify_chat(const struct mstdnt_chat* chat) return chat_hv; } -AV* perlify_chats(const struct mstdnt_chat* chats, size_t len) -{ - if (!(chats && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_chat(chats + i))); - } - - return av; -} +PERLIFY_MULTI(chat, chats, mstdnt_chat) HV* perlify_message(const struct mstdnt_message* message) { @@ -171,17 +160,4 @@ HV* perlify_message(const struct mstdnt_message* message) return message_hv; } -AV* perlify_messages(const struct mstdnt_message* messages, size_t len) -{ - if (!(messages && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_message(messages + i))); - } - - return av; -} - +PERLIFY_MULTI(message, messages, mstdnt_message) diff --git a/src/emoji.c b/src/emoji.c index 3a7025a..2cfe1b8 100644 --- a/src/emoji.c +++ b/src/emoji.c @@ -123,7 +123,7 @@ char* construct_emoji_picker(char* status_id, size_t* size) return dup; } -HV* perlify_emoji(struct mstdnt_emoji* const emoji) +HV* perlify_emoji(const struct mstdnt_emoji* const emoji) { if (!emoji) return NULL; HV* emoji_hv = newHV(); @@ -135,16 +135,5 @@ HV* perlify_emoji(struct mstdnt_emoji* const emoji) return emoji_hv; } -AV* perlify_emojis(struct mstdnt_emoji* const emos, size_t len) -{ - if (!(emos && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); +PERLIFY_MULTI(emoji, emojis, mstdnt_emoji) - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_emoji(emos + i))); - } - - return av; -} diff --git a/src/emoji.h b/src/emoji.h index 44017c5..9d9e8cf 100644 --- a/src/emoji.h +++ b/src/emoji.h @@ -38,7 +38,7 @@ void content_emoji_picker(PATH_ARGS); char* construct_emoji_picker(char* status_id, size_t* size); // Perl -HV* perlify_emoji(struct mstdnt_emoji* const emoji); -AV* perlify_emojis(struct mstdnt_emoji* const emos, size_t len); +HV* perlify_emoji(const struct mstdnt_emoji* const emoji); +AV* perlify_emojis(const struct mstdnt_emoji* const emos, size_t len); #endif // EMOJI_H diff --git a/src/emoji_reaction.c b/src/emoji_reaction.c index d73aecd..6479759 100644 --- a/src/emoji_reaction.c +++ b/src/emoji_reaction.c @@ -22,7 +22,7 @@ #include #include "easprintf.h" -HV* perlify_emoji_reaction(struct mstdnt_emoji_reaction* const emoji) +HV* perlify_emoji_reaction(const struct mstdnt_emoji_reaction* const emoji) { if (!emoji) return NULL; HV* emoji_hv = newHV(); @@ -34,16 +34,5 @@ HV* perlify_emoji_reaction(struct mstdnt_emoji_reaction* const emoji) return emoji_hv; } -AV* perlify_emoji_reactions(struct mstdnt_emoji_reaction* const emos, size_t len) -{ - if (!(emos && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); +PERLIFY_MULTI(emoji_reaction, emoji_reactions, mstdnt_emoji_reaction) - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_emoji_reaction(emos + i))); - } - - return av; -} diff --git a/src/emoji_reaction.h b/src/emoji_reaction.h index bacaa14..22dea36 100644 --- a/src/emoji_reaction.h +++ b/src/emoji_reaction.h @@ -22,7 +22,7 @@ #include "global_perl.h" // Perl -HV* perlify_emoji_reaction(struct mstdnt_emoji_reaction* const emoji); -AV* perlify_emoji_reactions(struct mstdnt_emoji_reaction* const emos, size_t len); +HV* perlify_emoji_reaction(const struct mstdnt_emoji_reaction* const emoji); +AV* perlify_emoji_reactions(const struct mstdnt_emoji_reaction* const emos, size_t len); #endif // EMOJI_REACTION_H diff --git a/src/global_perl.h b/src/global_perl.h index 74e3bb9..38c31ab 100644 --- a/src/global_perl.h +++ b/src/global_perl.h @@ -46,6 +46,17 @@ LEAVE; \ perl_unlock() +#define PERLIFY_MULTI(type, types, mstype) AV* perlify_##types(const struct mstype* const types, size_t len) { \ + if (!(types && len)) return NULL; \ + AV* av = newAV(); \ + av_extend(av, len-1); \ + for (size_t i = 0; i < len; ++i) \ + av_store(av, i, newRV_noinc((SV*)perlify_##type(types + i))); \ + return av; \ + } + + + extern PerlInterpreter* my_perl; extern HV* template_files; extern pthread_mutex_t perllock_mutex; diff --git a/src/lists.c b/src/lists.c index 93108cf..a49a92b 100644 --- a/src/lists.c +++ b/src/lists.c @@ -115,16 +115,5 @@ HV* perlify_list(const struct mstdnt_list* list) return list_hv; } -AV* perlify_lists(const struct mstdnt_list* lists, size_t len) -{ - if (!(lists && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); +PERLIFY_MULTI(list, lists, mstdnt_list) - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_list(lists + i))); - } - - return av; -} diff --git a/src/notifications.c b/src/notifications.c index 9eeecc7..1f15b36 100644 --- a/src/notifications.c +++ b/src/notifications.c @@ -181,7 +181,7 @@ static HV* perlify_notification_pleroma(struct mstdnt_notification_pleroma* noti // Converts it into a perl struct -HV* perlify_notification(struct mstdnt_notification* notif) +HV* perlify_notification(const struct mstdnt_notification* const notif) { if (!notif) return NULL; HV* notif_hv = newHV(); @@ -197,20 +197,7 @@ HV* perlify_notification(struct mstdnt_notification* notif) return notif_hv; } -// The same as above, but for multiple -AV* perlify_notifications(struct mstdnt_notification* notifs, size_t len) -{ - if (!(notifs && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_notification(notifs + i))); - } - - return av; -} +PERLIFY_MULTI(notification, notifications, mstdnt_notification) void api_notifications(PATH_ARGS) { diff --git a/src/notifications.h b/src/notifications.h index 1fa623f..50b4339 100644 --- a/src/notifications.h +++ b/src/notifications.h @@ -32,7 +32,7 @@ void content_notifications_read(PATH_ARGS); void api_notifications(PATH_ARGS); -HV* perlify_notification(struct mstdnt_notification* notif); -AV* perlify_notifications(struct mstdnt_notification* notif, size_t len); +HV* perlify_notification(const struct mstdnt_notification* const notif); +AV* perlify_notifications(const struct mstdnt_notification* const notif, size_t len); #endif // NOTIFICATION_H diff --git a/src/scrobble.c b/src/scrobble.c index c6a2040..521c147 100644 --- a/src/scrobble.c +++ b/src/scrobble.c @@ -22,7 +22,7 @@ #include "account.h" // Converts it into a perl struct -HV* perlify_scrobble(struct mstdnt_scrobble* scrobble) +HV* perlify_scrobble(const struct mstdnt_scrobble* const scrobble) { if (!scrobble) return NULL; HV* scrobble_hv = newHV(); @@ -38,17 +38,4 @@ HV* perlify_scrobble(struct mstdnt_scrobble* scrobble) return scrobble_hv; } -// The same as above, but for multiple -AV* perlify_scrobbles(struct mstdnt_scrobble* scrobble, size_t len) -{ - if (!(scrobble && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_scrobble(scrobble + i))); - } - - return av; -} +PERLIFY_MULTI(scrobble, scrobbles, mstdnt_scrobble) diff --git a/src/scrobble.h b/src/scrobble.h index ef20e9b..e2174e5 100644 --- a/src/scrobble.h +++ b/src/scrobble.h @@ -21,7 +21,7 @@ #include #include "global_perl.h" -HV* perlify_scrobble(struct mstdnt_scrobble* scrobble); -AV* perlify_scrobbles(struct mstdnt_scrobble* scrobble, size_t len); +HV* perlify_scrobble(const struct mstdnt_scrobble* const scrobble); +AV* perlify_scrobbles(const struct mstdnt_scrobble* const scrobble, size_t len); #endif /* SCROBBLE_H */ diff --git a/src/status.c b/src/status.c index ac125f6..f9ff323 100644 --- a/src/status.c +++ b/src/status.c @@ -470,17 +470,4 @@ HV* perlify_status(const struct mstdnt_status* status) return status_hv; } - -AV* perlify_statuses(const struct mstdnt_status* statuses, size_t len) -{ - if (!(statuses && len)) return NULL; - AV* av = newAV(); - av_extend(av, len-1); - - for (int i = 0; i < len; ++i) - { - av_store(av, i, newRV_inc((SV*)perlify_status(statuses + i))); - } - - return av; -} +PERLIFY_MULTI(status, statuses, mstdnt_status)