Generative loops for json items

FossilOrigin-Name: f2d1b20fceea52bad2b0f3a46950244e45383d6f056c64bf273a75b9552c622f
This commit is contained in:
me@ow.nekobit.net 2022-05-04 19:40:35 +00:00
parent 2fcedc1632
commit c717b142b8
6 changed files with 82 additions and 69 deletions

View file

@ -0,0 +1,46 @@
/*
* Treebird - Lightweight frontend for Pleroma
* Copyright (C) 2022 Nekobit
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef MASTODONT_GENERATE_H
#define MASTODONT_GENERATE_H
#include <stdlib.h>
#include <cjson/cJSON.h>
#define GENERATE_JSON_ARRAY_FUNC(symbol, type, call_symbol) int symbol(type* array[], size_t* array_size, cJSON* js) { \
if (!(array && cJSON_IsArray(js))) return 1; \
size_t i = 0; \
cJSON *arr_j_list; \
size_t arr_len = cJSON_GetArraySize(js); \
if (array_size) *array_size = arr_len; \
\
if (!arr_len) \
return 0; \
\
*array = calloc(1, arr_len * sizeof(type)); \
\
if (*array == NULL) \
return 1; \
cJSON_ArrayForEach(arr_j_list, js) \
{ \
call_symbol((*array) + i++, arr_j_list->child); \
} \
return 0; \
}
#endif // MASTODONT_GENERATE_H

View file

@ -91,6 +91,7 @@ struct mstdnt_account_statuses_args
};
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,
@ -104,6 +105,11 @@ int mstdnt_status_from_result(struct mstdnt_fetch_results* results,
int mstdnt_status_from_json(struct mstdnt_status* status, cJSON* js);
// Generated function
int mstdnt_statuses_json(struct mstdnt_status* statuses[],
size_t* size,
cJSON* js);
void _mstdnt_val_status_call(cJSON* v, void* _type);
void _mstdnt_val_malloc_status_call(cJSON* v, void* _type);
@ -160,8 +166,8 @@ int mastodont_create_status(mastodont_t* data,
/* Generates do and undo functions */
#define MSTDNT_STATUS_ACTION_DECL(type) int mastodont_##type##_status(mastodont_t* data, char* id, struct mstdnt_storage* storage, struct mstdnt_status* status)
#define MSTDNT_STATUS_ACTION_FUNC_URL(action) { \
return mstdnt_status_action(data, id, storage, status, "api/v1/statuses/%s/" action);\
#define MSTDNT_STATUS_ACTION_FUNC_URL(action) { \
return mstdnt_status_action(data, id, storage, status, "api/v1/statuses/%s/" action); \
}
MSTDNT_STATUS_ACTION_DECL(favourite);

1
src/.#search.c Symbolic link
View file

@ -0,0 +1 @@
nekobit@Hylands-MacBook-Pro.local.4809

View file

@ -67,23 +67,6 @@ int mstdnt_accounts_result(struct mstdnt_fetch_results* results,
if (_mstdnt_json_init(&root, results, storage) &&
!cJSON_IsArray(root))
return 1;
if (size) *size = cJSON_GetArraySize(root);
/* accounts can be an empty array! */
if (!(size ? *size : cJSON_GetArraySize(root)))
return 0; /* Not an error, but we are done parsing */
/* malloc array - cJSON does a loop to count, let's do it once preferably */
*accts = calloc(1, (size ? *size : cJSON_GetArraySize(root))
* sizeof(struct mstdnt_account));
if (*accts == NULL)
return 1;
cJSON_ArrayForEach(acct_j_list, root)
{
mstdnt_account_from_json((*accts) + i++, acct_j_list->child);
}
return 0;
}

View file

