home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2758 / bar.c next >
Encoding:
C/C++ Source or Header  |  1991-02-15  |  4.2 KB  |  240 lines

  1. /*+
  2.  * File: bar.c
  3.  *
  4.  * Description: Produce a bar graph using pic and troff.
  5.  *
  6.  * Audit Trail:
  7.  *   Original Author and Date: Dan Flak - November 26, 1990
  8.  *   Update to general case: Dan Flak - February 13, 1991
  9.  *
  10. -*/
  11. #ifdef VERSION
  12. static char *SCCS = "%Z% %M% %I% %G% %U%";
  13. #endif
  14.  
  15. /* #includes */
  16. #include <stdio.h>
  17.  
  18. /* #defines  */
  19. #define SAY printf
  20. #define QUOTE '\042'
  21. #define BACKSLASH '\134'
  22.  
  23. /* external variables */
  24.  
  25. extern char *optarg;
  26. extern int  optind;
  27. extern int  opterr;
  28. extern int  optopt;
  29. /* referenced external functions */
  30. float atof();
  31.  
  32. /* internal functions */
  33. void strip();
  34.  
  35. /* global variables   */
  36.  
  37. struct record
  38.     {
  39.     char  market[21];
  40.     float f;
  41.     char  m[8];
  42.     } rec[30];
  43.  
  44. float lower = 0, upper = 100, lineat = 10;
  45. char  title[80] = "";
  46. int nrecs = 0;
  47.  
  48. FILE *fin, *flimit, *fopen();
  49.  
  50.  
  51. /* static variables   */
  52.  
  53.  
  54. /*<
  55.  * Function: main
  56.  *
  57.  * Description: Parse the data and make a pic file
  58.  *
  59.  * Data Type: int
  60.  *
  61.  * Arguments: 
  62.  *    argv[] - data file.
  63.  *
  64.  *    Flags
  65.  *    -l    lower limit
  66.  *    -u    upper limit
  67.  *    -i    increment
  68.  *    -t    title string
  69.  *
  70.  * Returns:
  71.  *
  72.  * Side Effects:
  73.  *
  74.  * Calls:
  75.  *
  76.  * PDL
  77.  *
  78. >*/
  79.  
  80. int
  81. main (argc, argv)
  82. int  argc;
  83. char **argv;
  84. {
  85. int c;
  86. int i, j, k, nfound;
  87. float range, perinch;
  88. char linebuf[1000];
  89.  
  90. while ((c = getopt (argc, argv, "i:l:u:t:")) != EOF)
  91.     {
  92.     switch (c)
  93.         {
  94.         case 'i':
  95.             lineat = atof (optarg);
  96.         break;
  97.         case 'l':
  98.             lower = atof (optarg);
  99.         break;
  100.         case 'u':
  101.             upper = atof (optarg);
  102.         break;
  103.         case 't':
  104.             sprintf (title, "%s", optarg);
  105.         break;
  106.         default:
  107.             ;
  108.         break;
  109.         }
  110.     }
  111.  
  112. /*  Read data file */
  113. if ((fin = fopen (argv[optind], "r")) == NULL)
  114.     {
  115.     fprintf (stderr, "%s: can't open %s\n", argv[0], argv[optind]);
  116.     exit (-1);
  117.     }
  118.  
  119. while (fgets (linebuf, 1000, fin) != NULL)
  120.     {
  121.     nfound = sscanf (linebuf, "%[^|]|%[^'\n']",
  122.         rec[nrecs].market, rec[nrecs].m);
  123.     if (nfound == 2)
  124.         {
  125.         strip (rec[nrecs].m);
  126.         if (strncmp (rec[nrecs].m, "ND", 2))
  127.             {
  128.             rec[nrecs].f = atof (rec[nrecs].m);
  129.             }
  130.         else
  131.             {
  132.             rec[nrecs].f = lower;
  133.             }
  134.         nrecs++;
  135.         }
  136.     }
  137.  
  138. fclose (fin);
  139.  
  140. /*  Check upper and lower bounds  */
  141.  
  142. for (i = 0; i < nrecs; i++)
  143.     {
  144.     if (rec[i].f < lower)
  145.         {
  146.         fprintf (stderr, "Warning: lower limit exceeded %s - %f\n", 
  147.             rec[i].market, rec[i].f);
  148.         }
  149.  
  150.     if (rec[i].f > upper)
  151.         {
  152.         fprintf (stderr, "Warning: upper limit exceeded %s - %f\n",
  153.             rec[i].market, rec[i].f);
  154.         }
  155.     }
  156.  
  157.  
  158. /*  Build the file that will plot the graph  */
  159.  
  160. printf (".de )k\n..\n");
  161. printf (".PH\n");
  162. printf (".ps 12\n");
  163. printf (".vs 12\n");
  164. printf (".ce 3\n");
  165. printf ("%cs+4%cfB%s%cfP%cs0\n", 
  166.     BACKSLASH, BACKSLASH, title, BACKSLASH, BACKSLASH);
  167. printf (".ps13\n");
  168. printf (".br\n");
  169. printf (".DS\n");
  170. printf (".PS\n");
  171. printf ("\n# set invisible refernece box\n");
  172. printf ("boxht = 7.0; boxwid = 4.5\n");
  173. printf ("move to (1.5, -4.0); OB: box invis %c %c\n", QUOTE, QUOTE);
  174. printf ("\n# set standard box height for 12 point pitch\n");
  175. printf ("boxht = .1667\n");
  176. range = upper - lower;
  177. perinch = 4.5 / range;
  178. printf ("\n# workaround for bug in 'old' pic\n");
  179. printf ("%c   %c at OB.nw + (0, .2)\n", '"', '"');
  180. printf ("\n# add title reference lines\n");
  181. printf ("%c%4.1f%c at OB.nw + (0, .2)\n", 
  182.     '"', lower, '"');
  183. for (k = 0; k < range / lineat; k++)
  184.     {
  185.     printf ("%c%4.1f%c at OB.nw + (%f, .2)\n", 
  186.         '"', lower + (k + 1) * lineat, '"',
  187.         (k + 1) * lineat * perinch);
  188.     }
  189. for (i = 0; i < nrecs; i++)
  190.     {
  191.     printf ("# Graph for %s \n", rec[i].market);
  192.     printf ("boxwid = %f\n", (rec[i].f - lower) * perinch);
  193.     printf ("%c%s%c at OB.nw + (-.1, -%f) rjust\n", 
  194.         '"', rec[i].market, '"', i * .3333);
  195.     printf ("move to OB.nw + (0, -%f); box\n", i * .3333);
  196.     printf ("%c%s%c at last box.e + (.1, 0) ljust\n", 
  197.         '"', rec[i].m, '"');
  198.     putchar ('\n');
  199.     }
  200. printf ("\n# draw dashed reference lines\n");
  201. for (k = 0; k < range / lineat; k++)
  202.     {
  203.     printf ("line from OB.nw + (%f, .1) to last box.sw + (%f, -.087) dashed\n",
  204.         (k + 1) * lineat * perinch, (k + 1) * lineat * perinch);
  205.     }
  206. printf (".PE\n");
  207. printf (".DE\n");
  208. }
  209.  
  210.  
  211. /*<
  212.  * Function: strip (s)
  213.  *
  214.  * Description: Strip leading and trailing blanks from a string.
  215.  *
  216.  * Data Type: void
  217.  *
  218.  * Arguments: s - the string to be stripped.
  219.  *
  220.  * Returns: nothing!
  221.  *
  222.  * Side Effects:
  223.  *
  224.  * Calls:
  225.  *
  226.  * PDL
  227.  *
  228. >*/
  229.  
  230. void strip (s)
  231. char *s;
  232. {
  233. char *ptr;
  234.  
  235. ptr = s;
  236. while (*ptr++ == ' ')
  237. sprintf (s, "%s", ptr);
  238. return;
  239. }
  240.