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