@ -36,10 +36,7 @@ int mstdnt_search_from_result(struct mstdnt_storage* storage,
struct mstdnt_fetch_results* results,
struct mstdnt_search_results* search_results)
{
size_t i = 0;
size_t arr_len = 0;
size_t size = 0;
cJSON* root, *item;
cJSON* root;
if (_mstdnt_json_init(&root, results, storage))
return 1;
@ -49,15 +46,9 @@ int mstdnt_search_from_result(struct mstdnt_storage* storage,
cJSON* hashtags = cJSON_GetObjectItemCaseSensitive(root, "hashtags");
// Statuses
if (statuses && cJSON_IsArray(statuses) && (size = cJSON_GetArraySize(statuses)))
{
search_results->statuses = calloc(1, size * sizeof(struct mstdnt_status));
search_results->statuses_len = size;
cJSON_ArrayForEach(item, statuses)
{
mstdnt_status_from_json(search_results->statuses + arr_len++, item->child);
}
}
mstdnt_statuses_json(&(search_results->statuses),
&(search_results->statuses_len),
statuses);
return 0;
}

View file

@ -21,6 +21,7 @@
#include <mastodont_query.h>
#include <mastodont_pleroma.h>
#include <mastodont_request.h>
#include <mastodont_generate.h>
void _mstdnt_val_status_call(cJSON* v, void* _type)
{
@ -122,32 +123,17 @@ int mstdnt_statuses_from_result(struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size)
{
size_t i = 0;
cJSON* root, *status_j_list;
if (_mstdnt_json_init(&root, results, storage) &&
!cJSON_IsArray(root))
return 1;
if (size) *size = cJSON_GetArraySize(root);
/* Statuses can be an empty array! */
if (!(size ? *size : cJSON_GetArraySize(root)))
return 0; /* Not an error, but we are done parsing */
/* malloc array - cJSON does a loop to count, let's do it once preferably */
*statuses = calloc(1, (size ? *size : cJSON_GetArraySize(root))
* sizeof(struct mstdnt_status));
if (*statuses == NULL)
return 1;
cJSON_ArrayForEach(status_j_list, root)
{
mstdnt_status_from_json((*statuses) + i++, status_j_list->child);
}
return 0;
return mstdnt_statuses_json(statuses, size, root);
}
// GENERATE mstdnt_statuses_from_json
GENERATE_JSON_ARRAY_FUNC(mstdnt_statuses_json, struct mstdnt_status, mstdnt_status_from_json)
int _mstdnt_statuses_result_callback(struct mstdnt_fetch_results* results,
struct mstdnt_storage* storage,
void* _args)
@ -250,27 +236,27 @@ static int mstdnt_status_action(mastodont_t* data,
/* These are all the same */
MSTDNT_STATUS_ACTION_DECL(favourite)
MSTDNT_STATUS_ACTION_FUNC_URL("favourite")
MSTDNT_STATUS_ACTION_FUNC_URL("favourite")
MSTDNT_STATUS_ACTION_DECL(unfavourite)
MSTDNT_STATUS_ACTION_FUNC_URL("unfavourite")
MSTDNT_STATUS_ACTION_DECL(unfavourite)
MSTDNT_STATUS_ACTION_FUNC_URL("unfavourite")
MSTDNT_STATUS_ACTION_DECL(reblog)
MSTDNT_STATUS_ACTION_FUNC_URL("reblog")
MSTDNT_STATUS_ACTION_DECL(reblog)
MSTDNT_STATUS_ACTION_FUNC_URL("reblog")
MSTDNT_STATUS_ACTION_DECL(unreblog)
MSTDNT_STATUS_ACTION_FUNC_URL("unreblog")
MSTDNT_STATUS_ACTION_DECL(unreblog)
MSTDNT_STATUS_ACTION_FUNC_URL("unreblog")
MSTDNT_STATUS_ACTION_DECL(pin)
MSTDNT_STATUS_ACTION_FUNC_URL("pin")
MSTDNT_STATUS_ACTION_DECL(pin)
MSTDNT_STATUS_ACTION_FUNC_URL("pin")
MSTDNT_STATUS_ACTION_DECL(unpin)
MSTDNT_STATUS_ACTION_FUNC_URL("unpin")
MSTDNT_STATUS_ACTION_DECL(unpin)
MSTDNT_STATUS_ACTION_FUNC_URL("unpin")
MSTDNT_STATUS_ACTION_DECL(bookmark)
MSTDNT_STATUS_ACTION_FUNC_URL("bookmark")
MSTDNT_STATUS_ACTION_DECL(bookmark)
MSTDNT_STATUS_ACTION_FUNC_URL("bookmark")
MSTDNT_STATUS_ACTION_DECL(unbookmark)
MSTDNT_STATUS_ACTION_DECL(unbookmark)
MSTDNT_STATUS_ACTION_FUNC_URL("unbookmark")
/* TODO Mutes can be timed */
@ -516,10 +502,10 @@ int mastodont_get_bookmarks(mastodont_t* data,
}
int mastodont_get_favourites(mastodont_t* data,
struct mstdnt_favourites_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size)
struct mstdnt_favourites_args* args,
struct mstdnt_storage* storage,
struct mstdnt_status* statuses[],
size_t* size)
{
struct _mstdnt_statuses_cb_args cb_args = { statuses, size };