home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 328_01 / wfprintf.c < prev    next >
C/C++ Source or Header  |  1991-03-17  |  3KB  |  151 lines

  1. /* wfprintf.c
  2.  *
  3.  *    contains procedure wfprintform()
  4.  *
  5.  *
  6.  *    This procedure writes the contents of a WFORM table to a file
  7.  *
  8.  *    The information in the WFORM may be gathered from the user
  9.  *        using the function wscanform()
  10.  *
  11.  *    The resulting file may then be read back in using wfscanform()
  12.  *         (with the same WFORM table )
  13.  *
  14.  *    This procedure cna create header records or lists of options
  15.  *
  16.  *    The 4 procedures wscanform, wprintform, wfscanform, and wfprintform
  17.  *      are a matched set for
  18.  *        gathering options from the user,
  19.  *        writing them to a file, (which is used by other programs)
  20.  *        reading back the options at another time
  21.  *        displaying current sets of options on the screen
  22.  *    Each of these can be done using the same WFORM.
  23.  *
  24.  *    This system is flexible in that, if you initialize the userdata
  25.  *        to defaults before calling wfscanform
  26.  *        then not all options have to be in the file.
  27.  *
  28.  *
  29.  *    In addition, this system is 'upwardly mobile'
  30.  *        in that you can add a new element to the WFORM table
  31.  *        and set the user data = a default,
  32.  *        older files won't be affected,
  33.  *        newer files will contain the new line
  34.  *        and your program will be able to read any version.
  35.  *
  36.  *
  37.  *     PARAMETERS:
  38.  *        FILE *outfile  = file to write to.
  39.  *            If the file is openned in 'binary' mode,
  40.  *            ... each line will end with a \n\r
  41.  *            If the file is openned in 'text' mode,
  42.  *            ...a blank line will be written.
  43.  *            This won't affect wfscanform() though,
  44.  *               (wfscanform doesn't care about blanks)
  45.  *
  46.  *        WFORM form[] = table giving labels and data pointers, etc...
  47.  *    RETURNS:
  48.  *        void
  49.  */
  50. #include  "wsys.h"
  51.  
  52.  
  53. #define wftype wfspecial
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60. void wfprintform ( FILE *outfile, WFORM *form )
  61.     {
  62.     WFORM     *item, *firstentry;
  63.  
  64.     char *newline;
  65.  
  66.     /* decide if file is opened as BINARY, if so, needs a \r\n
  67.      *                                        if TEXT, needs just a \n
  68.      */
  69.     newline =  ( (outfile->flags) & _F_BIN )  ? "\r\n"  : "\n";
  70.  
  71.  
  72.     _NORMALIZE (form);
  73.  
  74.  
  75.  
  76.     firstentry = form;
  77.  
  78.  
  79.     /* scan form, decide what format data is in, write the data
  80.      */
  81.  
  82.  
  83.     for (     item = firstentry;
  84.         item->wflabel != NULL &&   ! ferror (outfile);
  85.         ++item
  86.         )
  87.         {
  88.         fprintf (outfile, item->wflabel );
  89.  
  90.  
  91.  
  92.         if ( item->wfuser  )
  93.             {
  94.  
  95.  
  96.             /* check data type against supported types,
  97.              * move single char type into form table
  98.              */
  99.             item->wftype = *(item->wformat +
  100.                 strcspn ( item->wformat, "csidfegEGouxX") );
  101.  
  102.  
  103.  
  104.  
  105.             switch ( item ->wftype )
  106.                 {
  107.             case ( 'c' ):
  108.                 fputc (*((char *) (item->wfuser)), outfile);
  109.                 break;
  110.  
  111.             case ( 's' ):
  112.                 fprintf (outfile, item->wformat,
  113.                      ((char *)item->wfuser));
  114.  
  115.                 break;
  116.  
  117.             case ( 'f' ):
  118.             case ( 'e' ):
  119.             case ( 'g' ):
  120.             case ( 'E' ):
  121.             case ( 'G' ):
  122.                 fprintf (outfile, item->wformat,
  123.                     *((float*)(item->wfuser)) );
  124.                 break;
  125.             case ( 'i' ):
  126.             case ( 'd' ):
  127.                 fprintf (outfile, item->wformat,
  128.                      *((int*)(item->wfuser)) );
  129.                 break;
  130.             case ( 'u' ):
  131.             case ( 'x' ):
  132.             case ( 'X' ):
  133.             case ( 'o' ):
  134.                 fprintf (outfile, item->wformat,
  135.                     *((unsigned int*)(item->wfuser)) );
  136.                 break;
  137.             
  138.                 }     /* end switch */
  139.  
  140.  
  141.  
  142.             } /* end if ...user data to be printed */
  143.         fprintf ( outfile, newline );
  144.         } /* end for... scanning table ... */
  145.  
  146.     return;    /* wfprintform */
  147.     }
  148.  
  149.  
  150. /*-------------------- end of wfprintfm.c ----------------------*/
  151.