]> git.sur5r.net Git - i3/i3/blob - i3bar/include/util.h
i3bar: Fully parse the JSON header
[i3/i3] / i3bar / include / util.h
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  */
8 #ifndef UTIL_H_
9 #define UTIL_H_
10
11 #include "queue.h"
12
13 /* Get the maximum/minimum of x and y */
14 #undef MAX
15 #define MAX(x,y) ((x) > (y) ? (x) : (y))
16 #undef MIN
17 #define MIN(x,y) ((x) < (y) ? (x) : (y))
18
19 #define STARTS_WITH(string, len, needle) ((len >= strlen(needle)) && strncasecmp(string, needle, strlen(needle)) == 0)
20
21 /* Securely free p */
22 #define FREE(p) do { \
23     if (p != NULL) { \
24         free(p); \
25         p = NULL; \
26     } \
27 } while (0)
28
29 /* Securely fee single-linked list */
30 #define FREE_SLIST(l, type) do { \
31     type *walk = SLIST_FIRST(l); \
32     while (!SLIST_EMPTY(l)) { \
33         SLIST_REMOVE_HEAD(l, slist); \
34         FREE(walk); \
35         walk = SLIST_FIRST(l); \
36     } \
37 } while (0)
38
39 #endif
40
41 /* Securely fee tail-queues */
42 #define FREE_TAILQ(l, type) do { \
43     type *walk = TAILQ_FIRST(l); \
44     while (!TAILQ_EMPTY(l)) { \
45         TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
46         FREE(walk); \
47         walk = TAILQ_FIRST(l); \
48     } \
49 } while (0)
50
51 /* Use cool logging-macros */
52 #define DLOG(fmt, ...) do { \
53     if (config.verbose) { \
54         printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
55     } \
56 } while(0)
57
58 /* We will include libi3.h which define its own version of ELOG.
59  * We want *our* version, so we undef the libi3 one. */
60 #if defined(ELOG)
61 #undef ELOG
62 #endif
63 #define ELOG(fmt, ...) do { \
64     fprintf(stderr, "[%s:%d] ERROR: " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
65 } while(0)