raw: Add include/ folder.
[swifty.git] / src / raw / include / util.h
1 /*
2  * useful structures/macros
3  *
4  * 2011, Operating Systems
5  */
6
7 #ifndef UTIL_H_
8 #define UTIL_H_         1
9
10 #ifdef __cplusplus
11 extern "C" {
12 #endif
13
14 #include <stdio.h>
15 #include <stdlib.h>
16
17 #if defined (_WIN32)
18
19 #include <windows.h>
20
21 static VOID PrintLastError(const PCHAR message)
22 {
23         CHAR errBuff[1024];
24
25         FormatMessage(
26                         FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_MAX_WIDTH_MASK,
27                         NULL,
28                         GetLastError(),
29                         0,
30                         errBuff,
31                         sizeof(errBuff) - 1,
32                         NULL);
33
34         fprintf(stderr, "%s: %s\n", message, errBuff);
35 }
36
37 #define ERR(call_description)                                   \
38         do {                                                    \
39                 fprintf(stderr, "(%s, %d): ",                   \
40                                 __FILE__, __LINE__);            \
41                         PrintLastError(call_description);       \
42         } while (0)
43
44 #elif defined (__linux__)
45
46 /* error printing macro */
47 #define ERR(call_description)                           \
48         do {                                            \
49                 fprintf(stderr, "(%s, %d): ",           \
50                                 __FILE__, __LINE__);    \
51                         perror(call_description);       \
52         } while (0)
53
54 #else
55   #error "Unknown platform"
56 #endif
57
58 /* print error (call ERR) and exit */
59 #define DIE(assertion, call_description)                \
60         do {                                            \
61                 if (assertion) {                        \
62                         ERR(call_description);          \
63                         exit(EXIT_FAILURE);             \
64                 }                                       \
65         } while(0)
66
67 #ifdef __cplusplus
68 }
69 #endif
70
71 #endif