home *** CD-ROM | disk | FTP | other *** search
- /*+
- * File: bar.c
- *
- * Description: Produce a bar graph using pic and troff.
- *
- * Audit Trail:
- * Original Author and Date: Dan Flak - November 26, 1990
- * Update to general case: Dan Flak - February 13, 1991
- *
- -*/
- #ifdef VERSION
- static char *SCCS = "%Z% %M% %I% %G% %U%";
- #endif
-
- /* #includes */
- #include <stdio.h>
-
- /* #defines */
- #define SAY printf
- #define QUOTE '\042'
- #define BACKSLASH '\134'
-
- /* external variables */
-
- extern char *optarg;
- extern int optind;
- extern int opterr;
- extern int optopt;
- /* referenced external functions */
- float atof();
-
- /* internal functions */
- void strip();
-
- /* global variables */
-
- struct record
- {
- char market[21];
- float f;
- char m[8];
- } rec[30];
-
- float lower = 0, upper = 100, lineat = 10;
- char title[80] = "";
- int nrecs = 0;
-
- FILE *fin, *flimit, *fopen();
-
-
- /* static variables */
-
-
- /*<
- * Function: main
- *
- * Description: Parse the data and make a pic file
- *
- * Data Type: int
- *
- * Arguments:
- * argv[] - data file.
- *
- * Flags
- * -l lower limit
- * -u upper limit
- * -i increment
- * -t title string
- *
- * Returns:
- *
- * Side Effects:
- *
- * Calls:
- *
- * PDL
- *
- >*/
-
- int
- main (argc, argv)
- int argc;
- char **argv;
- {
- int c;
- int i, j, k, nfound;
- float range, perinch;
- char linebuf[1000];
-
- while ((c = getopt (argc, argv, "i:l:u:t:")) != EOF)
- {
- switch (c)
- {
- case 'i':
- lineat = atof (optarg);
- break;
- case 'l':
- lower = atof (optarg);
- break;
- case 'u':
- upper = atof (optarg);
- break;
- case 't':
- sprintf (title, "%s", optarg);
- break;
- default:
- ;
- break;
- }
- }
-
- /* Read data file */
- if ((fin = fopen (argv[optind], "r")) == NULL)
- {
- fprintf (stderr, "%s: can't open %s\n", argv[0], argv[optind]);
- exit (-1);
- }
-
- while (fgets (linebuf, 1000, fin) != NULL)
- {
- nfound = sscanf (linebuf, "%[^|]|%[^'\n']",
- rec[nrecs].market, rec[nrecs].m);
- if (nfound == 2)
- {
- strip (rec[nrecs].m);
- if (strncmp (rec[nrecs].m, "ND", 2))
- {
- rec[nrecs].f = atof (rec[nrecs].m);
- }
- else
- {
- rec[nrecs].f = lower;
- }
- nrecs++;
- }
- }
-
- fclose (fin);
-
- /* Check upper and lower bounds */
-
- for (i = 0; i < nrecs; i++)
- {
- if (rec[i].f < lower)
- {
- fprintf (stderr, "Warning: lower limit exceeded %s - %f\n",
- rec[i].market, rec[i].f);
- }
-
- if (rec[i].f > upper)
- {
- fprintf (stderr, "Warning: upper limit exceeded %s - %f\n",
- rec[i].market, rec[i].f);
- }
- }
-
-
- /* Build the file that will plot the graph */
-
- printf (".de )k\n..\n");
- printf (".PH\n");
- printf (".ps 12\n");
- printf (".vs 12\n");
- printf (".ce 3\n");
- printf ("%cs+4%cfB%s%cfP%cs0\n",
- BACKSLASH, BACKSLASH, title, BACKSLASH, BACKSLASH);
- printf (".ps13\n");
- printf (".br\n");
- printf (".DS\n");
- printf (".PS\n");
- printf ("\n# set invisible refernece box\n");
- printf ("boxht = 7.0; boxwid = 4.5\n");
- printf ("move to (1.5, -4.0); OB: box invis %c %c\n", QUOTE, QUOTE);
- printf ("\n# set standard box height for 12 point pitch\n");
- printf ("boxht = .1667\n");
- range = upper - lower;
- perinch = 4.5 / range;
- printf ("\n# workaround for bug in 'old' pic\n");
- printf ("%c %c at OB.nw + (0, .2)\n", '"', '"');
- printf ("\n# add title reference lines\n");
- printf ("%c%4.1f%c at OB.nw + (0, .2)\n",
- '"', lower, '"');
- for (k = 0; k < range / lineat; k++)
- {
- printf ("%c%4.1f%c at OB.nw + (%f, .2)\n",
- '"', lower + (k + 1) * lineat, '"',
- (k + 1) * lineat * perinch);
- }
- for (i = 0; i < nrecs; i++)
- {
- printf ("# Graph for %s \n", rec[i].market);
- printf ("boxwid = %f\n", (rec[i].f - lower) * perinch);
- printf ("%c%s%c at OB.nw + (-.1, -%f) rjust\n",
- '"', rec[i].market, '"', i * .3333);
- printf ("move to OB.nw + (0, -%f); box\n", i * .3333);
- printf ("%c%s%c at last box.e + (.1, 0) ljust\n",
- '"', rec[i].m, '"');
- putchar ('\n');
- }
- printf ("\n# draw dashed reference lines\n");
- for (k = 0; k < range / lineat; k++)
- {
- printf ("line from OB.nw + (%f, .1) to last box.sw + (%f, -.087) dashed\n",
- (k + 1) * lineat * perinch, (k + 1) * lineat * perinch);
- }
- printf (".PE\n");
- printf (".DE\n");
- }
-
-
- /*<
- * Function: strip (s)
- *
- * Description: Strip leading and trailing blanks from a string.
- *
- * Data Type: void
- *
- * Arguments: s - the string to be stripped.
- *
- * Returns: nothing!
- *
- * Side Effects:
- *
- * Calls:
- *
- * PDL
- *
- >*/
-
- void strip (s)
- char *s;
- {
- char *ptr;
-
- ptr = s;
- while (*ptr++ == ' ')
- sprintf (s, "%s", ptr);
- return;
- }
-