home *** CD-ROM | disk | FTP | other *** search
/ vis-ftp.cs.umass.edu / vis-ftp.cs.umass.edu.tar / vis-ftp.cs.umass.edu / pub / Software / ASCENDER / ascender.tar.Z / ascender.tar / Triangulate / include / rh_util.h < prev    next >
C/C++ Source or Header  |  1995-04-13  |  5KB  |  205 lines

  1. /* @(#)rh_util.h    3.15 12/21/94 */
  2.  
  3. /*
  4.  * This is a general utilities macro file, used for both c and c++ 
  5.  * applications.
  6.  */
  7.  
  8. #ifndef _standard_rih_utils_file
  9. #define _standard_rih_utils_file
  10.  
  11. #ifdef __cplusplus
  12.  
  13. //---------------------------------------------------------------------
  14. //
  15. //    This is the C++ version.
  16. //
  17. //---------------------------------------------------------------------
  18.  
  19. #include <stdio.h>
  20. #include "bail_out.s"
  21. typedef short BOOLEAN;
  22. #define _BOOLEAN_DEFINED_
  23.  
  24. /* External declarations */
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <alloca.h>
  28. #include <malloc.h>
  29. #include <math.h>
  30.  
  31. /* Maximum and minimum */
  32.  
  33. #ifndef DEFINED_MAX_MIN
  34.  
  35. #   define TRUE    1
  36. #   define FALSE   0
  37.  
  38. #ifndef MAXINT
  39.    const int MAXINT  = 2147483647;
  40. #endif
  41.  
  42. #ifndef maxint
  43.    const int maxint  = 2147483647;
  44. #endif
  45.  
  46. inline int    rhMax(int a, int b)     { return (a>b ? a : b); }
  47. inline double rhMax(double a, double b) { return (a>b ? a : b); }
  48. inline int    rhMin(int a, int b)     { return (a>b ? b : a); }
  49. inline double rhMin(double a, double b) { return (a>b ? b : a); }
  50. inline int    rhAbs (int a)         { return (a>0 ? a : -a); }
  51. inline double rhAbs (double a)         { return (a>0.0 ? a : -a); }
  52.  
  53. #endif
  54.  
  55. // Allocation stuff
  56.  
  57. inline void ABORT () 
  58.    { 
  59.    fprintf(stderr,"ERROR ***Out of space.***\n"); 
  60.    bail_out(1); 
  61.    }
  62.  
  63. inline char *MALLOC(size_t len)  
  64.    {
  65.    register char *zztchar = new char[len];
  66.    // register char *zztchar = (char *)malloc(len);
  67.    if (zztchar == (char *)0) ABORT();
  68.    return zztchar;
  69.    }
  70.  
  71. #define REALLOC(arr,arr_size) \
  72.    if ((arr_size *= 2, arr = realloc(arr,arr_size*sizeof(*arr)))== 0) \
  73.       ABORT()
  74.  
  75. #define TESTALLOC(arr,arr_size,arr_count) \
  76.    if(arr_count>=arr_size) REALLOC(arr,arr_size)
  77.  
  78. #define NEW(p) ((p *)MALLOC(sizeof(p)))
  79.  
  80. //
  81. // String routines.
  82. //
  83.  
  84. inline char * COPY(char *str)
  85.    {
  86.    return (str ? strcpy(MALLOC(strlen(str)+1),str) : (char *)0);
  87.    }
  88.  
  89. /* Macro for opening files */
  90. #undef FOPEN
  91. inline FILE *FOPEN (char *fname, char *amode)
  92.    {
  93.    register FILE *zztFILE=fopen((fname),(amode));
  94.    if (zztFILE ==0)
  95.       {
  96.       fprintf (stderr, "Unable to open file %s for %s.\n", 
  97.     (fname),    
  98.         ((amode)[0]=='r')                         
  99.        ? "read"                         
  100.        :((amode)[0]=='w'||(amode)[0]=='a' ?"write":"(bad mode)")
  101.     );
  102.  
  103.       perror (fname);                        
  104.       bail_out(1);
  105.       }
  106.  
  107.    return zztFILE;
  108.    }
  109.  
  110. #else
  111.  
  112. /*---------------------------------------------------------------------
  113. //
  114. //    This is the standard C version.
  115. //
  116. //---------------------------------------------------------------------*/
  117.  
  118. #include <stdio.h>
  119. #define TRUE    1
  120. #define FALSE   0
  121. typedef short BOOLEAN;
  122. #define _BOOLEAN_DEFINED_
  123.  
  124. #ifndef MAXINT
  125. #   define MAXINT 2147483647
  126. #endif
  127.  
  128. #ifndef maxint
  129. #   define maxint 2147483647
  130. #endif
  131.  
  132. /* External declarations */
  133. #include <string.h>
  134. #include <malloc.h>
  135. #include <math.h>
  136. #include "bail_out.s"
  137. extern char *mktemp();
  138.  
  139. /* Temporary variables used in macros */
  140. static char *zztchar;
  141. static FILE *zztFILE;
  142.  
  143. /* Maximum and minimum */
  144.  
  145. #define rhMax(a,b) ((a)>(b) ? (a) : (b))
  146. #define rhMin(a,b) ((a)>(b) ? (b) : (a))
  147. #define rhAbs(a)   ((a)>0 ? (a) : -(a))
  148. #define PI 3.141592654
  149.  
  150. /* Macro nefinitions */
  151. #define MALLOC(len)                          \
  152.     (((zztchar=malloc((unsigned)len))==0)?            \
  153.     (fprintf(stderr,"ERROR ***Out of space.***\n"),        \
  154.     bail_out(1),(char *)0):zztchar)
  155.  
  156. #define NEW(p) ((p *)MALLOC(sizeof(p)))
  157.  
  158. #define STRCAT(str1,str2) \
  159.     (strcat(strcpy(MALLOC(strlen(str1)+strlen(str2)+1),str1),str2))
  160.  
  161. #define COPY(str) (str?strcpy(MALLOC(strlen(str)+1),str):0)
  162.  
  163. #define ABORT {fprintf(stderr,"ERROR ***Out of space.***\n"); bail_out(1);}
  164.  
  165. #define REALLOC(arr,arr_size)                         \
  166.     if ((arr_size *= 2, arr = realloc(arr,arr_size*sizeof(*arr)))== 0) \
  167.        ABORT
  168.  
  169. #if 0
  170. /* No longer in use, and causes problems with definition of calloc */
  171. extern char *calloc();
  172. #define CALLOC(arr,arr_size) \
  173.     if ((arr_size = 16, arr = calloc(arr_size, sizeof(*arr))) == 0) ABORT
  174. #endif
  175.  
  176. #define TESTALLOC(arr,arr_size,arr_count) \
  177.     if(arr_count>=arr_size) REALLOC(arr,arr_size)
  178.  
  179. /* Macro for opening files */
  180. #define FOPEN(fname,amode)                         \
  181.     ((zztFILE=fopen((fname),(amode)))==0                \
  182.     ? (FILE *)((fprintf (stderr,                     \
  183.             "Unable to open file %s for %s.\n", (fname),    \
  184.        ((amode)[0]=='r')                         \
  185.         ?"read"                         \
  186.         :((amode)[0]=='w'||(amode)[0]=='a' ?"write":"(bad mode)")),\
  187.           perror (fname),                        \
  188.       bail_out(1)))                            \
  189.     : zztFILE)
  190.     
  191. #endif
  192.  
  193. /*---------------------------------------------------------------------
  194. //
  195. //    Common to both
  196. //
  197. //---------------------------------------------------------------------*/
  198.  
  199. /* Include allocation routines as well */
  200. #include "alloc.h"
  201. #include "concat.s"
  202.     
  203.  
  204. #endif
  205.