Began versioning (version 0.5)
[stripchart.git] / err_mac.h
1 /* need to include STDIO.H for FPRINTF() */
2
3 #define SUCCESS 0
4 #define FAILURE 1
5
6 #ifndef TRUE
7 #define TRUE 1
8 #endif
9 #ifndef FALSE
10 #define FALSE 0
11 #endif
12 static int err_mac_ans;
13
14 /*
15  * For functions returning int, when you want an immediate return.
16  */
17
18 /* CHK() fails if (a != SUCCESS) */
19 #define CHK(a)                                                        \
20   do {                                                                \
21     if( (err_mac_ans = (a)) != SUCCESS ) {                            \
22       fprintf(stderr,"FAILURE: %s.%d. (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
23       fflush(stderr);                                                 \
24       return err_mac_ans;                                             \
25     }                                                                 \
26   } while (0)
27
28 /* AST() fails if (a != 0) */
29 #define AST(a)                                                        \
30   do {                                                                \
31     if( (err_mac_ans = (a)) == FALSE ) {                              \
32       fprintf(stderr, "ASSERT FAILED: %s.%d (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
33       fflush(stderr);                                                 \
34       return FAILURE;                                                 \
35     }                                                                 \
36   } while(0)
37
38 /* like AST(), but return E_CODE instead of the default FAILURE */
39 #define E_AST(a,e_code)                                               \
40   do {                                                                \
41     if((a) == FALSE) {                                                \
42       fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
43       fflush(stderr);                                                 \
44       return (e_code);                                                \
45     }                                                                 \
46   } while(0)
47
48 /* like E_AST(), but also fprintfs __VA_ARGS__ */
49 #define M_AST(a,e_code,...)                                           \
50   do {                                                                \
51     if((a) == FALSE) {                                                \
52       fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
53       fprintf(stderr, __VA_ARGS__);                                   \
54       fflush(stderr);                                                 \
55       return (e_code);                                                \
56     }                                                                 \
57   } while(0)
58
59 /* print a message and return E_CODE */
60 #define M_EXIT(e_code, ... )                               \
61   do {                                                     \
62     fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__);      \
63     fprintf(stderr, __VA_ARGS__ );                         \
64     fflush(stderr);                                        \
65     return (e_code);                                       \
66   } while(0)
67
68 /*
69  * When you don't want an immedate return, but would rather 'goto Error;'.
70  * These set the local variable RC with the return value before jumping.
71  */
72
73 #define G_CHK(a)                                                      \
74   do {                                                                \
75     if( (rc = (a)) != SUCCESS ) {                                     \
76       fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
77       fflush(stderr);                                                 \
78       goto Error;                                                     \
79     }                                                                 \
80   } while (0)
81
82 #define G_AST(a)                                                      \
83   do {                                                                \
84     if( (rc = (a)) == FALSE ) {                                       \
85       fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
86       fflush(stderr);                                                 \
87       goto Error;                                                     \
88     }                                                                 \
89   } while(0)
90
91 #define G_E_AST(a,e_code)                                             \
92   do {                                                                \
93     if((a) == FALSE) {                                                \
94       fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
95       fflush(stderr);                                                 \
96       rc = e_code;                                                    \
97       goto Error;                                                     \
98     }                                                                 \
99   } while(0)
100
101 #define G_M_AST(a,e_code,...)                                         \
102   do {                                                                \
103     if((a) == FALSE) {                                                \
104       fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
105       fprintf(stderr, __VA_ARGS__);                                   \
106       fflush(stderr);                                                 \
107       rc = e_code;                                                    \
108       goto Error;                                                     \
109     }                                                                 \
110   } while(0)
111
112 /* print a message and return E_CODE */
113 #define G_M_EXIT(e_code, ... )                             \
114   do {                                                     \
115     fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__);      \
116     fprintf(stderr, __VA_ARGS__ );                         \
117     fflush(stderr);                                        \
118     rc = e_code;                                           \
119     goto Error;                                            \
120   } while(0)
121
122 /*
123  * And when you only want to print a message
124  */
125 #define P_CHK(a)                                                      \
126   do {                                                                \
127     if( (a) != SUCCESS ) {                                            \
128       fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
129       fflush(stderr);                                                 \
130     }                                                                 \
131   } while (0)
132
133 /* AST() fails if (a != 0) */
134 #define P_AST(a)                                                      \
135   do {                                                                \
136     if( (a) == FALSE ) {                                              \
137       fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
138       fflush(stderr);                                                 \
139     }                                                                 \
140   } while(0)