]> Pileus Git - wmpus/blob - util.h
Add desktop files
[wmpus] / util.h
1 /*
2  * Copyright (c) 2011, Andy Spencer <andy753421@gmail.com>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  */
15
16 /* Various utility functions */
17
18 /* Misc macros */
19 #define MAX(a,b) ((a) > (b) ? (a) : (b))
20 #define MIN(a,b) ((a) < (b) ? (a) : (b))
21
22 #define new0(type) (calloc(1, sizeof(type)))
23
24 #define countof(x) (sizeof(x)/sizeof((x)[0]))
25
26 /* Constant length map functions */
27 #define map_getg(map, test) ({ \
28         int i; \
29         for (i = 0; i < countof(map) && !(test); i++); \
30         i < countof(map) ? &map[i] : NULL ; \
31 })
32
33 #define map_get(m,k)    map_getg(m,k==*((typeof(k)*)&m[i]))
34 #define map_getr(m,k)   map_getg(m,k==*(((typeof(k)*)&m[i+1])-1))
35 #define map_getk(m,k,a) map_getg(m,k==m[i].a)
36
37 /* Linked lists */
38 typedef struct list {
39         struct list *prev;
40         struct list *next;
41         void   *data;
42 } list_t;
43
44 list_t *list_insert(list_t *after, void *data);
45
46 void list_insert_after(list_t *after, void *data);
47
48 list_t *list_append(list_t *before, void *data);
49
50 list_t *list_remove(list_t *head, list_t *item, int freedata);
51
52 int list_length(list_t *item);
53
54 list_t *list_last(list_t *list);
55
56 list_t *list_find(list_t *list, void *data);
57
58 list_t *list_sort(list_t *list, int rev, int (*func)(void*,void*));
59
60 /* Misc */
61 int str2num(char *str, int def);
62
63 int error(char *fmt, ...);