Use booleans

FossilOrigin-Name: f292b0e3c04535be65eca37d75b18721bf040a2e5c7098c1d04bd45b988029e4
This commit is contained in:
nekobit 2022-06-26 03:15:36 +00:00
parent a4cf7e190e
commit 7c3936078f
4 changed files with 41 additions and 22 deletions

View file

@ -22,6 +22,7 @@ enum _mstdnt_query_type
{
_MSTDNT_QUERY_STRING,
_MSTDNT_QUERY_INT,
_MSTDNT_QUERY_BOOL,
_MSTDNT_QUERY_ARRAY,
_MSTDNT_QUERY_FILE,
};
@ -39,6 +40,7 @@ struct _mstdnt_query_param
union param_value {
char* s;
int i;
mstdnt_bool b;
struct _mstdnt_query_array a;
struct mstdnt_file* f;
} value;

View file

@ -22,7 +22,10 @@
#define _mstdnt_arr_len(arr) (sizeof(arr)/sizeof(arr[0]))
#define MSTDNT_URLSIZE 2048
#define MSTDNT_URISIZE 512
typedef unsigned char mstdnt_bool;
typedef int8_t mstdnt_bool;
#define MSTDNT_TRUE 1
#define MSTDNT_FALSE 0
#define MSTDNT_BOOL_UNSET -1
// It's more logical to not sanitize than to sanitize data
#define MSTDNT_FLAG_NO_URI_SANITIZE (1<<0)

View file

