home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / stdwin / H / tools.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-07  |  3.4 KB  |  171 lines  |  [TEXT/????]

  1. /* Useful includes, definitions etc. */
  2.  
  3. #include "stdwconf.h"    /* Figure out on which system we are */
  4. #include "_ARGS.h"    /* Define _ARGS() macro appropriately */
  5.  
  6.  
  7. /****************************/
  8. /* Auto-configuration tests */
  9. /****************************/
  10.  
  11. #ifdef __STDC__
  12. #define VOID_PTR
  13. #define HAVE_STDLIB
  14. #endif
  15.  
  16. #ifdef THINK_C
  17. #ifdef THINK_C_3_0
  18. #define NO_STRING_H
  19. #else
  20. #define HAVE_STDLIB
  21. #endif
  22. #endif
  23.  
  24. #ifdef THINK_C
  25. #define VOID_PTR
  26. #endif
  27.  
  28. #ifdef SYSV
  29. #define VOID_PTR
  30. #endif
  31.  
  32. #ifdef AMOEBA
  33. /* Amoeba has its own way of telling which one we want: */
  34. #ifndef VOIDSTAR
  35. #define VOID_PTR
  36. #endif
  37. #endif
  38.  
  39.  
  40. /*********************/
  41. /* Universal pointer */
  42. /*********************/
  43.  
  44. #ifdef VOID_PTR
  45. #define UNIVPTR void *
  46. #else
  47. #define UNIVPTR char *
  48. #endif
  49.  
  50.  
  51. /*************/
  52. /* C library */
  53. /*************/
  54.  
  55. #include <stdio.h>
  56. #include <ctype.h>
  57. #include <errno.h>
  58.  
  59. #ifdef NO_STRING_H
  60. /* Systems that don't have <string.h> should at least have <strings.h> */
  61. #include <strings.h>
  62. #else
  63. #include <string.h>
  64. #endif
  65.  
  66. #ifdef NO_STRCHR
  67. /* Systems that don't have str(r)chr should at least have (r)index */
  68. #define strchr index
  69. #define strrchr rindex
  70. #endif
  71.  
  72. #ifdef NO_MEMCPY
  73. /* Systems that don't have memcpy/memcmp should at least have bcopy/bcmp */
  74. #define memcpy(dest, src, n)    bcopy(src, dest, n)
  75. #define memcmp(a, b, cnt)    bcmp(a, b, cnt)
  76. #endif
  77.  
  78. #ifdef THINK_C
  79. #ifdef THINK_C_3_0
  80. #include <proto.h>
  81. #endif
  82. #endif
  83.  
  84.  
  85. #ifdef HAVE_STDLIB
  86.  
  87. #include <stdlib.h>
  88.  
  89. #else
  90.  
  91. #include <sys/types.h> /* For size_t -- hope it doesn't break other things */
  92.  
  93. UNIVPTR malloc _ARGS((size_t));
  94. UNIVPTR calloc _ARGS((size_t, size_t));
  95. UNIVPTR realloc _ARGS((UNIVPTR, size_t));
  96.  
  97. #ifndef NO_VOID_FREE
  98. void free _ARGS((UNIVPTR));
  99. #endif
  100.  
  101. #ifndef NO_VOID_EXIT
  102. void exit _ARGS((int));
  103. #endif
  104.  
  105. char *getenv _ARGS((char *));
  106.  
  107. #endif /* !HAVE_STDLIB */
  108.  
  109. /* According to the C Standard, errno may be a macro on systems with
  110.    multiple threads.  But on older systems, it may not be declared at
  111.    all in <errno.h>.  So we declare it here, except if it is a macro. */
  112.  
  113. #ifndef errno
  114. extern int errno;
  115. #endif
  116.  
  117.  
  118. /*************************************/
  119. /* Miscellaneous useful declarations */
  120. /*************************************/
  121.  
  122. /* Interface to getopt(): */
  123. extern int optind;
  124. extern char *optarg;
  125. int getopt _ARGS((int, char **, char *));
  126.  
  127. /* Boolean data type: */
  128. #define bool int    /* For single variable, argument or return value */
  129. #define tbool char    /* Tiny bool, used in structs or arrays */
  130. #define FALSE 0
  131. #define TRUE 1
  132.  
  133. /* Character shorthands: */
  134. #define EOS '\0'
  135. #define EOL '\n'
  136.  
  137. /* Copy string to malloc'ed memory: */
  138. char *strdup _ARGS((const char *));
  139.  
  140. /* Other useful macros: */
  141.  
  142. #define CNTRL(x) ((x) & 0x1f) /* Usage: CNTRL('X') */
  143.  
  144. #define ABS(x) ((x) < 0 ? -(x) : (x))
  145.  
  146. #ifndef MIN
  147. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  148. #endif
  149. #ifndef MAX
  150. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  151. #endif
  152.  
  153. #define CLIPMIN(var, min) if ((var) >= (min)) ; else (var)= (min)
  154. #define CLIPMAX(var, max) if ((var) <= (max)) ; else (var)= (max)
  155.  
  156. /* Memory allocation macros: */
  157.  
  158. #define ALLOC(type) ((type*) malloc(sizeof(type)))
  159. #define FREE(p) if ((p) != 0) { free((UNIVPTR)p); p = 0; } else /*empty*/
  160.  
  161. /* Array (re)allocation macros.
  162.    RESIZE yields nonzero if the realloc succeeded. */
  163.  
  164. #define NALLOC(type, n) ((type*) malloc((unsigned)(n) * sizeof(type)))
  165. #define RESIZE(var, type, n) \
  166.     (var= (type *) realloc((UNIVPTR)var, (n) * sizeof(type)))
  167.  
  168. /* Dynamic array macros: */
  169.  
  170. #include "lists.h"
  171.