gri3-wm/i3bar/include/util.h

71 lines
2.4 KiB
C
Raw Normal View History

2010-08-07 18:05:16 +02:00
/*
* vim:ts=4:sw=4:expandtab
2010-08-07 18:05:16 +02:00
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
2010-08-07 18:05:16 +02:00
*
*/
#pragma once
2010-07-22 01:15:18 +02:00
2010-07-30 03:11:54 +02:00
#include "queue.h"
2010-09-17 01:16:53 +02:00
/* Get the maximum/minimum of x and y */
2012-08-05 21:36:49 +02:00
#undef MAX
#define MAX(x, y) ((x) > (y) ? (x) : (y))
2012-08-05 21:36:49 +02:00
#undef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
2010-09-17 01:16:53 +02:00
#define STARTS_WITH(string, len, needle) (((len) >= strlen((needle))) && strncasecmp((string), (needle), strlen((needle))) == 0)
2012-09-03 10:43:29 +02:00
2010-07-22 01:15:18 +02:00
/* Securely free p */
#define FREE(p) \
do { \
if (p != NULL) { \
free(p); \
p = NULL; \
} \
} while (0)
2010-07-22 01:15:18 +02:00
2015-03-24 13:41:16 +01:00
/* Securely free single-linked list */
#define FREE_SLIST(l, type) \
do { \
type *walk = SLIST_FIRST(l); \
while (!SLIST_EMPTY(l)) { \
SLIST_REMOVE_HEAD(l, slist); \
FREE(walk); \
walk = SLIST_FIRST(l); \
} \
} while (0)
2010-07-22 01:15:18 +02:00
2015-03-24 13:41:16 +01:00
/* Securely free tail queue */
#define FREE_TAILQ(l, type) \
do { \
type *walk = TAILQ_FIRST(l); \
while (!TAILQ_EMPTY(l)) { \
TAILQ_REMOVE(l, TAILQ_FIRST(l), tailq); \
FREE(walk); \
walk = TAILQ_FIRST(l); \
} \
} while (0)
#if defined(DLOG)
#undef DLOG
#endif
2015-03-23 20:56:49 +01:00
/* Use cool logging macros */
#define DLOG(fmt, ...) \
do { \
if (config.verbose) { \
printf("[%s:%d] " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
} \
} while (0)
/* We will include libi3.h which define its own version of ELOG.
* We want *our* version, so we undef the libi3 one. */
#if defined(ELOG)
#undef ELOG
#endif
#define ELOG(fmt, ...) \
do { \
fprintf(stderr, "[%s:%d] ERROR: " fmt, __FILE__, __LINE__, ##__VA_ARGS__); \
} while (0)