/* $Id: consumption.c,v 1.4 2004/06/20 01:12:51 grog Exp grog $ */ #define MAXLINE 1024 #include #include #include #include #include #include #include #include #include #include #include #define MAXTOKEN 10 char *token [MAXTOKEN]; char *infile; /* path relative to kernel build directory */ char *htmlfile; /* and where to write the output */ char *todayfile; /* today's consumption */ /* Numbers of each kind of bottle used today, per brew */ int cornysedn; int bigusedn; int halfusedn; int smallusedn; /* Numbers of each kind of bottle left after today, per brew */ int cornyleftn; int bigleftn; int halfleftn; int smallleftn; /* Numbers of each kind of bottle used today, total */ int cornyusedall; int bigusedall; int halfusedall; int smallusedall; /* Numbers of each kind of bottle left after today, total */ int cornyleftall; int bigleftall; int halfleftall; int smallleftall; int total_bottles; float total_volume = 0; /* Fields in input file, in order */ int brewno; int day; char month [MAXLINE]; char cornyleft [MAXLINE]; char bigleft [MAXLINE]; char cornyused [MAXLINE]; char bigused [MAXLINE]; char halfleft [MAXLINE]; char halfused [MAXLINE]; char smallleft [MAXLINE]; char smallused [MAXLINE]; char todays_date [MAXLINE]; FILE *in; FILE *html; /* html output file */ FILE *today; /* today's consumption */ struct stat infilestat; #define skipblanks(x) while (isspace (*x)) x++ void usage (char *myname) { fprintf (stderr, "Usage:\n" "%s [filename [html-filename [consumption-filename]]]\n", myname ); } int main (int argc, char *argv []) { char buf [MAXLINE]; int i; int status = 0; for (i = 1; i < argc; i++) { if (argv [i] [0] == '-') { fprintf (stderr, "Invalid option: %s, aborting\n", argv [i] ); usage (argv [0]); return 1; } else if (infile == NULL) infile = argv [i]; else if (htmlfile == NULL) htmlfile = argv [i]; else { fprintf (stderr, "Extraneous startup information: \"%s\", aborting\n", argv [i] ); usage (argv [0]); return 1; } } if (infile == NULL) infile = "stocks"; if (htmlfile == NULL) htmlfile = "stocks-body"; if (todayfile == NULL) todayfile = "today"; if ((in = fopen (infile, "r")) == NULL) { fprintf (stderr, "Can't open input file %s: %s (%d)\n", infile, strerror (errno), errno ); return 1; } /* * Get the last mod time of the infile as date to use * in the printout. */ stat (infile, &infilestat); infilestat.st_mtime -= 43200; /* 12 hours ago */ strftime (todays_date, MAXLINE, "%e %b %Y", localtime (&infilestat.st_mtime)); if ((html = fopen (htmlfile, "w")) == NULL) { fprintf (stderr, "Can't open output file %s: %s (%d)\n", htmlfile, strerror (errno), errno ); return 1; } if ((today = fopen (todayfile, "a")) == NULL) { fprintf (stderr, "Can't open output file %s: %s (%d)\n", todayfile, strerror (errno), errno ); return 1; } fprintf (html, " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "
\n\n"); while (fgets (buf, MAXLINE, in)) { char *s = buf; skipblanks (s); if (isdigit (*s)) /* looks like it could be right */ { int fields = sscanf (s, "%d %d.%s %s %s %s %s %s %s\n", &brewno, &day, month, cornyleft, cornyused, bigleft, bigused, halfleft, halfused, smallleft, smallused ); cornyusedn = 0; bigusedn = 0; halfusedn = 0; smallusedn = 0; cornyleftn = atoi (cornyleft); cornyleftall += cornyleftn; if (strcmp (cornyused, "-")) /* used a corny key */ { cornyusedn = abs (atoi (cornyused)); cornyusedall += cornyusedn; cornyleftn -= cornyusedn; cornyleftall -= cornyusedn; sprintf (cornyleft, "%d", cornyleftn); strcpy (cornyused, "-"); } bigleftn = atoi (bigleft); bigleftall += bigleftn; if (strcmp (bigused, "-")) /* used a big bottle */ { bigusedn = abs (atoi (bigused)); bigusedall += bigusedn; bigleftn -= bigusedn; bigleftall -= bigusedn; sprintf (bigleft, "%d", bigleftn); strcpy (bigused, "-"); } halfleftn = atoi (halfleft); halfleftall += halfleftn; if (strcmp (halfused, "-")) /* used a half bottle */ { halfusedn = abs (atoi (halfused)); halfusedall += halfusedn; halfleftn -= halfusedn; halfleftall -= halfusedn; sprintf (halfleft, "%d", halfleftn); strcpy (halfused, "-"); } smallleftn = atoi (smallleft); smallleftall += smallleftn; if (strcmp (smallused, "-")) /* used a small bottle */ { smallusedn = abs (atoi (smallused)); smallusedall += smallusedn; smallleftn -= smallusedn; smallleftall -= smallusedn; sprintf (smallleft, "%d", smallleftn); strcpy (smallused, "-"); } if (fields < 9) { status = 1; /* give up at the end */ fprintf (stderr, "Warning: invalid input line %s\n", s); } if (cornyleftn + bigleftn + halfleftn + smallleftn) /* still have some */ { printf ("%d\t%2d. %-5s %d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", brewno, day, month, year, cornyleft, cornyused, bigleft, bigused, halfleft, halfused, smallleft, smallused ); fprintf (html, " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n\n", brewno, brewno, day, month, bigleft, bigleft, bigleft, bigleft, halfleft, smallleft, bigleftn + bigleftn + halfleftn + smallleftn, bigleftn * .75 + bigleftn * 19 + halfleftn * .5 + smallleftn * .375, (((bigleftn + halfleftn + smallleftn) > 5) || cornyleftn) ? "" : "finished" ); } total_bottles += cornyleftn + bigleftn + halfleftn + smallleftn; total_volume += cornyleftn * 19 + bigleftn * .75 + halfleftn * .5 + smallleftn * .375; if (cornyusedn || bigusedn || halfusedn || smallusedn) fprintf (today, "%s\t%d\t%d\t%d\t%d\t%d\n", todays_date, brewno, cornyusedn, bigusedn, halfusedn, smallusedn, cornyusedn + bigusedn + halfusedn + smallusedn ); } } fprintf (html, " \n" " \n" " \n\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n\n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" "
Brew      bottled      0.75 l      0.5 l      0.375 l      total      litres      
%d%d. %s%s%s%s%d%4.2f%s
 
 
Total bottles%d%d%d%d
Total litres%4.2f%4.2f%4.2f%4.2f
\n", cornyleftall, bigleftall, halfleftall, smallleftall, total_bottles, cornyleftall * 19, bigleftall * .75, halfleftall * .5, smallleftall * .375, total_volume ); if (cornyusedall || bigusedall || halfusedall || smallusedall) fprintf (today, "%s\tTotal\t%d\t%d\t%d\t%d\t%d\t%6.3f\n\n", todays_date, cornyusedall, bigusedall, halfusedall, smallusedall, cornyusedall + bigusedall + halfusedall + smallusedall, cornyusedall * 19 + bigusedall * .75 + halfusedall * .5 + smallusedall * .375 ); return status; }