@ -56,10 +56,12 @@ char* _mstdnt_query_string(mastodont_t* data,
int res_count = 0;
size_t arr_ind = 0;
char* key_ptr;
int invalid_bool;
/* If it's an array, we treat it as a "fake" regular key and keep iterating through it */
for (i = 0; i < param_len; ++i)
{
invalid_bool = 0;
escape_str = NULL;
/* If array is NULL, skip */
@ -81,9 +83,17 @@ char* _mstdnt_query_string(mastodont_t* data,
key_ptr = params[i].key;
}
// If boolean and set to 0, make it unset
if (params[i].type == _MSTDNT_QUERY_BOOL &&
!(params[i].value.b >= 1))
invalid_bool = 1;
// If invalid_bool is true, then it's not actually set
if (key_ptr &&
!(params[i].type == _MSTDNT_QUERY_STRING &&
params[i].value.s == NULL))
params[i].value.s == NULL) &&
!(params[i].type == _MSTDNT_QUERY_BOOL &&
invalid_bool))
{
if (res_count++ == 0 && src_l)
@ -91,17 +101,21 @@ char* _mstdnt_query_string(mastodont_t* data,
result[res_len-1] = '?';
/* Convert value */
if (params[i].type == _MSTDNT_QUERY_INT)
if (params[i].type == _MSTDNT_QUERY_BOOL)
{
if (params[i].value.b == 1)
val_ptr = "false";
else // Anything above 1, can't be zero though
val_ptr = "true";
}
else if (params[i].type == _MSTDNT_QUERY_INT) {
snprintf(conv_val, CONV_SIZE, "%d", params[i].value.i);
val_ptr = conv_val;
}
else if (params[i].type == _MSTDNT_QUERY_ARRAY)
{
else if (params[i].type == _MSTDNT_QUERY_ARRAY) {
val_ptr = params[i].value.a.arr[arr_ind];
}
else /* Point to it, it's a string */
{
else { /* Point to it, it's a string */
/* First, let's encode it */
escape_str = MSTDNT_T_FLAG_ISSET(args, MSTDNT_FLAG_NO_URI_SANITIZE) ?
params[i].value.s : curl_easy_escape(data->curl, params[i].value.s, 0);

View file

@ -33,16 +33,16 @@ int mastodont_timeline_list(mastodont_t* data,
snprintf(url, MSTDNT_URLSIZE, "api/v1/timelines/list/%s", list_id);
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_INT, "local", { .i = args->local } },
{ _MSTDNT_QUERY_INT, "remote", { .i = args->remote } },
{ _MSTDNT_QUERY_INT, "only_media", { .i = args->only_media } },
{ _MSTDNT_QUERY_BOOL, "local", { .b = args->local } },
{ _MSTDNT_QUERY_BOOL, "remote", { .b = args->remote } },
{ _MSTDNT_QUERY_BOOL, "only_media", { .b = args->only_media } },
{ _MSTDNT_QUERY_BOOL, "with_muted", { .b = args->with_muted } },
/* { _MSTDNT_QUERY_INT, "exclude_visibilities", { .i = args->only_media } }, */
{ _MSTDNT_QUERY_STRING, "max_id", { .s = args->max_id } },
{ _MSTDNT_QUERY_STRING, "since_id", { .s = args->since_id } },
{ _MSTDNT_QUERY_STRING, "min_id", { .s = args->min_id } },
{ _MSTDNT_QUERY_INT, "limit", { .i = args->limit } },
{ _MSTDNT_QUERY_INT, "offset", { .i = args->offset } },
{ _MSTDNT_QUERY_INT, "with_muted", { .i = args->with_muted } },
};
struct mastodont_request_args req_args = {
@ -80,11 +80,11 @@ int mastodont_timeline_tag(mastodont_t* data,
// TODO NONE
// TODO exclude_visibilities
{ _MSTDNT_QUERY_INT, "limit", { .i = args->limit } },
{ _MSTDNT_QUERY_INT, "local", { .i = args->local } },
{ _MSTDNT_QUERY_INT, "offset", { .i = args->offset } },
{ _MSTDNT_QUERY_INT, "remote", { .i = args->remote } },
{ _MSTDNT_QUERY_INT, "only_media", { .i = args->only_media } },
{ _MSTDNT_QUERY_INT, "with_muted", { .i = args->with_muted } },
{ _MSTDNT_QUERY_BOOL, "local", { .b = args->local } },
{ _MSTDNT_QUERY_BOOL, "remote", { .b = args->remote } },
{ _MSTDNT_QUERY_BOOL, "only_media", { .b = args->only_media } },
{ _MSTDNT_QUERY_BOOL, "with_muted", { .b = args->with_muted } },
};
struct mastodont_request_args req_args = {
@ -124,12 +124,12 @@ int mastodont_timeline_public(mastodont_t* data,
{
struct _mstdnt_statuses_cb_args cb_args = { statuses, size };
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_INT, "local", { .i = args->local } },
{ _MSTDNT_QUERY_BOOL, "local", { .b = args->local } },
{ _MSTDNT_QUERY_STRING, "instance", { .s = args->instance } },
{ _MSTDNT_QUERY_INT, "with_muted", { .i = args->with_muted } },
{ _MSTDNT_QUERY_BOOL, "with_muted", { .b = args->with_muted } },
{ _MSTDNT_QUERY_STRING, "reply_visibility", { .s = (char*)reply_visibility_str(args->reply_visibility) } },
{ _MSTDNT_QUERY_INT, "remote", { .i = args->remote } },
{ _MSTDNT_QUERY_INT, "only_media", { .i = args->only_media } },
{ _MSTDNT_QUERY_BOOL, "remote", { .b = args->remote } },
{ _MSTDNT_QUERY_BOOL, "only_media", { .b = args->only_media } },
{ _MSTDNT_QUERY_STRING, "max_id", { .s = args->max_id } },
{ _MSTDNT_QUERY_STRING, "since_id", { .s = args->since_id } },
{ _MSTDNT_QUERY_STRING, "min_id", { .s = args->min_id } },
@ -167,7 +167,7 @@ int mastodont_timeline_direct(mastodont_t* data,
{ _MSTDNT_QUERY_STRING, "min_id", { .s = args->min_id } },
{ _MSTDNT_QUERY_INT, "limit", { .i = args->limit } },
{ _MSTDNT_QUERY_INT, "offset", { .i = args->offset } },
{ _MSTDNT_QUERY_INT, "with_muted", { .i = args->with_muted } },
{ _MSTDNT_QUERY_BOOL, "with_muted", { .b = args->with_muted } },
};
struct mastodont_request_args req_args = {
@ -195,8 +195,8 @@ int mastodont_timeline_home(mastodont_t* data,
struct _mstdnt_statuses_cb_args cb_args = { statuses, size };
struct _mstdnt_query_param params[] = {
{ _MSTDNT_QUERY_INT, "local", { .i = args->local } },
{ _MSTDNT_QUERY_INT, "remote", { .i = args->remote } },
{ _MSTDNT_QUERY_BOOL, "local", { .b = args->local } },
{ _MSTDNT_QUERY_BOOL, "remote", { .b = args->remote } },
{ _MSTDNT_QUERY_INT, "only_media", { .i = args->only_media } },
{ _MSTDNT_QUERY_INT, "with_muted", { .i = args->with_muted } },
/* { _MSTDNT_QUERY_INT, "exclude_visibilities", { .i = args->with_muted } }, */