home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume44 / va.h / part01 / Va.h
Encoding:
C/C++ Source or Header  |  1994-09-05  |  3.2 KB  |  115 lines

  1. /**
  2.     <Va.h> -- MUVES "Va" (variable argument) package definitions
  3.  
  4. **/
  5. /*
  6.     created:    94/08/16    D A Gwyn
  7.     last edit:    94/08/19    D A Gwyn
  8.     SCCS ID:    @(#)Va.h    1.1
  9.  */
  10. /**
  11.  
  12.     The Va package provides portable support for functions taking
  13.     a variable number of arguments.  <Va.h> defines several macros
  14.     that work together to hide the differences between the old
  15.     UNIX <varargs.h> facility and the new Standard C <stdarg.h>.
  16.  
  17.     Rather than describing each Va package macro separately, it is
  18.     best to give an example of the proper usage of the whole set of
  19.     macros.  It should be easy to adapt this generic example to any
  20.     specific requirement.
  21.  
  22.     The example is for a function named Advise that has two
  23.     required arguments, the first being a printf-like format string
  24.     and the second a flag that indicates (when true) that an extra
  25.     "verbosity level" is provided as the third argument.  Remaining
  26.     arguments are those, if any, associated with the format string.
  27.     The Advise function prints the formatted result on the standard
  28.     error output if and only if the verbosity level is given and is
  29.     greater than 0.  It returns true unless it had trouble printing
  30.     on the standard error output stream.
  31.  
  32.     Any code that wants to invoke the Advise function must include
  33.     a proper declaration for it:
  34.  
  35.     #include    <Va.h>
  36.     extern bool    Advise( VaT(const char *) VaT(bool) VaDots );
  37.  
  38.     The implementation of the Advise function might be:
  39.  
  40.     #include    <stdio.h>
  41.     #include    <std.h>
  42.     #include    <Va.h>
  43.  
  44.     // VARARGS                // not VARARGS2
  45.     bool
  46.     Advise( VaT( const char *format ) VaT( bool verbose ) VaAList )
  47.         VaDcl
  48.         {
  49.         VaD( char    *format )    // no "const" here
  50.         VaD( bool    verbose )
  51.         VaList(        ap )
  52.         register int    verbosity;
  53.         register bool    status;
  54.  
  55.         VaStart( ap, verbose )
  56.         VaI( ap, char *, format )    // no "const" here
  57.         VaI( ap, bool, verbose )
  58.  
  59.         if ( verbose )
  60.             verbosity = VaArg( ap, int );
  61.         else
  62.             verbosity = 0;
  63.  
  64.         if ( verbosity > 0 )
  65.             status = vfprintf( stderr, format, ap ) > 0;
  66.         else
  67.             status = true;
  68.  
  69.         VaEnd( ap )
  70.         return status;
  71.         }
  72.  
  73.     Note that several of these macros are reminiscent of the va_*
  74.     macros in <varargs.h> or <stdarg.h>, but there are significant
  75.     differences.  Proper usage of the "function-like" macros, in
  76.     particular, does not require semicolons; this is intentional,
  77.     in order to avoid warnings about "null statements" from certain
  78.     compilers and "lint".  It is suggested that the best way to use
  79.     this package is to copy the above example and then make changes
  80.     to the copy that are necessary for the specific application.
  81. **/
  82.  
  83. #if    __STDC__
  84.  
  85. #include    <stdarg.h>
  86.  
  87. #define    VaT( t )        t,
  88. #define    VaDots            ...
  89. #define    VaAList            ...
  90. #define    VaD( d )        /* nothing */
  91. #define    VaDcl            /* nothing */
  92. #define    VaList( ap )        va_list ap;
  93. #define    VaStart( ap, A0 )    va_start( ap,A0 );
  94. #define    VaI( ap, T, Ai )    /* nothing */
  95. #define    VaArg( ap, T )        va_arg( ap, T )
  96. #define    VaEnd( ap )        va_end( ap );
  97.  
  98. #else    /* "classic" version of UNIX assumed */
  99.  
  100. #include    <varargs.h>
  101.  
  102. #define    VaT( t )        /* nothing */
  103. #define    VaDots            /* nothing */
  104. #define    VaAList            va_alist
  105. #define    VaD( d )        d;
  106. #define    VaDcl            va_dcl
  107. #define    VaList( ap )        va_list ap;
  108. #define    VaStart( ap, A0 )    va_start( ap );
  109. #define    VaI( ap, T, Ai )    Ai = va_arg( ap, T );
  110. #define    VaArg( ap, T )        va_arg( ap, T )
  111. #define    VaEnd( ap )        va_end( ap );
  112.  
  113. #endif
  114.  
  115.