Skip to content

Commit

Permalink
qsort replace: ARRAY_SORT and sort_t
Browse files Browse the repository at this point in the history
  • Loading branch information
roccoblues authored and flatcap committed Aug 10, 2023
1 parent 6d45184 commit 09eb460
Show file tree
Hide file tree
Showing 21 changed files with 62 additions and 57 deletions.
8 changes: 4 additions & 4 deletions alias/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ static short SortAlias = 0;
*
* @note Non-visible Aliases are sorted to the end
*/
static int alias_sort_name(const void *a, const void *b)
static int alias_sort_name(const void *a, const void *b, void *arg)
{
const struct AliasView *av_a = a;
const struct AliasView *av_b = b;
Expand All @@ -66,7 +66,7 @@ static int alias_sort_name(const void *a, const void *b)
*
* @note Non-visible Aliases are sorted to the end
*/
static int alias_sort_address(const void *a, const void *b)
static int alias_sort_address(const void *a, const void *b, void *arg)
{
const struct AliasView *av_a = a;
const struct AliasView *av_b = b;
Expand Down Expand Up @@ -126,7 +126,7 @@ static int alias_sort_address(const void *a, const void *b)
*
* @note Non-visible Aliases are sorted to the end
*/
static int alias_sort_unsort(const void *a, const void *b)
static int alias_sort_unsort(const void *a, const void *b, void *arg)
{
const struct AliasView *av_a = a;
const struct AliasView *av_b = b;
Expand Down Expand Up @@ -172,7 +172,7 @@ void alias_array_sort(struct AliasViewArray *ava, const struct ConfigSubset *sub
return;

SortAlias = cs_subset_sort(sub, "sort_alias");
ARRAY_SORT(ava, alias_get_sort_function(SortAlias));
ARRAY_SORT(ava, alias_get_sort_function(SortAlias), NULL);

struct AliasView *avp = NULL;
ARRAY_FOREACH(avp, ava)
Expand Down
8 changes: 4 additions & 4 deletions browser/sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ static int browser_sort_count_new(const void *a, const void *b)
* a way to tell "../" is always on the top of the list, independently of the
* sort method. $browser_sort_dirs_first is also handled here.
*/
static int browser_compare(const void *a, const void *b)
static int browser_compare(const void *a, const void *b, void *arg)
{
const struct FolderFile *pa = (const struct FolderFile *) a;
const struct FolderFile *pb = (const struct FolderFile *) b;
Expand All @@ -168,7 +168,7 @@ static int browser_compare(const void *a, const void *b)
if (S_ISDIR(pa->mode) != S_ISDIR(pb->mode))
return S_ISDIR(pa->mode) ? -1 : 1;

const enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
const enum SortType c_sort_browser = *(enum SortType *) arg;
switch (c_sort_browser & SORT_MASK)
{
case SORT_COUNT:
Expand Down Expand Up @@ -198,7 +198,7 @@ static int browser_compare(const void *a, const void *b)
*/
void browser_sort(struct BrowserState *state)
{
const enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
enum SortType c_sort_browser = cs_subset_sort(NeoMutt->sub, "sort_browser");
switch (c_sort_browser & SORT_MASK)
{
#ifdef USE_NNTP
Expand All @@ -211,5 +211,5 @@ void browser_sort(struct BrowserState *state)
break;
}

ARRAY_SORT(&state->entry, browser_compare);
ARRAY_SORT(&state->entry, browser_compare, &c_sort_browser);
}
2 changes: 1 addition & 1 deletion commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ enum CommandResult set_dump(ConfigDumpFlags flags, struct Buffer *err)
}

/**
* envlist_sort - Sort two environment strings
* envlist_sort - Sort two environment strings - Implements ::sort_t - @ingroup sort_api
* @param a First string
* @param b Second string
* @param arg (not used)
Expand Down
2 changes: 1 addition & 1 deletion complete/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ int mutt_command_complete(struct CompletionData *cd, struct Buffer *buf, int pos
}

/**
* label_sort - Sort two label strings
* label_sort - Sort two label strings - Implements ::sort_t - @ingroup sort_api
* @param a First string
* @param b Second string
* @param arg (not used)
Expand Down
2 changes: 1 addition & 1 deletion config/subset.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static const struct Mapping ConfigEventNames[] = {
};

/**
* elem_list_sort - Sort two HashElem pointers to config
* elem_list_sort - Sort two HashElem pointers to config - Implements ::sort_t - @ingroup sort_api
* @param a First HashElem
* @param b Second HashElem
* @param arg (not used)
Expand Down
4 changes: 2 additions & 2 deletions core/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static struct CommandArray Commands = ARRAY_HEAD_INITIALIZER;
/**
* commands_sort - Compare two commands by name - Implements ::sort_t - @ingroup sort_api
*/
static int commands_sort(const void *a, const void *b)
static int commands_sort(const void *a, const void *b, void *arg)
{
struct Command x = *(const struct Command *) a;
struct Command y = *(const struct Command *) b;
Expand All @@ -56,7 +56,7 @@ void commands_register(const struct Command *cmds, const size_t num_cmds)
{
ARRAY_ADD(&Commands, cmds[i]);
}
ARRAY_SORT(&Commands, commands_sort);
ARRAY_SORT(&Commands, commands_sort, NULL);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions imap/imap.c
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ bool imap_has_flag(struct ListHead *flag_list, const char *flag)
}

/**
* imap_sort_email_uid - Compare two Emails by UID
* imap_sort_email_uid - Compare two Emails by UID - Implements ::sort_t - @ingroup sort_api
* @param a First email to compare
* @param b Second email to compare
* @param arg (not used)
Expand Down Expand Up @@ -1388,7 +1388,7 @@ int imap_fast_trash(struct Mailbox *m, const char *dest)
{
struct UidArray uida = ARRAY_HEAD_INITIALIZER;
select_email_uids(m->emails, m->msg_count, MUTT_TRASH, false, false, &uida);
ARRAY_SORT(&uida, imap_sort_uid);
ARRAY_SORT(&uida, imap_sort_uid, NULL);
rc = imap_exec_msg_set(adata, "UID COPY", dest_mdata->munge_name, &uida);
if (rc == 0)
{
Expand Down Expand Up @@ -1488,7 +1488,7 @@ enum MxStatus imap_sync_mailbox(struct Mailbox *m, bool expunge, bool close)
{
struct UidArray uida = ARRAY_HEAD_INITIALIZER;
select_email_uids(m->emails, m->msg_count, MUTT_DELETED, true, false, &uida);
ARRAY_SORT(&uida, imap_sort_uid);
ARRAY_SORT(&uida, imap_sort_uid, NULL);
rc = imap_exec_msg_set(adata, "UID STORE", "+FLAGS.SILENT (\\Deleted)", &uida);
ARRAY_FREE(&uida);
if (rc < 0)
Expand Down
2 changes: 1 addition & 1 deletion imap/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -1650,7 +1650,7 @@ static int emails_to_uid_array(struct EmailArray *ea, struct UidArray *uida)

ARRAY_ADD(uida, edata->uid);
}
ARRAY_SORT(uida, imap_sort_uid);
ARRAY_SORT(uida, imap_sort_uid, NULL);

return ARRAY_SIZE(uida);
}
Expand Down
2 changes: 1 addition & 1 deletion imap/msg_set.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ int ImapMaxCmdlen = 8192;
/**
* imap_sort_uid - Compare two UIDs - Implements ::sort_t - @ingroup sort_api
*/
int imap_sort_uid(const void *a, const void *b)
int imap_sort_uid(const void *a, const void *b, void *arg)
{
unsigned int ua = *(unsigned int *) a;
unsigned int ub = *(unsigned int *) b;
Expand Down
2 changes: 1 addition & 1 deletion imap/msg_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct ImapAccountData;
/// Set of Email UIDs to work on
ARRAY_HEAD(UidArray, unsigned int);

int imap_sort_uid(const void *a, const void *b);
int imap_sort_uid(const void *a, const void *b, void *arg);
int imap_make_msg_set(struct UidArray *uida, struct Buffer *buf, int *pos);
int imap_exec_msg_set(struct ImapAccountData *adata, const char *pre, const char *post, struct UidArray *uida);

Expand Down
6 changes: 3 additions & 3 deletions maildir/maildir.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static void maildir_check_dir(struct Mailbox *m, const char *dir_name,
}

/**
* maildir_sort_flags - Compare two flag characters
* maildir_sort_flags - Compare two flag characters - Implements ::sort_t - @ingroup sort_api
* @param a First character to compare
* @param b Second character to compare
* @param arg (not used)
Expand Down Expand Up @@ -521,7 +521,7 @@ static void maildir_update_mtime(struct Mailbox *m)
/**
* maildir_sort_inode - Compare two Maildirs by inode number - Implements ::sort_t - @ingroup sort_api
*/
static int maildir_sort_inode(const void *a, const void *b)
static int maildir_sort_inode(const void *a, const void *b, void *arg)
{
const struct MdEmail *ma = *(struct MdEmail **) a;
const struct MdEmail *mb = *(struct MdEmail **) b;
Expand Down Expand Up @@ -590,7 +590,7 @@ static int maildir_parse_dir(struct Mailbox *m, struct MdEmailArray *mda,
return -2; /* action aborted */
}

ARRAY_SORT(mda, maildir_sort_inode);
ARRAY_SORT(mda, maildir_sort_inode, NULL);

cleanup:
buf_pool_release(&buf);
Expand Down
10 changes: 5 additions & 5 deletions maildir/mh.c
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,11 @@ static int mh_parse_dir(struct Mailbox *m, struct MdEmailArray *mda, struct Prog
/**
* mh_sort_path - Compare two Maildirs by path - Implements ::sort_t - @ingroup sort_api
*/
static int mh_sort_path(const void *a, const void *b)
static int mh_sort_path(const void *a, const void *b, void *arg)
{
struct MdEmail const *const *pa = (struct MdEmail const *const *) a;
struct MdEmail const *const *pb = (struct MdEmail const *const *) b;
return mutt_str_cmp((*pa)->email->path, (*pb)->email->path);
struct MdEmail const *pa = *(struct MdEmail const *const *) a;
struct MdEmail const *pb = *(struct MdEmail const *const *) b;
return mutt_str_cmp(pa->email->path, pb->email->path);
}

/**
Expand Down Expand Up @@ -671,7 +671,7 @@ static void mh_delayed_parsing(struct Mailbox *m, struct MdEmailArray *mda,
if (m && mda && (ARRAY_SIZE(mda) > 0) && (c_sort == SORT_ORDER))
{
mutt_debug(LL_DEBUG3, "maildir: sorting %s into natural order\n", mailbox_path(m));
ARRAY_SORT(mda, mh_sort_path);
ARRAY_SORT(mda, mh_sort_path, NULL);
}
}

Expand Down
6 changes: 4 additions & 2 deletions mutt/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,11 @@
* ARRAY_SORT - Sort an array
* @param head Pointer to a struct defined using ARRAY_HEAD()
* @param fn Sort function, see ::sort_t
* @param arg Opaque argument to pass to sort function
*/
#define ARRAY_SORT(head, fn) \
((head)->entries && (qsort((head)->entries, ARRAY_SIZE(head), ARRAY_ELEM_SIZE(head), (fn)), true))
#define ARRAY_SORT(head, fn, arg) \
((head)->entries && \
(mutt_qsort_r((head)->entries, ARRAY_SIZE(head), ARRAY_ELEM_SIZE(head), (fn), (arg)), true))

/******************************************************************************
* Internal APIs
Expand Down
2 changes: 1 addition & 1 deletion mutt_thread.c
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ void mutt_clear_threads(struct ThreadsContext *tctx)
}

/**
* compare_threads - qsort_r() function for comparing email threads
* compare_threads - qsort_r() function for comparing email threads - Implements ::sort_t - @ingroup sort_api
* @param a First thread to compare
* @param b Second thread to compare
* @param arg ThreadsContext for how to compare
Expand Down
11 changes: 6 additions & 5 deletions ncrypt/dlg_gpgme.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "mutt_logging.h"
#include "muttlib.h"
#include "opcodes.h"
#include "sort.h"

/// Help Bar for the GPGME key selection dialog
static const struct Mapping GpgmeHelp[] = {
Expand Down Expand Up @@ -130,7 +131,7 @@ static int crypt_compare_key_address(const void *a, const void *b)
}

/**
* crypt_compare_address_qsort - Compare the addresses of two keys
* crypt_compare_address_qsort - Compare the addresses of two keys - Implements ::sort_t - @ingroup sort_api
* @param a First key
* @param b Second key
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -165,7 +166,7 @@ static int crypt_compare_keyid(const void *a, const void *b)
}

/**
* crypt_compare_keyid_qsort - Compare the IDs of two keys
* crypt_compare_keyid_qsort - Compare the IDs of two keys - Implements ::sort_t - @ingroup sort_api
* @param a First key ID
* @param b Second key ID
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -207,7 +208,7 @@ static int crypt_compare_key_date(const void *a, const void *b)
}

/**
* crypt_compare_date_qsort - Compare the dates of two keys
* crypt_compare_date_qsort - Compare the dates of two keys - Implements ::sort_t - @ingroup sort_api
* @param a First key
* @param b Second key
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -271,7 +272,7 @@ static int crypt_compare_key_trust(const void *a, const void *b)
}

/**
* crypt_compare_trust_qsort - Compare the trust levels of two keys
* crypt_compare_trust_qsort - Compare the trust levels of two keys - Implements ::sort_t - @ingroup sort_api
* @param a First key
* @param b Second key
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -682,7 +683,7 @@ struct CryptKeyInfo *dlg_select_gpgme_key(struct CryptKeyInfo *keys,
{
int keymax;
int i;
int (*f)(const void *, const void *, void *); // TODO: use sort_t
sort_t f = NULL;
enum MenuType menu_to_use = MENU_GENERIC;
bool unusable = false;

Expand Down
11 changes: 6 additions & 5 deletions ncrypt/dlg_pgp.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
#include "pgp_functions.h"
#include "pgpkey.h"
#include "pgplib.h"
#include "sort.h"

/// Help Bar for the PGP key selection dialog
static const struct Mapping PgpHelp[] = {
Expand Down Expand Up @@ -134,7 +135,7 @@ static int pgp_compare_key_address(const void *a, const void *b)
}

/**
* pgp_compare_address_qsort - Compare the addresses of two PGP keys
* pgp_compare_address_qsort - Compare the addresses of two PGP keys - Implements ::sort_t - @ingroup sort_api
* @param a First address
* @param b Second address
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -168,7 +169,7 @@ static int pgp_compare_key_date(const void *a, const void *b)
}

/**
* pgp_compare_date_qsort - Compare the dates of two PGP keys
* pgp_compare_date_qsort - Compare the dates of two PGP keys - Implements ::sort_t - @ingroup sort_api
* @param a First key
* @param b Second key
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -202,7 +203,7 @@ static int pgp_compare_keyid(const void *a, const void *b)
}

/**
* pgp_compare_keyid_qsort - Compare key IDs
* pgp_compare_keyid_qsort - Compare key IDs - Implements ::sort_t - @ingroup sort_api
* @param a First key ID
* @param b Second key ID
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -252,7 +253,7 @@ static int pgp_compare_key_trust(const void *a, const void *b)
}

/**
* pgp_compare_trust_qsort - Compare the trust levels of two PGP keys
* pgp_compare_trust_qsort - Compare the trust levels of two PGP keys - Implements ::sort_t - @ingroup sort_api
* @param a First key
* @param b Second key
* @param arg Boolean indicating reverse sort order
Expand Down Expand Up @@ -635,7 +636,7 @@ struct PgpKeyInfo *dlg_select_pgp_key(struct PgpKeyInfo *keys,
return NULL;
}

int (*f)(const void *, const void *, void *); // TODO: use sort_t
sort_t f = NULL;
short c_pgp_sort_keys = cs_subset_sort(NeoMutt->sub, "pgp_sort_keys");
switch (c_pgp_sort_keys & SORT_MASK)
{
Expand Down
Loading

0 comments on commit 09eb460

Please sign in to comment.