Cleanup, fix curl function bug

FossilOrigin-Name: c4fd27e2874f7f67528b566f21abe2c324555f65d6859d02c6ecfc6ad93329ac
This commit is contained in:
me@ow.nekobit.net 2022-01-25 04:05:34 +00:00
parent 27a433bc27
commit 00285d5069
9 changed files with 32 additions and 33 deletions

View file

@ -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 */

View file

@ -26,7 +26,7 @@ struct mstdnt_emoji
mstdnt_bool visible_in_picker;
/* Optional */
char* category
char* category;
};

View file

@ -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;

View file

@ -15,6 +15,7 @@
#ifndef MASTODONT_TAG
#define MASTODONT_TAG
#include "mastodont_history.h"
struct mstdnt_tag
{

View file

@ -16,6 +16,7 @@
#ifndef MASTODONT_TIMELINE_H
#define MASTODONT_TIMELINE_H
#include <mastodont_types.h>
#include <mastodont_status.h>
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 */

View file

@ -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 */

View file

@ -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;
}

View file

@ -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);
}

View file

@ -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;