radare2/libr/flags/sort.c

55 lines
1.1 KiB
C
Raw Normal View History

2015-01-09 16:37:06 +00:00
/* radare - LGPL - Copyright 2007-2015 pancake */
2011-05-21 20:14:59 +00:00
#include <r_flags.h>
/* compare names */
static int ncmp(const void *a, const void *b) {
RFlagItem *fa = (RFlagItem *)a;
RFlagItem *fb = (RFlagItem *)b;
return strcmp (fa->name, fb->name);
}
/* compare offsets */
static int cmp(const void *a, const void *b) {
RFlagItem *fa = (RFlagItem *)a;
RFlagItem *fb = (RFlagItem *)b;
if (fa->offset > fb->offset) return 1;
else if (fa->offset < fb->offset) return -1;
return 0;
}
R_API int r_flag_sort(RFlag *f, int namesort) {
2015-09-14 00:08:31 +00:00
int ret = false;
2011-05-21 20:14:59 +00:00
int changes;
RFlagItem *flag, *fi = NULL;
RListIter *iter, *it_elem;
RList *tmp = r_list_new ();
// find bigger ones after this
do {
changes = 0;
fi = NULL;
r_list_foreach (f->flags, iter, flag) {
if (!fi) {
2011-05-21 20:14:59 +00:00
fi = flag;
it_elem = iter;
changes = 1;
2015-01-09 16:37:06 +00:00
} else if (((namesort)? ncmp (fi, flag): cmp (fi, flag)) > 0) {
2011-05-21 20:14:59 +00:00
fi = flag;
it_elem = iter;
changes = 1;
}
}
if (fi && changes) {
2015-09-14 00:08:31 +00:00
ret = true;
2011-05-21 20:14:59 +00:00
r_list_split_iter (f->flags, it_elem);
free (it_elem);
r_list_append (tmp, fi);
}
} while (changes);
free (f->flags);
f->flags = tmp;
f->flags->free = free;
return ret;
}