MED fichier
med_utils.h
Aller à la documentation de ce fichier.
1 /* -*- mode:C; coding:utf-8 -*- */
2 /* This file is part of MED.
3  *
4  * COPYRIGHT (C) 1999 - 2021 EDF R&D, CEA/DEN
5  * MED is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * MED is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with MED. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 
20 # ifndef __UTILITES_H__
21 # define __UTILITES_H__
22 
23 #include <stdlib.h>
24 #include <med_exit_if.h>
25 #include <med_err.h>
26 
27 /* pour indiquer le statut des arguments des fonctions. */
28 
29 #ifdef _IN
30 #error _IN already defined
31 #endif
32 #define _IN
33 
34 #ifdef _OUT
35 #error _OUT already defined
36 #endif
37 #define _OUT
38 
39 #ifdef _INOUT
40 #error _INOUT already defined
41 #endif
42 #define _INOUT
43 
44 #ifdef _UNUSED
45 #error _UNUSED already defined
46 #endif
47 #define _UNUSED
48 
49 
50 
51 
52 
53 
54 
55 /* --- Pour afficher le nom du fichier source courant et le numero de la ligne courante --- */
56 /* --- sur la stderr. --- */
57 
58 # define ICI {\
59  fflush(stdout);\
60  fprintf(stderr, "%s [%d] : " , __FILE__ , __LINE__ ) ;\
61  fflush(stderr) ;\
62  }
63 
64 
65 
66 
67 
68 
69 
70 /* --- Pour afficher la date et l'heure de la compilation du fichier source courant, --- */
71 /* --- sur la stdout. --- */
72 
73 # ifdef INFOS_COMPILATION
74 # error INFOS_COMPILATION already defined
75 # endif
76 # define INFOS_COMPILATION {\
77  fflush(stderr);\
78  fprintf(stdout, "%s [%d] : " , __FILE__ , __LINE__ ) ;\
79  fprintf(stdout,"Compilation le %s" , __DATE__);\
80  fprintf(stdout," a %s" , __TIME__ );\
81  fprintf(stdout,"\n\n\n" );\
82  fflush(stdout) ;\
83  }
84 
85 
86 
87 
88 
89 
90 
91 /* --- Pour attendre "secondes" secondes et afficher un message sur la stderr indiquant --- */
92 /* --- cette attente volontaire. --- */
93 
94 # ifdef ATTENTE
95 # error ATTENTE already defined
96 # endif
97 # define ATTENTE(secondes) {\
98  ICI ;\
99  fprintf( stderr, "ATTENTE de %d secondes" , secondes);\
100  fflush(stderr) ;\
101  sleep(secondes) ;\
102  fprintf( stderr, "\n" );\
103  fflush(stderr) ;\
104  }
105 
106 
107 
108 
109 
110 
111 
112 /* ---------- Les macros suivantes ne doivent pas deja exister ! ---------- */
113 
114 # ifdef EXECUTION
115 # error EXECUTION already defined
116 # endif
117 # ifdef INTERRUPTION
118 # error INTERRUPTION already defined
119 # endif
120 # ifdef ISCRUTE
121 # error ISCRUTE already defined
122 # endif
123 # ifdef RSCRUTE
124 # error RSCRUTE already defined
125 # endif
126 # ifdef SSCRUTE
127 # error SSCRUTE already defined
128 # endif
129 # ifdef CSCRUTE
130 # error CSCRUTE already defined
131 # endif
132 # ifdef XSCRUTE
133 # error XSCRUTE already defined
134 # endif
135 # ifdef MESSAGE
136 # error MESSAGE already defined
137 # endif
138 
139 
140 
141 /* D馩nition du format ?tiliser pour afficher un med_int */
142 # define IFORMAT_int "%d"
143 # define IFORMAT_long "%ld"
144 # define IFORMAT_llong "%lld"
145 # define IFORMAT IFORMAT_int
146 
150 # define MED_IFORMAT_int "d"
151 # define MED_IFORMAT_long "ld"
152 # define MED_IFORMAT_llong "lld"
153 # define MED_IFORMAT MED_IFORMAT_int
154 
159 #define MED_EXP2_(msg1,msg2) msg1 msg2
160 #define MED_ERR_(rt,r1,r2,r3) { rt = r1 r2; MESSAGE(MED_EXP2_(r1 ## _MSG,r2 ## _MSG)); MESSAGE(r3); }
161 
162 
163 #if ! defined(MESGERR)
164 #error "Explicit error messages must be either activated or deactivated, hence MESGERR macro must be defined."
165 #error "Verify that you include med_utils.h after med_config.h, thanks."
166 #endif
167 
168 # if MESGERR == 1 || defined(MED_MESGERR_FOR_TOOLS)
169 
170 
171 /* --- Pour tracer sur la stderr l'execution d"une instruction. --- */
172 
173 # define EXECUTION(instruction) {\
174  ICI ;\
175  fprintf( stderr,"INSTRUCTION %s" , #instruction ) ;\
176  fflush(stderr);\
177  instruction ;\
178  fflush(stdout);\
179  fprintf( stderr," FRANCHIE\n" ) ;\
180  fflush(stderr);\
181  }
182 
183 
184 
185 
186 
187 
188 
189 /* --- Pour afficher un message d'interruption volontaire et retourner le code retour --- */
190 /* --- "code" --- */
191 
192 # define INTERRUPTION(code) {\
193  ICI ;\
194  fprintf( stderr," INTERRUPTION code = %d",code) ;\
195  fprintf(stderr,"\n") ;\
196  exit(code) ;\
197  }
198 
199 
200 
201 
202 
203 
204 
205 /* --- Pour conditionner la poursuite du traitement par la validite de la condition --- */
206 /* --- "condition". --- */
207 
208 # ifndef ASSERT
209 # define ASSERT(condition) if( !(condition) ){\
210  ICI ;\
211  fprintf(stderr,"condition %s VIOLEE\n",#condition);\
212  INTERRUPTION(17);\
213  }
214 # endif /* # ifndef ASSERT */
215 
216 
217 /* --- Pour conditionner la poursuite du traitement au fait qu'une chaine ne finie pas par un blanc --- */
218 /* --- "nofinalblank". --- */
219 # ifndef NOFINALBLANK
220 # define NOFINALBLANK(chaine,label) if (chaine) if (strlen(chaine)) if( ( chaine[strlen(chaine)-1] == ' ' ) ){ \
221  ICI ;\
222  fprintf(stderr,"La chaine |%s| ne doit pas finir par un caract貥 blanc.\n",chaine);\
223  goto label; \
224  }
225 # endif /* # ifndef NOFINALBLANK */
226 
227 
228 
229 
230 
231 
232 /* --- Pour afficher sur la stderr la valeur d'une variable precedee de son nom. --- */
233 
234 # define ISCRUTE(entier) ISCRUTE_int(entier)
235 
236 # define ISCRUTE_int(entier) {\
237  ICI ;\
238  fprintf(stderr,"%s = %d\n",#entier,(int) entier) ; \
239  fflush(stderr) ;\
240  }
241 # define ISCRUTE_int8(entier) {\
242  ICI ;\
243  fprintf(stderr,"%s = %hd\n",#entier,entier) ;\
244  fflush(stderr) ;\
245  }
246 # define ISCRUTE_long(entier) {\
247  ICI ;\
248  fprintf(stderr,"%s = %ld\n",#entier,(long) entier) ; \
249  fflush(stderr) ;\
250  }
251 # define ISCRUTE_llong(entier) {\
252  ICI ;\
253  fprintf(stderr,"%s = %lld\n",#entier,(long long) entier) ; \
254  fflush(stderr) ;\
255  }
256 # define ISCRUTE_size(entier) {\
257  ICI ;\
258  fprintf(stderr,"%s = %llu\n",#entier,entier) ;\
259  fflush(stderr) ;\
260  }
261 # define ISCRUTE_id(entier) ISCRUTE_long(entier)
262 # define RSCRUTE(reel) {\
263  ICI ;\
264  fprintf(stderr,"%s = %f\n",#reel,reel) ;\
265  fflush(stderr) ;\
266  }
267 # define XSCRUTE(pointeur) {\
268  ICI ;\
269  fprintf(stderr,"%s = %p\n",#pointeur,pointeur) ;\
270  fflush(stderr) ;\
271  }
272 # define CSCRUTE(car) {\
273  ICI ;\
274  fprintf(stderr,"%s = %c\n",#car,car) ;\
275  fflush(stderr) ;\
276  }
277 # define SSCRUTE(chaine) {\
278  ICI ;\
279  fprintf(stderr,"%s = \"%s\"\n",#chaine,chaine) ;\
280  fflush(stderr) ;\
281  }
282 # define MESSAGE(chaine) {\
283  ICI ;\
284  fprintf(stderr,"%s\n",chaine) ;\
285  fflush(stderr) ;\
286  }
287 # define FIN(nom) {\
288  ICI ;\
289  fprintf( stderr , "} FIN %s\n\n\n" , nom ) ;\
290  fflush(stderr) ;\
291  }
292 # define DEBUT(nom) {\
293  fprintf( stderr , "\n\n\n") ;\
294  ICI ;\
295  fprintf( stderr , "{ DEBUT %s\n" , nom ) ;\
296  fflush(stderr) ;\
297  }
298 
299 
300 # else /* # if MESGERR != 1*/
301 
302 
303 
304 # define EXECUTION(instruction) instruction
305 # define INTERRUPTION(code)
306 
307 # ifndef ASSERT
308 # define ASSERT(condition)
309 # endif
310 
311 # define NOFINALBLANK(chaine,label)
312 
313 # define ISCRUTE(entier)
314 # define ISCRUTE_int(entier)
315 # define ISCRUTE_int8(entier)
316 # define ISCRUTE_long(entier)
317 # define ISCRUTE_llong(entier)
318 # define ISCRUTE_size(entier)
319 # define ISCRUTE_id(entier)
320 # define XSCRUTE(pointeur)
321 # define RSCRUTE(reel)
322 # define CSCRUTE(car)
323 # define SSCRUTE(chaine)
324 # define MESSAGE(chaine)
325 # define DEBUT(nom)
326 # define FIN(nom)
327 
328 # endif /* # if MESGERR */
329 
330 /* Ces macros ne doivent être utilisées que dans les outils ou les tests.
331  Elles ne doivent pas être utilsées dans la bibliothèque (pour respecter l'option --disable-mesgerr).
332 */
333 
334 # ifdef WITH_EXCEPTIONS
335 # ifdef __cplusplus
336 # define EXIT_IF(expression,message,arg) {\
337  if ( ( expression ) != 0) \
338  throw MEDerreur(__FILE__,__LINE__,message,arg); }
339 # else
340 # error Impossible d'activer l'option WITH_EXCEPTIONS avec un compilateur C
341 # endif
342 # else
343 # define EXIT_IF(expression,message,arg) { exit_if(__FILE__,__LINE__,expression,message,arg); }
344 # endif
345 
346 
347 # ifdef WITH_EXCEPTIONS
348 # ifdef __cplusplus
349 # define MED_ERR_EXIT_IF(expression,r1,r2,arg) {\
350  if ( ( expression ) != 0) \
351  throw MEDerreur(__FILE__,__LINE__,MED_EXP2_(r1 ## _MSG,r2 ## _MSG),arg); }
352 # else
353 # error Impossible d'activer l'option WITH_EXCEPTIONS avec un compilateur C
354 # endif
355 # else
356 # define MED_ERR_EXIT_IF(expression,r1,r2,arg) { exit_if(__FILE__,__LINE__,expression,MED_EXP2_(r1 ## _MSG,r2 ## _MSG),arg); }
357 # endif
358 
359 
360 # endif /* # ifndef __UTILITES_H__ */
med_err.h
med_exit_if.h