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