home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
FUNCS
/
FNVALSTR.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-03-28
|
3KB
|
139 lines
/*
fnvalstr.c 9/17/88
% valid_String
String validation functions
C-scape 3.2
Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
11/18/88 jmd Added command code prefixes
11/04/89 jdc changed toupper & tolower to otoupper & otolower
3/28/90 jmd ansi-fied
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "cscape.h"
#define DELIMITER ','
boolean valid_String(char *string, char *vstr)
/*
Returns whether the string is within the list of choices
specified by vstr.
vstr is composed of strings separated by ','s.
For example:
valid_String("Joe", "Joe,John,Ted");
would return TRUE.
Return TRUE if vstr == NULL or if vstr == "";
vstr can have an optional command code prefix:
(the command code must be the first item in the string)
valid_String("Joe", "{i}Joe,John,Ted");
i ignore case
s strip spaces before compare
digit use only the first n characters for the comparison. (0-9 only)
*/
{
char *p, *q, *s1, *s2, hold;
boolean igcase = FALSE;
boolean strip = FALSE;
boolean digit = FALSE;
boolean equal;
int count, clen = 0;
if (vstr == NULL || vstr[0] == '\0') {
return(TRUE);
}
/* process command code prefix */
p = vstr;
if (*p == '{') {
for(;*p != '}' && *p != '\0';p++) {
switch(*p) {
case 'i':
/* Ignore case */
igcase = TRUE;
break;
case 's':
/* Strip spaces */
strip = TRUE;
break;
default:
/* Set compare length */
if (*p >= '0' && *p <= '9') {
clen = *p - '0';
digit = TRUE;
}
break;
}
}
if (*p == '\0' || *(p+1) == '\0') {
return(TRUE);
}
else {
/* skip past '{' */
p++;
}
}
q = p;
for(;;p++) {
if (*p == DELIMITER || *p == '\0') {
/* compare the two strings */
equal = TRUE;
count = 0;
for (s1 = string, s2 = q; (!digit || count < clen); s1++, s2++) {
if (strip) {
/* skip spaces */
while(*s1 == ' ') {
s1++;
}
while(*s2 == ' ') {
s2++;
}
}
/* Convert DELIMITER to '\0' */
hold = (*s2 == DELIMITER) ? (char) '\0' : *s2;
if ( ((igcase) ? otoupper(*s1) : *s1) !=
((igcase) ? otoupper(hold) : hold)) {
equal = FALSE;
break;
}
if (digit) {
/* Increment count if we're counting characters */
count++;
}
if (*s1 == '\0' || *s2 == '\0' || *s2 == DELIMITER) {
break;
}
}
if (equal) {
return(TRUE);
}
if (*p == '\0') {
break;
}
else {
q = p + 1;
}
}
}
return(FALSE);
}