home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / dist-stat / pair.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-13  |  4.3 KB  |  230 lines

  1. /*
  2.  * $Header: /files1/home/toy/src/stat/RCS/pair.c,v 1.2 90/09/10 12:45:22 toy Exp $
  3.  * NAME
  4.  *    pair - pair element groups
  5.  *
  6.  * SYNOPSIS
  7.  *    pair [-H] [-P prec] [-c cols] [-F base] [-x reps] [vectors...]
  8.  *
  9.  * DESCRIPTION
  10.  * HISTORY
  11.  * $Log:    pair.c,v $
  12.  * Revision 1.2  90/09/10  12:45:22  toy
  13.  * Removed some debugging statements.
  14.  * Forgot to initialize variable j before using it!
  15.  *
  16.  * Revision 1.1  90/09/06  18:20:52  toy
  17.  * Initial revision
  18.  *
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <assert.h>
  24.  
  25. #if    defined(__STDC__) || defined(__GNUC__)
  26. #include <stddef.h>
  27. #include <stdlib.h>
  28. #else
  29. #include <malloc.h>
  30. #endif
  31.  
  32. #include "gps.h"
  33.  
  34. #define    DEF_PROGNAME    "pair"
  35.  
  36. static const char RCSID[] = "@(#) $Id: pair.c,v 1.2 90/09/10 12:45:22 toy Exp $";
  37.  
  38. extern int getopt ();
  39. extern int optind;
  40. extern char *optarg;
  41.  
  42. const char *progname;
  43.  
  44. #define    OPT_STRING    "HP:c:F:x:"
  45.  
  46. #if    defined(__STDC__) || defined(__GNUC__)
  47. void
  48. help (void)
  49. #else
  50. void
  51. help ()
  52. #endif
  53. {
  54.   static const char *help_strings[] =
  55.   {
  56.     "\t-H\tThis help\n",
  57.     "\t-P p\tSet number of significant digits to p.  If not specified\n",
  58.     "\t\tuse value of STAT_PREC from environment, if available.  Else\n",
  59.     "\t\tuse default.\n",
  60.     "\t-c c\tSet number of output elements per line to n\n",
  61.     "\t-F F\tBase (x) vector is read from file F.  If not specified, the\n",
  62.     "\t\tdata is read from stdin.\n",
  63.     "\t-x x\tNumber of elements taken from the base for each one element\n",
  64.     "\t\ttaken from the vector.\n",
  65.     "Output is a vector with elements taken alternately from a base\n",
  66.     "and from a vector.  The base vector is specified with the F\n",
  67.     "option, or is implicitly defined to come from stdin.\n",
  68.     NULL
  69.   };
  70.  
  71.   (void) fprintf (stderr, "%s\n", RCSID);
  72.   print_help_strings (help_strings);
  73. }
  74.  
  75.  
  76. int
  77. main (argc, argv)
  78.      int argc;
  79.      char *argv[];
  80. {
  81.   char *base;
  82.   int k;
  83.   int option;
  84.   int errcnt;
  85.   int done;
  86.   int nfiles;
  87.   int base_reps;
  88.   double x;
  89.   FILE *base_fp;
  90.   FILE **file_list;
  91.  
  92.   set_def_prec ();
  93.   progname = get_my_name (argv[0], DEF_PROGNAME);
  94.  
  95.   errcnt = 0;
  96.   base = NULL;
  97.   base_fp = NULL;
  98.   base_reps = 1;
  99.  
  100.   /*
  101.    * Read options
  102.    */
  103.  
  104.   while ((option = getopt (argc, argv, OPT_STRING)) != -1)
  105.     {
  106.       switch (option)
  107.     {
  108.     case 'H':        /* Help                 */
  109.       help ();
  110.       exit (0);
  111.       break;
  112.     case 'P':        /* Precision             */
  113.       (void) set_precision (atoi (optarg));
  114.       break;
  115.     case 'c':        /* Number of output columns     */
  116.       (void) set_max_columns (atoi (optarg));
  117.       break;
  118.     case 'F':        /* Base vector file         */
  119.       base = optarg;
  120.       break;
  121.     case 'x':        /* number of reps from base     */
  122.       base_reps = atoi (optarg);
  123.       break;
  124.     default:
  125.       break;
  126.     }            /* endswitch     */
  127.     }                /* endwhile     */
  128.  
  129.   /*
  130.    * If a base vector is given, read it from there.
  131.    * Otherwise read it from stdin.
  132.    */
  133.  
  134.   if (base == NULL)
  135.     {
  136.       base_fp = stdin;
  137.     }
  138.   else
  139.     {
  140.       base_fp = fopen (base, "r");
  141.       if (base_fp == NULL)
  142.     {
  143.       message ("cannot open base file `%s'\n", base);
  144.       errcnt++;
  145.     }
  146.     }
  147.  
  148.   /*
  149.    * Exit on errors
  150.    */
  151.  
  152.   if (errcnt > 0)
  153.     {
  154.       exit (1);
  155.     }
  156.  
  157.   /*
  158.    * Open any other files listed on the command line
  159.    */
  160.  
  161.   nfiles = argc - optind;
  162.  
  163.   if (nfiles <= 0)
  164.     {
  165.       nfiles = 1;
  166.       file_list = (FILE **) malloc (nfiles * sizeof (FILE *));
  167.       assert (file_list != NULL);
  168.       *file_list = stdin;
  169.     }
  170.   else
  171.     {
  172.       int j;
  173.       file_list = (FILE **) malloc (nfiles * sizeof (FILE *));
  174.       assert (file_list != NULL);
  175.       j = 0;
  176.       for (k = 0; k < nfiles; k++, optind++)
  177.     {
  178.       file_list[j] = fopen (argv[optind], "r");
  179.       if (file_list[j] == NULL)
  180.         {
  181.           message ("cannot open for reading:  `%s'\n", argv[optind]);
  182.         }
  183.       else
  184.         {
  185.           j++;
  186.         }
  187.     }            /* endfor     */
  188.       nfiles = j;
  189.     }
  190.  
  191.   /*
  192.    * Read some elements from the base and print them out.
  193.    * Then print an element from the given files.
  194.    */
  195.  
  196.   done = FALSE;
  197.   do
  198.     {
  199.       for (k = 0; k < base_reps; k++)
  200.     {
  201.       if (get_number (base_fp, &x) == 1)
  202.         {
  203.           print_number (stdout, x);
  204.         }
  205.       else
  206.         {
  207.           done = TRUE;
  208.         }
  209.     }
  210.  
  211.       if (!done)
  212.     {
  213.       for (k = 0; k < nfiles; k++)
  214.         {
  215.           if (get_number (file_list[k], &x) == 1)
  216.         {
  217.           print_number (stdout, x);
  218.         }
  219.           else
  220.         {
  221.           done = TRUE;
  222.         }
  223.         }            /* endfor     */
  224.     }
  225.   } while (!done);
  226.  
  227.   end_column (stdout);
  228.   return 0;
  229. }
  230.