X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=util.h;h=bc23ecd9a6005fce68d6007bec1b6cb016845039;hb=e1cb276d972748a3044f1f7b694034170033e780;hp=697ca9ce04a2b387abe0f55dd90b9e2bbba2172e;hpb=776ac6910ee9db701c1cfe2ab234ae5fb96ce9db;p=wmpus diff --git a/util.h b/util.h index 697ca9c..bc23ecd 100644 --- a/util.h +++ b/util.h @@ -24,16 +24,16 @@ #define countof(x) (sizeof(x)/sizeof((x)[0])) /* Constant length map functions */ -#define map_getg(map, test) ({ \ - int i; \ - for (i = 0; i < countof(map) && !(test); i++); \ - i < countof(map) ? &map[i] : NULL ; \ +#define map_get(map, k, kv, v, def) ({ \ + typeof(def) val = def; \ + for (int i = 0; i < countof(map); i++) \ + if (map[i].k == kv) { \ + val = map[i].v; \ + break; \ + } \ + val; \ }) -#define map_get(m,k) map_getg(m,k==*((typeof(k)*)&m[i])) -#define map_getr(m,k) map_getg(m,k==*(((typeof(k)*)&m[i+1])-1)) -#define map_getk(m,k,a) map_getg(m,k==m[i].a) - /* Linked lists */ typedef struct list { struct list *prev; @@ -55,5 +55,11 @@ list_t *list_last(list_t *list); list_t *list_find(list_t *list, void *data); +list_t *list_sort(list_t *list, int rev, int (*func)(void*,void*)); + /* Misc */ +int str2num(char *str, int def); + +int warn(char *fmt, ...); + int error(char *fmt, ...);