-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.h
118 lines (101 loc) · 4.04 KB
/
util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* See LICENSE file for copyright and license details. */
#ifndef UTIL_H
#define UTIL_H
#define MAX(A, B) ((A) > (B) ? (A) : (B))
#define MIN(A, B) ((A) < (B) ? (A) : (B))
#define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
#ifndef NDEBUG
# define DEBUG_PRINTF(...) \
do { \
fprintf(log_file, "[DWM DBG]: " __VA_ARGS__); \
fputc('\n', log_file); \
fflush(log_file); \
} while (0)
# define DEBUG_PRINT(VAR, FMT) fprintf(log_file, "[DWM DBG]: " #VAR " = " #FMT, VAR)
# define DEBUG_PRINT_ARR(ARR, SZ, FMT) \
do { \
fputs("[DWM DBG]: " #ARR ":", log_file); \
for (size_t i = 0; i < SZ; i++) { \
fprintf(log_file, "\t" #ARR "[%lu] = " FMT "\n", i, ARR[i]); \
} \
} while (0)
# define IF_DEBUG if (1)
# ifdef DWM_TRACE_EVENTS
# define IF_EVENT_TRACE if (1)
# else
# define IF_EVENT_TRACE if (0)
# endif
#else // NDEBUG
# define DEBUG_PRINTF(...) (void)0
# define DEBUG_PRINT(VAR, FMT) (void)0
# define DEBUG_PRINT_ARR(ARR, SZ, FMT) (void)0
# define IF_DEBUG if (0)
# define IF_EVENT_TRACE if (0)
#endif // NDEBUG
#define LOG(...) \
do { \
if (log_file) { \
fprintf(log_file, "[DWM]: " __VA_ARGS__); \
fputc('\n', log_file); \
fflush(log_file); \
} else { \
die("Log file closed"); \
} \
} while (0)
#define WARN(...) \
do { \
if (log_file) { \
fprintf(log_file, "[DWM WARNING]: " __VA_ARGS__); \
fputc('\n', log_file); \
} else { \
die("Log file closed"); \
} \
} while (0)
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#ifdef __GNUC__
__attribute__((format(printf, 1, 2)))
#endif
void die(char const *fmt, ...);
void *ecalloc(size_t nmemb, size_t size);
void delay(int delay_for, void (*fn)(void *), void *arg);
extern FILE *log_file;
/**
* Get dwm log directory
*
* Caller owns returned string
*
* If an error occurs, `NULL` is returned
*/
char *getLogDir(void);
/**
* Concatenates any number of strings until NULL is reached
*
* All strings are copied. Caller owns returned memory allocated with `malloc`
*
* If `first` is `NULL`, `NULL` is returned
*
* NOTE: Even if all strings are empty, a new string of length 1 will be allocated containing the null
* terminator.
*/
char *buildString(char const *first, ...);
/// same as `buildString`. but takes a `va_list`
char *buildStringV(char const *first, va_list args);
/// same as `buildString` but deallocates `first` before retuning
char *buildStringDealloc(char *first, ...);
/// same as `buildStringV` but deallocates `first` before retuning
char *buildStringDeallocV(char *first, va_list args);
/**
* Like mkdir -p
*
* If directory exists does nothing and returns 0
*
* If directory does not exist tries to create all directories leading up to it.
*
* If any errors occur, returns -1 and sets `errno` appropriatly.
*
* If part of the intermediate path is not a directory, sets errno to EEXISTS
*/
int mkdirP(char const *dir_name, int mode);
#endif // UTIL_H