home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume11 / watcher / part01 / check_item.c < prev    next >
C/C++ Source or Header  |  1987-09-27  |  3KB  |  117 lines

  1. /*
  2.    check_item: given a value and a change format structure, make sure
  3.    that the value is in range.
  4.  
  5.    Basically, this routine is a large switch statement on the type of
  6.    change that grabs the necessary info, and checks to see if the item
  7.    is worth mentioning.
  8.  
  9.    Note that what we print out depends on whether or not something else
  10.    has been found wrong on this line.
  11.  
  12.    Kenneth Ingham
  13.  
  14.    Copyright (C) 1987 The University of New Mexico
  15. */
  16.  
  17. #include "defs.h"
  18.  
  19. check_item(cf, value, cmd, line, prev_val)
  20. char *value, *cmd, *line;
  21. struct change_fmt_st *cf;
  22. union all_u *prev_val;
  23. {
  24.     extern int line_ok, cmd_ok;
  25.     double v, pct_chg, abs_chg;
  26.     int len;
  27.  
  28.     v = atof(value);
  29.     len = strlen(value);
  30.  
  31.     switch(cf->type) {
  32.         case PERCENT:
  33.             if (prev_val == NULL) /* nothing to compare with */
  34.                 return;
  35.             if (v == 0) /* avoid divide by 0 */
  36.                 return;
  37.             pct_chg = (v - prev_val->floatval) / v;
  38.             if (pct_chg > cf->fmt.percent) {
  39.                 if (line_ok) {
  40.                     printf("%s had ", cmd);
  41.                     printf("%s change by more than %.2f percent.\n",
  42.                         cf->name, cf->fmt.percent*100);
  43.                     printf("%s\n",line);
  44.                 }
  45.                 else {
  46.                     printf("Also, it had ");
  47.                     printf("%s change by more than %.2f percent.\n",
  48.                         cf->name, cf->fmt.percent*100);
  49.                 }
  50.                 printf("Previous value %.2f; ",
  51.                     prev_val->floatval);
  52.                 printf("current value %.2f.\n", v);
  53.                 line_ok = False;
  54.             }
  55.             break;
  56.         case ABSOLUTE:
  57.             if (prev_val == NULL) /* nothing to compare with */
  58.                 return;
  59.             abs_chg = v - prev_val->floatval;
  60.             if (abs_chg > cf->fmt.abs_amount) {
  61.                 if (line_ok) {
  62.                     printf("%s had ", cmd);
  63.                     printf("%s change by more than %.2f.\n",
  64.                         cf->name, cf->fmt.abs_amount);
  65.                     printf("%s\n",line);
  66.                 }
  67.                 else {
  68.                     printf("Also, it had ");
  69.                     printf("%s change by more than %.2f.\n",
  70.                         cf->name, cf->fmt.abs_amount);
  71.                 }
  72.                 printf("Previous value %.2f; ",
  73.                     prev_val->floatval);
  74.                 printf("current value %.2f.\n", v);
  75.                 line_ok = False;
  76.             }
  77.             break;
  78.         case MAX_MIN:
  79.             if (v > cf->fmt.max_min.max || v < cf->fmt.max_min.min) {
  80.                 if (line_ok) {
  81.                     printf("%s has a ", cmd);
  82.                     printf("max/min value out of range:\n");
  83.                     printf("%s\n",line);
  84.                 }
  85.                 else {
  86.                     printf("Also, it has a ");
  87.                     printf("max/min value out of range:\n");
  88.                 }
  89.                 printf("where %s = %.2f; ", cf->name, v);
  90.                 printf("valid range %.2f to %.2f.\n",
  91.                       cf->fmt.max_min.min, cf->fmt.max_min.max);
  92.                 line_ok = False;
  93.             }
  94.             break;
  95.         case STRING:
  96.             if (strncmp(cf->fmt.str_value, value, len) != 0) {
  97.                 if (line_ok) {
  98.                     printf("%s has a string ", cmd);
  99.                     printf("value which is not valid:\n");
  100.                     printf("%s\n",line);
  101.                 }
  102.                 else {
  103.                     printf("Also, it has a string");
  104.                     printf("value which is not valid:\n");
  105.                 }
  106.                 printf("where %s = '%s'; Should be '%s'\n",
  107.                     cf->name, value, cf->fmt.str_value);
  108.                 line_ok = False;
  109.             }
  110.             break;
  111.         default:
  112.             printf("check_item: impossible condition\n");
  113.             break;
  114.     }
  115.     cmd_ok = line_ok;
  116. }
  117.