]> git.sur5r.net Git - i3/i3/blob - i3bar/include/util.h
9ffd4467f56ceb3f5078e0432b0c569dd3565b60
[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 #pragma once
9
10 #include "queue.h"
11
12 /* Get the maximum/minimum of x and y */
13 #undef MAX
14 #define MAX(x,y) ((x) > (y) ? (x) : (y))
15 #undef MIN
16 #define MIN(x,y) ((x) < (y) ? (x) : (y))
17
18 #define STARTS_WITH(string, len, needle) ((len >= strlen(needle)) && strncasecmp(string, needle, strlen(needle)) == 0)
19
20 /* Securely free p */
21 #define FREE(p) do { \
22     if (p != NULL) { \
23         free(p); \
24         p = NULL; \
25     } \
26 } while (0)
27
28 /* Securely fee single-linked list */
29 #define FREE_SLIST(l, type) do { \
30     type *walk = SLIST_FIRST(l); \
31     while (!SLIST_EMPTY(l)) { \
32         SLIST_REMOVE_HEAD(l, slist); \
33         FREE(walk); \
34         walk = SLIST_FIRST(l); \
35     } \
36 } while (0)
37
38 /* Securely fee tail-queues */
39 #define FREE_TAILQ(l, type) do { \
40     type *walk = TAILQ_FIRST(l); \
41     while (!TAILQ_EMPTY(l)) { \
42         TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
43         FREE(walk); \
44         walk = TAILQ_FIRST(l); \
45     } \
46 } while (0)
47
48 #if defined(DLOG)
49 #undef DLOG
50 #endif
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)