Added GPL blurbs to err_mac.h and stripchart.py.
[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 #include <stdio.h> /* for fprintf(), stderr */
27
28 #define SUCCESS 0
29 #define FAILURE 1
30
31 #ifndef TRUE
32 #define TRUE 1
33 #endif
34 #ifndef FALSE
35 #define FALSE 0
36 #endif
37 static int err_mac_ans;
38
39 /*
40  * For functions returning int, when you want an immediate return.
41  */
42
43 /* CHK() fails if (a != SUCCESS) */
44 #define CHK(a)                                                        \
45   do {                                                                \
46     if( (err_mac_ans = (a)) != SUCCESS ) {                            \
47       fprintf(stderr,"FAILURE: %s.%d. (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
48       fflush(stderr);                                                 \
49       return err_mac_ans;                                             \
50     }                                                                 \
51   } while (0)
52
53 /* AST() fails if (a != 0) */
54 #define AST(a)                                                        \
55   do {                                                                \
56     if( (err_mac_ans = (a)) == FALSE ) {                              \
57       fprintf(stderr, "ASSERT FAILED: %s.%d (%s) returned %d\n",__FILE__,__LINE__, #a, err_mac_ans); \
58       fflush(stderr);                                                 \
59       return FAILURE;                                                 \
60     }                                                                 \
61   } while(0)
62
63 /* like AST(), but return E_CODE instead of the default FAILURE */
64 #define E_AST(a,e_code)                                               \
65   do {                                                                \
66     if((a) == FALSE) {                                                \
67       fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
68       fflush(stderr);                                                 \
69       return (e_code);                                                \
70     }                                                                 \
71   } while(0)
72
73 /* like E_AST(), but also fprintfs __VA_ARGS__ */
74 #define M_AST(a,e_code,...)                                           \
75   do {                                                                \
76     if((a) == FALSE) {                                                \
77       fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
78       fprintf(stderr, __VA_ARGS__);                                   \
79       fflush(stderr);                                                 \
80       return (e_code);                                                \
81     }                                                                 \
82   } while(0)
83
84 /* print a message and return E_CODE */
85 #define M_EXIT(e_code, ... )                               \
86   do {                                                     \
87     fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__);      \
88     fprintf(stderr, __VA_ARGS__ );                         \
89     fflush(stderr);                                        \
90     return (e_code);                                       \
91   } while(0)
92
93 /*
94  * When you don't want an immedate return, but would rather 'goto Error;'.
95  * These set the local variable RC with the return value before jumping.
96  */
97
98 #define G_CHK(a)                                                      \
99   do {                                                                \
100     if( (rc = (a)) != SUCCESS ) {                                     \
101       fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
102       fflush(stderr);                                                 \
103       goto Error;                                                     \
104     }                                                                 \
105   } while (0)
106
107 #define G_AST(a)                                                      \
108   do {                                                                \
109     if( (rc = (a)) == FALSE ) {                                       \
110       fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
111       fflush(stderr);                                                 \
112       goto Error;                                                     \
113     }                                                                 \
114   } while(0)
115
116 #define G_E_AST(a,e_code)                                             \
117   do {                                                                \
118     if((a) == FALSE) {                                                \
119       fprintf(stderr,"ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
120       fflush(stderr);                                                 \
121       rc = e_code;                                                    \
122       goto Error;                                                     \
123     }                                                                 \
124   } while(0)
125
126 #define G_M_AST(a,e_code,...)                                         \
127   do {                                                                \
128     if((a) == FALSE) {                                                \
129       fprintf(stderr,"ASSERT FAILED: %s.%d (%s) ",__FILE__,__LINE__, #a); \
130       fprintf(stderr, __VA_ARGS__);                                   \
131       fflush(stderr);                                                 \
132       rc = e_code;                                                    \
133       goto Error;                                                     \
134     }                                                                 \
135   } while(0)
136
137 /* print a message and return E_CODE */
138 #define G_M_EXIT(e_code, ... )                             \
139   do {                                                     \
140     fprintf(stderr,"EXIT: %s.%d ",__FILE__,__LINE__);      \
141     fprintf(stderr, __VA_ARGS__ );                         \
142     fflush(stderr);                                        \
143     rc = e_code;                                           \
144     goto Error;                                            \
145   } while(0)
146
147 /*
148  * And when you only want to print a message
149  */
150 #define P_CHK(a)                                                      \
151   do {                                                                \
152     if( (a) != SUCCESS ) {                                            \
153       fprintf(stderr,"FAILURE: %s.%d. (%s)\n",__FILE__,__LINE__, #a); \
154       fflush(stderr);                                                 \
155     }                                                                 \
156   } while (0)
157
158 /* AST() fails if (a != 0) */
159 #define P_AST(a)                                                      \
160   do {                                                                \
161     if( (a) == FALSE ) {                                              \
162       fprintf(stderr, "ASSERT FAILED: %s.%d (%s)\n",__FILE__,__LINE__, #a); \
163       fflush(stderr);                                                 \
164     }                                                                 \
165   } while(0)