home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c185 / 1.ddi / FUNCS.EXE / CSCAPE / SOURCE / FNVALNUM.C < prev    next >
Encoding:
C/C++ Source or Header  |  1989-09-07  |  2.0 KB  |  106 lines

  1. /*
  2.       fnvalnum.c
  3.  
  4.     % valid_Double
  5.  
  6.     Numerical validation functions
  7.  
  8.     C-scape 3.1
  9.     Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
  10.     ALL RIGHTS RESERVED.
  11.  
  12.     Revision History:
  13.     -----------------
  14.      9/17/88 jmd    Converted to one function and macros, renamed file
  15.     12/16/88 jmd    Added test for empty validation string
  16. */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. #include "cscape.h"
  23. #include "ostdlib.h"            /* for atol() */
  24.  
  25. boolean valid_Double(num, vstr)
  26.     double num;
  27.     char *vstr;
  28. /*
  29.     Returns whether the value of num is within the range
  30.     specified by vstr.
  31.     The rules for vstr are as follows:
  32.     "(x1,y1)"          ===> (x1 < num) && (num < y1)
  33.     "(x1,y1)(x2,y2)" ===> ((x1 < num) && (num < y1)) || ((x2 < num) && (num < y2))
  34.     "(,y1)"         ===> (num < y1)
  35.     "(x1,)"         ===> (x1 < num)
  36.  
  37.     Return TRUE if vstr == NULL;
  38. */
  39. {
  40.     double     x, y;
  41.     int     state;
  42.     boolean lower, upper;
  43.     char     *p, *q;
  44.     char     temp[VALID_SLEN + 1];
  45.     boolean result = FALSE;
  46.  
  47.     if (vstr == NULL || vstr[0] == '\0') {
  48.         return(TRUE);
  49.     }
  50.  
  51.     for (p = vstr, state = 0; *p != '\0'; p++) {
  52.         switch(state) {
  53.         case 0:
  54.             if (*p == '(') {
  55.                 q = temp;
  56.                 state = 1;
  57.             }
  58.             break;
  59.         case 1:                    /* "(" */
  60.             if (*p == ','|| q >= temp + VALID_SLEN) {
  61.                 if (q != temp) {
  62.                     *q = '\0';
  63.                     x = atof(temp);
  64.                     lower = FALSE;
  65.                 }
  66.                 else {
  67.                     lower = TRUE;
  68.                 }
  69.                 q = temp;
  70.                 state = 2;
  71.             }
  72.             else if (isdigit(*p) || *p == '-' || *p == '.' || *p == 'e' || *p == 'E') {
  73.                 *q++ = *p;
  74.             }
  75.             break;
  76.         case 2:                    /* "(xx," */
  77.             if (*p == ')'|| q >= temp + VALID_SLEN) {
  78.                 if (q != temp) {
  79.                     *q = '\0';
  80.                     y = atof(temp);
  81.                     upper = FALSE;
  82.                 }
  83.                 else {
  84.                     upper = TRUE;
  85.                 }
  86.  
  87.                 /* evaluate */
  88.                 result = ((lower || (num >= x)) && (upper || (num <= y)));
  89.                 if (result) {
  90.                     return(TRUE);
  91.                 }
  92.  
  93.                 q = temp;
  94.                 state = 0;
  95.             }
  96.             else if (isdigit(*p) || *p == '-' || *p == '.' || *p == 'e' || *p == 'E') {
  97.                 *q++ = *p;
  98.             }
  99.             break;
  100.         }
  101.     }
  102.  
  103.     return(FALSE);
  104. }
  105.  
  106.