From 00285d5069f5a2b20cf6a6e42229537c6c2ff593 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Tue, 25 Jan 2022 04:05:34 +0000 Subject: [PATCH] Cleanup, fix curl function bug FossilOrigin-Name: c4fd27e2874f7f67528b566f21abe2c324555f65d6859d02c6ecfc6ad93329ac --- include/mastodont.h | 2 -- .../{mastodont_emojis.h => mastodont_emoji.h} | 2 +- include/mastodont_status.h | 11 ++++++---- include/{mastodont_tags.h => mastodont_tag.h} | 1 + include/mastodont_timeline.h | 3 ++- include/mastodont_types.h | 9 ++------- src/fetch.c | 12 +++++++---- src/mastodont.c | 5 ----- src/timeline.c | 20 ++++++++++--------- 9 files changed, 32 insertions(+), 33 deletions(-) rename include/{mastodont_emojis.h => mastodont_emoji.h} (97%) rename include/{mastodont_tags.h => mastodont_tag.h} (96%) diff --git a/include/mastodont.h b/include/mastodont.h index aa05e7d..fddcebd 100644 --- a/include/mastodont.h +++ b/include/mastodont.h @@ -25,6 +25,4 @@ void mastodont_global_curl_cleanup(); int mastodont_init(mastodont_t* data); void mastodont_free(mastodont_t* data); -void mastodont_response_cleanup(struct mstdnt_response* response); - #endif /* MASTODONT_H */ diff --git a/include/mastodont_emojis.h b/include/mastodont_emoji.h similarity index 97% rename from include/mastodont_emojis.h rename to include/mastodont_emoji.h index 88690e0..2f5cffc 100644 --- a/include/mastodont_emojis.h +++ b/include/mastodont_emoji.h @@ -26,7 +26,7 @@ struct mstdnt_emoji mstdnt_bool visible_in_picker; /* Optional */ - char* category + char* category; }; diff --git a/include/mastodont_status.h b/include/mastodont_status.h index 41f1356..8cbff66 100644 --- a/include/mastodont_status.h +++ b/include/mastodont_status.h @@ -19,6 +19,9 @@ #include "mastodont_attachment.h" #include "mastodont_application.h" #include "mastodont_mention.h" +#include "mastodont_account.h" +#include "mastodont_emoji.h" +#include "mastodont_tag.h" /* Status: Complete, not implemented */ @@ -42,13 +45,13 @@ struct mstdnt_status enum mstdnt_status_visibility visibility; mstdnt_bool sensitive; char* spoiler_text; - mstdnt_attachment media_attachments[]; + struct mstdnt_attachment* media_attachments; struct mstdnt_application application; /* Rendering attributes */ - struct mstdnt_mention mentions[]; - struct mstdnt_tag tags[]; - struct mstdnt_emoji emojis[]; + struct mstdnt_mention* mentions; + struct mstdnt_tag* tags; + struct mstdnt_emoji* emojis; /* Information attributes */ unsigned reblogs_count; diff --git a/include/mastodont_tags.h b/include/mastodont_tag.h similarity index 96% rename from include/mastodont_tags.h rename to include/mastodont_tag.h index caca4fb..5fc43a0 100644 --- a/include/mastodont_tags.h +++ b/include/mastodont_tag.h @@ -15,6 +15,7 @@ #ifndef MASTODONT_TAG #define MASTODONT_TAG +#include "mastodont_history.h" struct mstdnt_tag { diff --git a/include/mastodont_timeline.h b/include/mastodont_timeline.h index 8e3d94f..62733a6 100644 --- a/include/mastodont_timeline.h +++ b/include/mastodont_timeline.h @@ -16,6 +16,7 @@ #ifndef MASTODONT_TIMELINE_H #define MASTODONT_TIMELINE_H #include +#include struct mstdnt_timeline_public_args { int local; @@ -29,6 +30,6 @@ struct mstdnt_timeline_public_args { int mastodont_timeline_public(mastodont_t* data, struct mstdnt_timeline_public_args* args, - struct mstdnt_response* response); + struct mstdnt_status* response[]); #endif /* MASTODONT_TIMELINE_H */ diff --git a/include/mastodont_types.h b/include/mastodont_types.h index 2316e75..353e0a9 100644 --- a/include/mastodont_types.h +++ b/include/mastodont_types.h @@ -20,15 +20,10 @@ #define MSTDNT_URLSIZE 2048 typedef unsigned char mstdnt_bool; -typedef struct mastodont { +typedef struct mastodont +{ char* url; CURL* curl; } mastodont_t; -/* FIXME */ -struct mstdnt_response { - char* data; - size_t size; -}; - #endif /* MASTODONT_TYPES_H */ diff --git a/src/fetch.c b/src/fetch.c index 4c95eea..961c72f 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -21,7 +21,7 @@ static size_t write_callback(char* ptr, size_t _size, size_t nmemb, void* _content) { size_t size = nmemb * _size; /* Mostly pointless, but portable */ - struct mastodont_fetch_results* res = _content; /* Cast */ + struct mstdnt_fetch_results* res = _content; /* Cast */ char* data; if ((data = realloc(res->response, res->size + size + 1)) == NULL) @@ -31,21 +31,21 @@ static size_t write_callback(char* ptr, size_t _size, size_t nmemb, void* _conte } res->response = data; - memcpy(&(res->response[res->size]), data, size); + memcpy(&(res->response[res->size]), ptr, size); res->size += size; res->response[res->size] = 0; return size; } -void mastodont_fetch_results_cleanup(struct mastodont_fetch_results* res) +void mastodont_fetch_results_cleanup(struct mstdnt_fetch_results* res) { free(res->response); } int mastodont_fetch_curl(mastodont_t* mstdnt, char* _url, - struct mastodont_fetch_results* results) + struct mstdnt_fetch_results* results) { int res; @@ -60,6 +60,10 @@ int mastodont_fetch_curl(mastodont_t* mstdnt, curl_easy_setopt(mstdnt->curl, CURLOPT_WRITEDATA, results); res = curl_easy_perform(mstdnt->curl); + if (res != CURLE_OK) + { + printf("curl_easy_perform: %s\n", curl_easy_strerror(res)); + } return res; } diff --git a/src/mastodont.c b/src/mastodont.c index 440b600..c58fb42 100644 --- a/src/mastodont.c +++ b/src/mastodont.c @@ -22,8 +22,3 @@ void mastodont_free(mastodont_t* data) { curl_easy_cleanup(data->curl); } - -void mastodont_response_cleanup(struct mstdnt_response* response) -{ - free(response->data); -} diff --git a/src/timeline.c b/src/timeline.c index e159ef6..92952c9 100644 --- a/src/timeline.c +++ b/src/timeline.c @@ -20,9 +20,10 @@ int mastodont_timeline_public(mastodont_t* data, struct mstdnt_timeline_public_args* args, - struct mstdnt_response* response) + struct mstdnt_status* statuses[]) { int res; + cJSON* p; struct mstdnt_fetch_results results = { 0 }; /* Default args */ struct mstdnt_timeline_public_args _args; @@ -38,18 +39,19 @@ int mastodont_timeline_public(mastodont_t* data, args = &_args; } - res = mastodont_fetch_curl(data, "/api/v1/timelines/public", &results); + res = mastodont_fetch_curl(data, "api/v1/timelines/public", &results); - cJSON* parse = cJSON_Parse(results.response); - if (!parse) + cJSON* parse = cJSON_ParseWithLength(results.response, results.size); + if (parse == NULL) { - + const char* jerror = cJSON_GetErrorPtr(); + if (jerror) + fprintf(stderr, "cJSON_Parse: %s\n", jerror); + goto cleanup; } - - /* Cleanup */ -free: - cJSON_Delete(parse); +cleanup: + if (parse) cJSON_Delete(parse); mastodont_fetch_results_cleanup(&results); return res;