home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / VRAC / ECHORPT.ZIP / ECHOREPT.C next >
Encoding:
C/C++ Source or Header  |  1993-05-05  |  6.2 KB  |  251 lines

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. #define NAMESIZE (31)
  7. #define DBSIZE 300
  8. #define MAXHUD (200)
  9. char gepath[128];
  10.  
  11. FILE *dbfp;
  12.  
  13. int thisdate;
  14. struct tm *block;
  15.  
  16. struct dbrecord {
  17.     char echoname[NAMESIZE];
  18.     int area;       /* area number */
  19.     int tally[30];    /* 30 day record */
  20. } db[DBSIZE] = {0};
  21.  
  22. #define a_area (((struct dbrecord *)a)->area)
  23. #define b_area (((struct dbrecord *)b)->area)
  24. #define a_name (((struct dbrecord *)a)->echoname)
  25. #define b_name (((struct dbrecord *)b)->echoname)
  26.  
  27. int sort_function(const void *a, const void *b) {
  28.     if (a_area == 0) return (b_area ? 1 : 0);
  29.     if (b_area == 0) return -1;
  30.     if (a_area <= MAXHUD) { /* first is hudson */
  31.         if (b_area <= MAXHUD)
  32.             return (a_area < b_area? -1 : 1);    /* no dupes are possible */
  33.         else
  34.             return -1; /* Hudson less than passthrough */
  35.     }
  36.     else { /* first is passthrough */
  37.         if (b_area <= MAXHUD)
  38.             return 1;
  39.         else 
  40.             return strcmp(a_name, b_name);
  41.     }
  42. }
  43.  
  44. void sortit(void) {
  45.     int i, i2, j;
  46.     for (i=0; i<DBSIZE; i++) {    /* delete entries that have no messages */
  47.         for (j=0,i2=0; i2<30; i2++) j += db[i].tally[i2];
  48.         if (j == 0) { /* clear entry */
  49.             db[i].echoname[0] = 0;    
  50.             db[i].area = 0;
  51.         }
  52.     }
  53.     qsort((void *)db, DBSIZE, sizeof(struct dbrecord), sort_function);
  54. }
  55.  
  56. void makereport(char *filename, int days, char *title) {
  57.     
  58.     FILE *fp;
  59.     char ans[64];
  60.     char areastr[6];
  61.     int st = (days>28 || days==1 ? 0 : 1);    /* starting day */
  62.     int i,j;
  63.     long gt=0;    /* grand total */
  64.     int tot[DBSIZE];    /* total for each area */
  65.     int wtot[4][DBSIZE];    /* totals for weeks */
  66.  
  67.     days += st;    /* when we start on day 1, end is one further */
  68.     
  69.     if ((fp = fopen(filename, "w")) == NULL) {
  70.         fprintf(stderr, "Error--unable to create %s\n", filename);
  71.         return;    /* no error exit on this one */
  72.     }
  73.     
  74.     strcpy(ans, asctime(block));
  75.     fprintf(fp, "%s          Report prepared %.10s, %.4s\n\n", 
  76.         title, ans, &ans[20]);
  77.  
  78.     
  79.     for (i = 0; i < DBSIZE; i++) {
  80.         tot[i] = 0;
  81.         for (j = st; j < days; j++) tot[i] += db[i].tally[j];
  82.         gt += tot[i];
  83.         if (days==29) {
  84.             wtot[0][i] = wtot[1][i] = wtot[2][i] = wtot[3][i] = 0;
  85.             for (j=1; j<8; j++) {
  86.                 wtot[0][i] += db[i].tally[j];
  87.                 wtot[1][i] += db[i].tally[j+7];
  88.                 wtot[2][i] += db[i].tally[j+14];
  89.                 wtot[3][i] += db[i].tally[j+21];
  90.             }
  91.         }
  92.     }
  93.     
  94.     if (days==8) 
  95.         fputs(" #  Area Name                Daily Totals            Total Percent\n",fp);
  96.     else if (days==29)
  97.         fputs(" #  Area Name                Weekly Totals   Total Percent\n",fp);
  98.     else
  99.         fputs(" #  Area Name                      Total Percent\n",fp);
  100.  
  101.     for (i = 0; i < DBSIZE; i++) {
  102.         if (tot[i] == 0) continue;
  103.         if (db[i].area > MAXHUD) 
  104.             strcpy(areastr, " P ");
  105.         else
  106.             sprintf(areastr, "%3d", db[i].area);
  107.         if (days==8) { /* different for weekly report */
  108.             fprintf(fp, "%s %-20.20s%4d%4d%4d%4d%4d%4d%4d %5d %.1f%%\n", 
  109.                 areastr,
  110.                 db[i].echoname, 
  111.                 db[i].tally[7],
  112.                 db[i].tally[6],
  113.                 db[i].tally[5],
  114.                 db[i].tally[4],
  115.                 db[i].tally[3],
  116.                 db[i].tally[2],
  117.                 db[i].tally[1],
  118.                 tot[i], 
  119.                 (100.0 * tot[i])/gt);
  120.         }
  121.         else if (days==29) { /* and for monthly report */
  122.             fprintf(fp, "%s %-20.20s%4d %4d %4d %4d  %5d  %.1f%%\n",
  123.                 areastr,
  124.                 db[i].echoname,
  125.                 wtot[3][i], wtot[2][i], wtot[1][i], wtot[0][i],
  126.                 tot[i],
  127.                 (100.0 * tot[i])/gt);
  128.         }
  129.         else {
  130.             fprintf(fp, "%s %-30.30s %5d %.1f%%\n", areastr,
  131.                 db[i].echoname, 
  132.                 tot[i], 
  133.                 (100.0 * tot[i])/gt);
  134.         }
  135.     }
  136.     fprintf(fp, "\n%ld total messages\n", gt);
  137.     fclose(fp);
  138. }
  139.  
  140. void readsummary(void) {
  141.     FILE *transp;
  142.     char filename[128];
  143.     char buff[128];
  144.     char area[NAMESIZE];
  145.     int i,j, areanum;
  146.  
  147.     strcpy(filename, gepath);
  148.     strcat(filename, "summary.log");
  149.     
  150.     if ((transp = fopen(filename, "r")) == NULL) {
  151.         fprintf(stderr, "Note-- summary.log file not found\n");
  152.         return;
  153.     }
  154.     
  155.     i=5;
  156.     while (i--) fgets(buff, sizeof(buff), transp); /* header lines */
  157.     
  158.     while (fgets(buff, sizeof(buff), transp) != NULL) {
  159.         if (strncmp(buff, "-----", 5) == 0) break;    /* end reached */
  160.         if (sscanf(buff, "%d %30s %d", &areanum, &area, &j) != 3 ||
  161.             areanum < 1 ) continue; /* bad record ?? */
  162.         for (i=0; i<DBSIZE && db[i].echoname[0] != 0; i++) 
  163.             if (strcmp(area, db[i].echoname)==0) break;
  164.         if (i < DBSIZE) {
  165.             strcpy(db[i].echoname, area);
  166.             db[i].area = areanum;
  167.             db[i].tally[0] += j;
  168.         }
  169.     }
  170.     fclose(transp);
  171.     unlink(filename);    /* get rid of file! */
  172. }
  173.     
  174.  
  175. void getdb(void) {    /* get the database */
  176.     time_t timer;
  177.     int olddate;
  178.     int i, diff;
  179.  
  180.     timer = time(NULL);
  181.     block = localtime(&timer);
  182.     thisdate = block->tm_yday;
  183.     
  184.     if ((dbfp = fopen("netrep2.dat","rb+")) == NULL) { /* no file */
  185.         if ((dbfp = fopen("netrep2.dat", "wb+")) == NULL) {
  186.             fprintf(stderr, "Error-- could not create netrep2.dat file\n");
  187.             exit(1);
  188.         }
  189.         memset(db, 0, sizeof(db));
  190.         return;
  191.     }
  192.  
  193.     fread(&olddate, sizeof(int), 1, dbfp);
  194.     
  195.     if (fread(db, sizeof(db), 1, dbfp) != 1) {
  196.         fprintf(stderr, "Error -- bad netrep2.dat file\n");
  197. /*        exit(1); */
  198.     }
  199.  
  200.     rewind(dbfp);
  201.     
  202.  
  203.     if (olddate == thisdate) return;
  204.     
  205.     if (olddate > thisdate) {
  206.         olddate -= 365;
  207.         if (block->tm_year % 4 == 1) olddate--;
  208.     }
  209.     
  210.     diff = thisdate - olddate;
  211.  
  212.     if (diff > 29) { /* boy has time passed! */
  213.         memset(db, 0, sizeof(db));
  214.         return;
  215.     }
  216.  
  217.     for (i=0; i<DBSIZE; i++) {
  218.         memmove(&db[i].tally[diff], &db[i].tally[0], sizeof(int)*(30-diff));
  219.         memset(&db[i].tally[0], 0, sizeof(int)*diff);
  220.     }
  221.     return;
  222. }
  223.     
  224.  
  225. void putdb(void) {
  226.     if (fwrite(&thisdate, sizeof(int), 1, dbfp) != 1 ||
  227.         fwrite(db, sizeof(db), 1, dbfp) != 1) {
  228.         fprintf(stderr, "Error -- write of netrep2.dat file failed");
  229.         exit(1);
  230.     }
  231.     fclose(dbfp);
  232. }
  233.  
  234.  
  235. void main(int argc, char **argv) {
  236.     strcpy(gepath, getenv("GE"));    /* gecho path */
  237.     if (*gepath && gepath[strlen(gepath)-1] !='\\') strcat(gepath, "\\");
  238.  
  239.     getdb();
  240.     
  241.     readsummary();
  242.  
  243.     if (argc > 1) sortit();    /* sort database if we are making a report */
  244.     if(argc > 1) makereport(argv[1], 1,  "Todays's Echo Traffic    ");
  245.     if(argc > 2) makereport(argv[2], 7,  "Previous Week's Echo Traffic");
  246.     if(argc > 3) makereport(argv[3], 28, "Four Week Echo Traffic      ");
  247.     if(argc > 4) makereport(argv[4], 30, "Thirty Day Echo Traffic     ");
  248.     
  249.     putdb();
  250. }
  251.