Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 03c4b23 in mainline for uspace/app/nav/panel.c


Ignore:
Timestamp:
2021-10-15T16:13:21Z (16 months ago)
Author:
Jiri Svoboda <jiri@…>
Children:
9bed565
Parents:
fe5c7a1
Message:

Sort panel entries

File:
1 edited

Legend:

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

    rfe5c7a1 r03c4b23  
    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.