-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.h
72 lines (56 loc) · 1.12 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
/*
* useful structures/macros
*
* 2011-2017, Operating Systems
*/
#ifndef UTIL_H_
#define UTIL_H_ 1
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
#include <windows.h>
static VOID PrintLastError(const PCHAR message)
{
CHAR errBuff[1024];
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
NULL,
GetLastError(),
0,
errBuff,
sizeof(errBuff) - 1,
NULL);
fprintf(stderr, "%s: %s\n", message, errBuff);
}
#define ERR(call_description) \
do { \
fprintf(stderr, "(%s, %d): ", \
__FILE__, __LINE__); \
PrintLastError(call_description); \
} while (0)
#elif defined(__linux__)
/* error printing macro */
#define ERR(call_description) \
do { \
fprintf(stderr, "(%s, %d): ", \
__FILE__, __LINE__); \
perror(call_description); \
} while (0)
#else
#error "Unknown platform"
#endif
/* print error (call ERR) and exit */
#define DIE(assertion, call_description) \
do { \
if (assertion) { \
ERR(call_description); \
exit(EXIT_FAILURE); \
} \
} while (0)
#ifdef __cplusplus
}
#endif
#endif