home *** CD-ROM | disk | FTP | other *** search
- /*
- fnvalnum.c
-
- % valid_Double
-
- Numerical validation functions
-
- C-scape 3.2
- Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 9/17/88 jmd Converted to one function and macros, renamed file
- 12/16/88 jmd Added test for empty validation string
- 3/28/90 jmd ansi-fied
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "cscape.h"
- #include "ostdlib.h" /* for atol() */
-
- boolean valid_Double(double num, char *vstr)
- /*
- Returns whether the value of num is within the range
- specified by vstr.
- The rules for vstr are as follows:
- "(x1,y1)" ===> (x1 < num) && (num < y1)
- "(x1,y1)(x2,y2)" ===> ((x1 < num) && (num < y1)) || ((x2 < num) && (num < y2))
- "(,y1)" ===> (num < y1)
- "(x1,)" ===> (x1 < num)
- Return TRUE if vstr == NULL;
- */
- {
- double x, y;
- int state;
- boolean lower, upper;
- char *p, *q;
- char temp[VALID_SLEN + 1];
- boolean result = FALSE;
-
- if (vstr == NULL || vstr[0] == '\0') {
- return(TRUE);
- }
-
- for (p = vstr, state = 0; *p != '\0'; p++) {
- switch(state) {
- case 0:
- if (*p == '(') {
- q = temp;
- state = 1;
- }
- break;
- case 1: /* "(" */
- if (*p == ','|| q >= temp + VALID_SLEN) {
- if (q != temp) {
- *q = '\0';
- x = atof(temp);
- lower = FALSE;
- }
- else {
- lower = TRUE;
- }
- q = temp;
- state = 2;
- }
- else if (isdigit(*p) || *p == '-' || *p == '.' || *p == 'e' || *p == 'E') {
- *q++ = *p;
- }
- break;
- case 2: /* "(xx," */
- if (*p == ')'|| q >= temp + VALID_SLEN) {
- if (q != temp) {
- *q = '\0';
- y = atof(temp);
- upper = FALSE;
- }
- else {
- upper = TRUE;
- }
-
- /* evaluate */
- result = ((lower || (num >= x)) && (upper || (num <= y)));
- if (result) {
- return(TRUE);
- }
-
- q = temp;
- state = 0;
- }
- else if (isdigit(*p) || *p == '-' || *p == '.' || *p == 'e' || *p == 'E') {
- *q++ = *p;
- }
- break;
- }
- }
-
- return(FALSE);
- }
-
-