home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / MAILBOX.ZIP / MPMISC.H < prev    next >
Encoding:
C/C++ Source or Header  |  1988-06-27  |  7.4 KB  |  192 lines

  1. /****************************************************************************/
  2. /* mpmisc.h -- Mike Parker's MISCELLANEOUS HANDY ``C'' EXTENSIONS        */
  3. /* Created:  8/1/87        Release:  0.7        Version:  06/27/88  */
  4. /****************************************************************************
  5. (c) Copyright 1987, 1988 by Michael Benjamin Parker     (USA SS# 557-49-4130)
  6.  
  7. All Rights Reserved unless specified in the following include files: */
  8. #include "mptsk.cpy" /*
  9.  
  10. DO NOT REMOVE OR ALTER THIS NOTICE AND ITS PROVISIONS.
  11. ****************************************************************************/
  12. /* OVERVIEW:
  13.  
  14. This file contains a miscellaneous bunch of extensions to the ``C'' language,
  15. mostly macros and typedefs.
  16.  
  17. Most of them are declarations for portability of code and data structures.
  18.  
  19. Some, as cast and fld2str, provide important functional extensions to
  20. the ``C'' language.
  21.  
  22. See below for more details... */
  23. /****************************************************************************/
  24. #ifndef MPMISC_H
  25. #define MPMISC_H
  26. /****************************************************************************/
  27. /****************************************************************************/
  28. /* FOR FORMING PROTOTYPES FOR EXTERNAL FUNCTIONS WITH/WITHOUT ANSCI C */
  29. #ifdef    ANSI_C
  30. #define    ARGS(p)    p
  31. #define    VOIDPTR    void PTR
  32. #else
  33. #define    ARGS(p)    ()
  34. #define    VOIDPTR    char PTR
  35. #endif
  36. /****************************************************************************/
  37. /* for(ever) {LOOPS}                                */
  38. #define    EVER    ;;
  39. #define    ever    ;;
  40. /* unil(COND) {LOOPS} */
  41. #define    until(COND)    while(COND)
  42. /****************************************************************************/
  43. /****************************************************************************/
  44. /* CASTING OPERATOR THAT WORKS ON ANY TYPE (INCLUDING STRUCTURES)        */
  45. #define    cast(TYPE,var)    (*((TYPE *)(&var)))
  46.     /* CASTS VARIABLE var TO TYPE TYPE, EVEN IF TYPE IS A STRUCTURE */
  47. /****************************************************************************/
  48. /****************************************************************************/
  49. /* ASSIGNMENT OPERATOR WHICH RETURNS THE OLD VALUE, NOT THE NEW VALUE    */
  50. #define    assign(VAR,newval)                \
  51.     (VAR);                        \
  52.     (VAR)=    newval
  53. /* ASSIGNS VAR TO newval.  "RETURNS" THE OLD VALUE OF VAR */
  54. /****************************************************************************/
  55. /****************************************************************************/
  56. /* FIELD-POINTER / STRUCTURE-POINTER CONVERSION    OPERATORS            */
  57. #define    str2fld(strptr,FLDNAM)        \
  58.     (&((strptr)->FLDNAM))
  59.     /* RETURNS THE ADDRESS OF A FIELD IN A STRUCTURE GIVEN A POINTER TO
  60.     THE STRUCTURE AND ITS FIELD NAME */
  61.  
  62. #define    fld2str(STRTYPEID,FLDNAM,fldptr)                \
  63.     ((STRTYPEID)(((char PTR)(fldptr))                \
  64.             - ((char PTR)str2fld((STRTYPEID)0,FLDNAM))))
  65. /*
  66. #define    fld2str(STRTYPEID,FLDNAM,fldptr)                \
  67.     ((STRTYPEID)int_2ptr(    ptr_2int(fldptr) -            \
  68.                 ptr_2int(str2fld((STRTYPEID)0,FLDNAM))))
  69. */
  70.     /* RETURNS THE ADDRESS OF A STRUCTURE STRTYPEID WITH FIELD FLDNAM
  71.         AT ADDRESS fldptr. */
  72. /****************************************************************************/
  73. /****************************************************************************/
  74. /****************************************************************************/
  75. /* STACK-STORAGE ALLOCATION "DECLARATION"                    */
  76. /*    STACK CHARACTER ARRAYS WITH SIZE SPECIFIED AT RUN TIME            */
  77. #ifdef    __TURBOC__
  78. #define    stackalloc(VARIABLE,size,PGMBLOCK)        \
  79.     {                        \
  80.         unsigned    varsize=    (size); \
  81.         _SP-=    varsize;            \
  82.         {                    \
  83.             char    (VARIABLE)[0];        \
  84.             {PGMBLOCK}            \
  85.         }                    \
  86.         _SP+=    varsize;            \
  87.     }
  88. #endif    
  89. /****************************************************************************/
  90. /****************************************************************************/
  91. /* EXACT BIT WIDTH TYPES FOR DATA MANIPULATED ON MORE THAN ONE MACHINE        */
  92. typedef    unsigned char    UB08;
  93. typedef unsigned short    UB16;
  94. typedef    unsigned long    UB32;
  95. #ifdef    ANSI_C
  96. typedef    signed char    SB08;
  97. typedef signed short    SB16;
  98. typedef signed long    SB32;
  99. #else
  100. typedef        char    SB08;
  101. typedef     short    SB16;
  102. typedef     long    SB32;
  103. #endif;
  104. /****************************************************************************/
  105. /****************************************************************************/
  106. /* BOOLEAN DATA TYPE                                */
  107. #ifndef    xwindows
  108. typedef    enum {
  109.     BOOL_FALSE,
  110.     BOOL_TRUE
  111. } BOOL;
  112. #else
  113. #define    BOOL    unsigned char
  114. /* typedef    unsigned char    BOOL; */
  115. #define    BOOL_FALSE    ('\0')
  116. #define    BOOL_TRUE    ('\1')
  117. #endif
  118. /****************************************************************************/
  119. #define    bool_create(value)    ((value)?(BOOL_TRUE):(BOOL_FALSE))
  120. /****************************************************************************/
  121. /****************************************************************************/
  122. /* INTEGER TYPE -- FOR PORTABILITY & CONSISTENT POINTER/INTEGER CONVERSIONS */
  123. #define    INT    long    /* INTEGER WHICH CAN HOLD AT LEAST AS MANY
  124.                BYTES AS MACHINE CAN ADDRESS */
  125. typedef    SB32    SINT;
  126. typedef UB32    UINT;    
  127. #define    SINT_d    "ld"    /* printf TYPE */
  128. #define    UINT_d    "lu"    /* dECIMAL */
  129. #define    UINT_o    "lo"    /* oCTAL */
  130. #define    UINT_x    "lx"    /* HExIDECIMAL */
  131. #define    UINT_X    "lX"
  132. /****************************************************************************/
  133. /* INTEGER OPERATIONS                                */
  134. #define    int_min(a,b)    (((a)<(b))?(a):(b))
  135. #define    int_sign(no)    ((no)?(((no)>0)?(1):(-1)):(0))
  136.  
  137. /* RETURNS a WRAPED WITHIN b (A DECENT % (mod) FUNCTION) */
  138. #define    int_wrap(a,b)                \
  139. (                                                  \
  140.     ( ( (a) && (!((a)>=0) != !((b)>=0)) )    \
  141.         ?(b)                \
  142.         :(0)                \
  143.     )                    \
  144.     +                    \
  145.     ((a) % abs(b))                \
  146. )
  147. /* WRAPS a WITHIN b (FOR POSITIVE b ONLY!) */
  148. #define    int_wraper(a,b)                \
  149.     while ((a) < 0)        (a)+=    (b);    \
  150.     while ((a) > (b))    (a)-=    (b)    
  151. /****************************************************************************/
  152. /****************************************************************************/
  153. /* POINTER TYPE -- FOR PORTABILITY & CONSISTENT POINTER/INTEGER CONVERSIONS */
  154. #if (defined(__TURBOC__) || defined(M_I86))
  155. #define    PTR    far *
  156.  
  157. /* ANY PTR TO UNIQUE INTEGER */
  158. #define    ptr_2int(ptr)                            \
  159. ((UB32)(     (((((UB32)((void far *)(ptr))) & 0xFFFF0000) >> 12) +    \
  160.          ((UB16)((void far *)(ptr))))                \
  161.     & 0x000FFFFF) )
  162.  
  163. /* ANY INT TO UNIQUE NORMAL PTR */
  164. #define    int_2ptr(Int)                            \
  165. ((VOIDPTR)    ( ((((UB32)(Int)) & 0x000F0000) << 12) | ((UB16)(Int)) ) )
  166.  
  167. /* ANY INT TO UNIQUE MIN. OFFSET PTR
  168.      (SAME AS NORMALIZED huge POINTER IN AT LEAST TURBO C) */
  169. #define    int_2ptrl(Int)                            \
  170. ((VOIDPTR)    ( ((((UB32)(Int)) & 0x000FFFF0) << 12)             \
  171.             | (((UB16)(Int)) & 0x000F) ) )
  172.  
  173. /* ANY INT TO UNIQUE MAX. OFFSET PTR */
  174. #define    int_2ptrh(Int)                            \
  175. ((VOIDPTR)    ((((((UB32)(Int)) & 0x000FFFF0) << 12) - 0x0FFF0000)    \
  176.             | ((((UB16)(Int)) & 0x000F) | 0xFFF0) ) )
  177. /* If this looks slow & messy,...
  178.         thank Intel for their braindamaged segmented architecture! */
  179. #define    PTR_    "Fp"    /* printf TYPE */
  180. #else
  181. #define    PTR    *
  182. #define    ptr_2int(ptr)    ((UINT)        (ptr))
  183. #define    int_2ptr(Int)    ((VOIDPTR)    (Int))
  184. #define    int_2ptrl(Int)    ((VOIDPTR)    (Int))
  185. #define    int_2ptrh(Int)    ((VOIDPTR)    (Int))
  186. #define    PTR_    "ld"
  187. #endif
  188. /****************************************************************************/
  189. /****************************************************************************/
  190. /****************************************************************************/
  191. #endif    /* MPMISC_H */
  192.