Added ifndef/def/endif protection to C headers.
[stripchart.git] / err_mac.h
1 /* 
2   err_mac - convenient macros for error-checking
3  
4   Copyright (C) 2007-2009 William Trevor King
5  
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License as
8   published by the Free Software Foundation; either version 3 of the
9   License, or (at your option) any later version.
10  
11   This program is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   See the GNU General Public License for more details.
15  
16   You should have received a copy of the GNU General Public License
17   along with this program; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20  
21   The author may be contacted at <wking@drexel.edu> on the Internet, or
22   write to Trevor King, Drexel University, Physics Dept., 3141 Chestnut St.,
23   Philadelphia PA 19104, USA.
24 */
25
26 #ifndef _ERR_MAC_H
27 #define _ERR_MAC_H
28
29 #include <stdio.h> /* for fprintf(), stderr */
30
31 #define SUCCESS 0
32 #define FAILURE 1
33
34 #ifndef TRUE
35 #define TRUE 1
36 #endif
37 #ifndef FALSE
38 #define FALSE 0
39 #endif
40 static int err_mac_ans;
41
42 /*
43  * For functions returning int, when you want an immediate return.
44  */
45
46 /* CHK() fails if (a != SUCCESS) */
47 #define CHK(a)                                                        \
48   do {                                                                \
49     if( (err_mac_ans = (a)) != SUCCESS ) {                            \
50       fprintf(stderr,"FAILURE: %s.%d. (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
51       fflush(stderr);                                                 \
52       return err_mac_ans;                                             \
53     }                                                                 \
54   } while (0)
55
56 /* AST() fails if (a != 0) */
57 #define AST(a)                                                        \
58   do {                                                                \
59     if( (err_mac_ans = (a)) == FALSE ) {                              \
60       fprintf(stderr, "ASSERT FAILED: %s.%d (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
61       fflush(stderr);                                                 \
62       return FAILURE;                                                 \
63     }                                                                 \
64   } while(0)
65
66 /* like AST(), but return E_CODE instead of the default FAILURE */
67 #define E_AST(a,e_code)                                               \
68   do {                                                                \
69     if((a) == FALSE) {                                                \
70       fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
71       fflush(stderr);                                                 \
72       return (e_code);                                                \
73     }                                                                 \
74   } while(0)
75
76 /* like E_AST(), but also fprintfs __VA_ARGS__ */
77 #define M_AST(a,e_code,...)                                           \
78   do {                                                                \
79     if((a) == FALSE) {                                                \
80       fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
81       fprintf(stderr, __VA_ARGS__);                                   \
82       fflush(stderr);                                                 \
83       return (e_code);                                                \
84     }                                                                 \
85   } while(0)
86
87 /* print a message and return E_CODE */
88 #define M_EXIT(e_code, ... )                               \
89   do {                                                     \
90     fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__);      \
91     fprintf(stderr, __VA_ARGS__ );                         \
92     fflush(stderr);                                        \
93     return (e_code);                                       \
94   } while(0)
95
96 /*
97  * When you don't want an immedate return, but would rather 'goto Error;'.
98  * These set the local variable RC with the return value before jumping.
99  */
100
101 #define G_CHK(a)                                                      \
102   do {                                                                \
103     if( (rc = (a)) != SUCCESS ) {                                     \
104       fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
105       fflush(stderr);                                                 \
106       goto Error;                                                     \
107     }                                                                 \
108   } while (0)
109
110 #define G_AST(a)                                                      \
111   do {                                                                \
112     if( (rc = (a)) == FALSE ) {                                       \
113       fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
114       fflush(stderr);                                                 \
115       goto Error;                                                     \
116     }                                                                 \
117   } while(0)
118
119 #define G_E_AST(a,e_code)                                             \
120   do {                                                                \
121     if((a) == FALSE) {                                                \
122       fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
123       fflush(stderr);                                                 \
124       rc = e_code;                                                    \
125       goto Error;                                                     \
126     }                                                                 \
127   } while(0)
128
129 #define G_M_AST(a,e_code,...)                                         \
130   do {                                                                \
131     if((a) == FALSE) {                                                \
132       fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
133       fprintf(stderr, __VA_ARGS__);                                   \
134       fflush(stderr);                                                 \
135       rc = e_code;                                                    \
136       goto Error;                                                     \
137     }                                                                 \
138   } while(0)
139
140 /* print a message and return E_CODE */
141 #define G_M_EXIT(e_code, ... )                             \
142   do {                                                     \
143     fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__);      \
144     fprintf(stderr, __VA_ARGS__ );                         \
145     fflush(stderr);                                        \
146     rc = e_code;                                           \
147     goto Error;                                            \
148   } while(0)
149
150 /*
151  * And when you only want to print a message
152  */
153 #define P_CHK(a)                                                      \
154   do {                                                                \
155     if( (a) != SUCCESS ) {                                            \
156       fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
157       fflush(stderr);                                                 \
158     }                                                                 \
159   } while (0)
160
161 /* AST() fails if (a != 0) */
162 #define P_AST(a)                                                      \
163   do {                                                                \
164     if( (a) == FALSE ) {                                              \
165       fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
166       fflush(stderr);                                                 \
167     }                                                                 \
168   } while(0)
169
170 #endif /* _ERR_MAC_H */