|
| 1 | +/* |
| 2 | + macros.h |
| 3 | +
|
| 4 | + Based on util_macros originally written by |
| 5 | +
|
| 6 | + Louis Ziantz |
| 7 | + Jim Teresco |
| 8 | +
|
| 9 | + Rensselaer Polytechnic Institute |
| 10 | + Department of Computer Science |
| 11 | + Scientific Computation Research Center |
| 12 | +
|
| 13 | + Macros which are generally useful. |
| 14 | +
|
| 15 | + Created: |
| 16 | + Wed Nov 29 17:05:11 EST 1995 |
| 17 | +
|
| 18 | + Last change: |
| 19 | + Mon Jan 22 13:02:18 EST 1996 |
| 20 | +
|
| 21 | + $Id$ |
| 22 | +
|
| 23 | + Modification History |
| 24 | + 07/16/1998 Added in FIELD_SET and OUTPUT_IN_ORDER from pmdb |
| 25 | +*/ |
| 26 | + |
| 27 | +#ifndef __H_MACROS |
| 28 | +#define __H_MACROS |
| 29 | + |
| 30 | +#include <stdlib.h> |
| 31 | +#include <stdio.h> |
| 32 | +#include <errno.h> |
| 33 | + |
| 34 | +#ifndef FALSE |
| 35 | +#define FALSE 0 |
| 36 | +#endif |
| 37 | +#ifndef TRUE |
| 38 | +#define TRUE 1 |
| 39 | +#endif |
| 40 | + |
| 41 | +extern void com_abort(char *, char *); |
| 42 | + |
| 43 | +#define SAFE_MALLOC(v,type,size) \ |
| 44 | + { v = (type) malloc(size) ; \ |
| 45 | + if ( v == NULL) { \ |
| 46 | + fflush(stdout); \ |
| 47 | + fprintf(stderr,"in file %s, line %d, failed to allocate %ld bytes",\ |
| 48 | + __FILE__,__LINE__,size); \ |
| 49 | + com_abort(NULL,NULL); \ |
| 50 | + } \ |
| 51 | + } |
| 52 | + |
| 53 | +#define SAFE_REALLOC(v,type,size) \ |
| 54 | + { v = (type) realloc(v,size) ; \ |
| 55 | + if ( v == NULL) { \ |
| 56 | + fflush(stdout); \ |
| 57 | + fprintf(stderr,"in file %s, line %d, failed to reallocate %ld bytes",\ |
| 58 | + __FILE__,__LINE__,size); \ |
| 59 | + com_abort(NULL,NULL); \ |
| 60 | + } \ |
| 61 | + } |
| 62 | + |
| 63 | +#define GRACEFULLY_OPEN(fp, filename, mode) { \ |
| 64 | + fp = fopen(filename,mode) ; \ |
| 65 | + if ( fp == NULL ) { \ |
| 66 | + char msg[FILENAME_MAX+25]; \ |
| 67 | + sprintf(msg, "Could not open file %s.\n", filename); \ |
| 68 | + perror(" fopen"); \ |
| 69 | + com_abort("GRACEFULLY_OPEN", msg); \ |
| 70 | + } \ |
| 71 | +} |
| 72 | + |
| 73 | +#define FILE_EXISTS(fp,filename) \ |
| 74 | + ( ((fp = fopen(filename,"r")) != NULL) \ |
| 75 | + ? (! fclose(fp)) /* TRUE */ : FALSE) |
| 76 | + |
| 77 | +/* |
| 78 | + * Macro for checking return from fprintf |
| 79 | + * |
| 80 | + * Should only be used if routine doesn't need to clean up before |
| 81 | + * returning. Calling routine should decare "int ret=0;" and do a |
| 82 | + * "return(ret)" at the end. This should not be used in main, as it |
| 83 | + * will result in the program terminating and returning 0 on failure. |
| 84 | + * |
| 85 | + */ |
| 86 | + |
| 87 | +#define SAFE_FPRINTF if (ret==EOF) return(0); else ret=fprintf |
| 88 | + |
| 89 | +/* |
| 90 | + * Macro for checking return from fclose |
| 91 | + * |
| 92 | + * Should only be used if routine doesn't need to clean up before |
| 93 | + * returning. Calling routine must return an int, 0 for failure. |
| 94 | + * It is the caller's repsonsibility to abort if necessary. |
| 95 | + * |
| 96 | + */ |
| 97 | + |
| 98 | + |
| 99 | +#define SAFE_FCLOSE(fp, fname) \ |
| 100 | + if (fclose(fp)) { \ |
| 101 | + fprintf(stderr,"error closing file %s\n",fname); \ |
| 102 | + perror(" fclose"); \ |
| 103 | + return(0); \ |
| 104 | + } |
| 105 | + |
| 106 | +#define GRACEFULLY_CLOSE(fp, fname) \ |
| 107 | + if (fclose(fp)) { \ |
| 108 | + char msg[FILENAME_MAX+25]; \ |
| 109 | + sprintf(msg,"Error closing file %s\n",fname); \ |
| 110 | + perror(" fclose"); \ |
| 111 | + com_abort("GRACEFULLY_CLOSE", msg); \ |
| 112 | + } |
| 113 | + |
| 114 | +#define ASSERT(cond) \ |
| 115 | + if (!(cond)) { \ |
| 116 | + fflush(stdout); \ |
| 117 | + fprintf(stderr,"ASSERT failed in file %s at line %d\n", \ |
| 118 | + __FILE__,__LINE__); \ |
| 119 | + com_abort(NULL,NULL); \ |
| 120 | + } |
| 121 | + |
| 122 | +#define FAIL \ |
| 123 | + fflush(stdout); \ |
| 124 | + fprintf(stderr,"FAIL in file %s at line %d\n", \ |
| 125 | + __FILE__,__LINE__); \ |
| 126 | + com_abort(NULL,NULL) |
| 127 | + |
| 128 | +/* do not add anything below this line! */ |
| 129 | +#endif /* __H_MACROS */ |
0 commit comments