home *** CD-ROM | disk | FTP | other *** search
- /*
- fnvalrng.c 3/27/90
-
- % valid_Range
-
- Range checker for (whole number) numeric strings
-
- C-scape 3.2
- Copyright (c) 1988, by Oakland Group, Inc.
- ALL RIGHTS RESERVED.
-
- Revision History:
- -----------------
- 3/27/90 pmcm made
- */
-
- #include <stdio.h>
- #include <string.h>
- #include <ctype.h>
-
- #include "cscape.h"
- #include "strdecl.h" /* for C-scape string functions */
- #include "fnfunc.h" /* For the rest of the str functions */
-
-
- OSTATIC int ostr_NumCmp(char *s1, char *s2);
- OSTATIC char *oskipchars(char *s, char *c);
- OSTATIC int ostrdigits(char *s);
-
- boolean valid_Range(sed_type sed, long min, long max)
-
- /* compares digit string in current record to max and min
-
- returns TRUE if within bounds
- returns FALSE if out of bounds
-
- a digit string is:
-
- leading whitespace followed by
- '-' or '+' sign followed by
- leading 0's followed by
- digits
-
- the end of a digit string is marked by '\0' or any non-digit
- (such as commas or an alphabetic letter)
- */
-
- {
- char bound[30]; /* scratchpad for long */
-
- /* put current field record in sed scratchpad */
- strcpy(sed_GetScratchPad(sed), sed_GetCurrRecord(sed));
-
- sprintf(bound, "%-ld", min);
- if (ostr_NumCmp(sed_GetScratchPad(sed), bound) == -1) {
- /* is less than the minimum bound */
- return(FALSE);
- }
- sprintf(bound, "%-ld", max);
- if (ostr_NumCmp(sed_GetScratchPad(sed), bound) == 1) {
- /* is greater than maximum bound */
- return(FALSE);
- }
-
- /* within bounds */
- return(TRUE);
- }
-
-
- static int ostr_NumCmp(char *s1, char *s2)
-
- /* s1 and s2 are digit strings denoting (signed) whole numbers.
-
- RETURNS:
-
- 1 if s1 > s2
- 0 if s1 == s2
- -1 if s1 < s2
-
-
- A digit string is:
-
- leading whitespace followed by
- '-' or '+' sign followed by
- leading 0's followed by
- digits
-
- The end of a digit string is marked by '\0' or any non-digit
- (such as commas or an alphabetic letter).
- */
-
- {
- int ndigits1;
- int ndigits2;
- char * tmp;
- boolean rev;
-
- if (s1 == NULL || s2 == NULL) {
- /* one or both of the pointers is null, do something */
- return((s1) ? 1 : -1);
- }
-
- /* skip whitespace at start of strings */
- s1 = oskipchars(s1, " \t\r\n");
- s2 = oskipchars(s2, " \t\r\n");
-
- /* check the signs of the digit strings first */
- rev = FALSE;
- if (*s1 == '-' || *s2 == '-') {
- /* flag negatives for later on (the both negative case) */
- rev = TRUE;
- if (*s1 != *s2) {
-
- /* only one of them is negative so we can make quick evaluation */
- rev = FALSE;
- if (*s2 == '-') {
- /* flip pointers to save from duplicating logic */
- rev = TRUE;
- tmp = s1;
- s1 = s2;
- s2 = tmp;
- }
- s1 = oskipchars(++s1, "0");
- if (isdigit(*s1)) {
- /* is non-zero and negative */
- return((rev ? 1 : -1));
- }
- else {
- s2 = oskipchars(((*s2 == '+') ? ++s2 : s2), "0");
- return ((isdigit(*s2)) ? (rev ? 1 : -1) : 0);
- }
- }
- }
-
- /* both have same sign, compare number of digits next */
-
- s1 = oskipchars(oskipchars(s1,"-+"), "0");
- s2 = oskipchars(oskipchars(s2,"-+"), "0");
-
- if ((ndigits1 = ostrdigits(s1)) > (ndigits2 = ostrdigits(s2))) {
- return((rev ? -1 : 1));
- }
-
- if (ndigits1 < ndigits2) {
- return((rev ? 1 : -1));
- }
-
- /* same number of digits, compare digit by digit */
-
- for ( ;ndigits1 > 0 ;ndigits1--, s1++, s2++) {
- if(*s1 > *s2) {
- return((rev ? -1 : 1));
- }
- else if (*s1 < *s2) {
- return((rev ? 1 : -1));
- }
- }
- return(0);
- }
-
-
- static char *oskipchars(char *s, char *c)
-
- /* RETURNS a pointer to the first character in s
- that is not one of the given characters in c
-
- s and c must be null terminated strings
- */
- {
- char *c1;
-
- if (s != NULL && c != NULL) {
- for ( ; *s != '\0'; s++) {
- for (c1 = c; *c1 != '\0' && *s != *c1; c1++){}
- if (*c1 == '\0') {
- /* we passed through the list of chars w/o a match in s */
- break;
- }
- }
- }
- return(s);
-
- }
-
- static int ostrdigits(char *s)
-
- /* Skips whitespace, -/+ sign,
- then counts digits (including leading 0's)
- up to first non-digit
-
- RETURNS count of digits
- */
- {
- int count = 0;
-
- if(s != NULL) {
-
- /* skip leading whitespace, then -/+ sign, then leading 0's */
-
- s = oskipchars(oskipchars(s, " \t\r\n"), "-+");
-
- for( ; *s; s++) {
- if (isdigit(*s)) {
- count++;
- }
- else {
- break;
- }
- }
- }
- return(count);
- }