Changeset 1eb0fafe in mainline for uspace/app/nav/panel.c


Ignore:
Timestamp:
2021-10-25T00:32:45Z (2 years ago)
Author:
jxsvoboda <5887334+jxsvoboda@…>
Branches:
master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fa792e8
Parents:
692c7f40
git-author:
Jiri Svoboda <jiri@…> (2021-10-15 16:13:21)
git-committer:
jxsvoboda <5887334+jxsvoboda@…> (2021-10-25 00:32:45)
Message:

Sort panel entries

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nav/panel.c

    r692c7f40 r1eb0fafe  
    4444#include <ui/paint.h>
    4545#include <ui/resource.h>
     46#include <qsort.h>
    4647#include "panel.h"
    4748#include "nav.h"
     
    484485        closedir(dir);
    485486
     487        rc = panel_sort(panel);
     488        if (rc != EOK)
     489                goto error;
     490
    486491        panel->cursor = panel_first(panel);
    487492        panel->cursor_idx = 0;
     
    492497        closedir(dir);
    493498        return rc;
     499}
     500
     501/** Sort panel entries.
     502 *
     503 * @param panel Panel
     504 * @return EOK on success, ENOMEM if out of memory
     505 */
     506errno_t panel_sort(panel_t *panel)
     507{
     508        panel_entry_t **emap;
     509        panel_entry_t *entry;
     510        size_t i;
     511
     512        /* Create an array to hold pointer to each entry */
     513        emap = calloc(panel->entries_cnt, sizeof(panel_entry_t *));
     514        if (emap == NULL)
     515                return ENOMEM;
     516
     517        /* Write entry pointers to array */
     518        entry = panel_first(panel);
     519        i = 0;
     520        while (entry != NULL) {
     521                assert(i < panel->entries_cnt);
     522                emap[i++] = entry;
     523                entry = panel_next(entry);
     524        }
     525
     526        /* Sort the array of pointers */
     527        qsort(emap, panel->entries_cnt, sizeof(panel_entry_t *),
     528            panel_entry_ptr_cmp);
     529
     530        /* Unlink entries from entry list */
     531        entry = panel_first(panel);
     532        while (entry != NULL) {
     533                list_remove(&entry->lentries);
     534                entry = panel_first(panel);
     535        }
     536
     537        /* Add entries back to entry list sorted */
     538        for (i = 0; i < panel->entries_cnt; i++)
     539                list_append(&emap[i]->lentries, &panel->entries);
     540
     541        free(emap);
     542        return EOK;
     543}
     544
     545/** Compare two panel entries indirectly referenced by pointers.
     546 *
     547 * @param pa Pointer to pointer to first entry
     548 * @param pb Pointer to pointer to second entry
     549 * @return <0, =0, >=0 if pa < b, pa == pb, pa > pb, resp.
     550 */
     551int panel_entry_ptr_cmp(const void *pa, const void *pb)
     552{
     553        panel_entry_t *a = *(panel_entry_t **)pa;
     554        panel_entry_t *b = *(panel_entry_t **)pb;
     555
     556        return str_cmp(a->name, b->name);
    494557}
    495558
Note: See TracChangeset for help on using the changeset viewer.