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

  1. /*
  2.       fnvalstr.c        9/17/88
  3.  
  4.     % valid_String
  5.  
  6.     String 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.     11/18/88 jmd    Added command code prefixes
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. #include "cscape.h"
  22.  
  23. #define    DELIMITER    ','
  24.  
  25. boolean valid_String(string, vstr)
  26.     char *string;
  27.     char *vstr;
  28. /*
  29.     Returns whether the string is within the list of choices
  30.     specified by vstr.
  31.  
  32.     vstr is composed of strings separated by ','s.
  33.  
  34.  
  35.     For example:
  36.  
  37.     valid_String("Joe", "Joe,John,Ted");
  38.  
  39.     would return TRUE.
  40.  
  41.     Return TRUE if vstr == NULL or if vstr == "";
  42.  
  43.     vstr can have an optional command code prefix:
  44.     (the command code must be the first item in the string)
  45.  
  46.     valid_String("Joe", "{i}Joe,John,Ted");
  47.  
  48.     i        ignore case
  49.     s        strip spaces before compare
  50.     digit    use only the first n characters for the comparison. (0-9 only)
  51. */
  52. {
  53.     char *p, *q, *s1, *s2, hold;
  54.     boolean    igcase = FALSE;
  55.     boolean    strip = FALSE;
  56.     boolean    digit = FALSE;
  57.     boolean    equal;
  58.     int      count, clen = 0;
  59.  
  60.     if (vstr == NULL || vstr[0] == '\0') {
  61.         return(TRUE);
  62.     }
  63.  
  64.     /* process command code prefix */
  65.     p = vstr;
  66.     if (*p == '{') {
  67.         for(;*p != '}' && *p != '\0';p++) {
  68.             switch(*p) {
  69.             case 'i':
  70.                 /* Ignore case */
  71.                 igcase = TRUE;
  72.                 break;
  73.             case 's':
  74.                 /* Strip spaces */
  75.                 strip = TRUE;
  76.                 break;
  77.             default:
  78.                 /* Set compare length */
  79.                 if (*p >= '0' && *p <= '9') {
  80.                     clen = *p - '0';
  81.                     digit = TRUE;
  82.                 }
  83.                 break;
  84.             }
  85.         }
  86.  
  87.         if (*p == '\0' || *(p+1) == '\0') {
  88.             return(TRUE);
  89.         }
  90.         else {
  91.             /* skip past '{' */
  92.             p++;
  93.         }
  94.     }
  95.  
  96.     q = p;
  97.     for(;;p++) {
  98.         if (*p == DELIMITER || *p == '\0') {
  99.  
  100.             /* compare the two strings */
  101.             equal = TRUE;
  102.             count = 0;
  103.             for (s1 = string, s2 = q; (!digit || count < clen); s1++, s2++) {
  104.  
  105.                 if (strip) {
  106.                     /* skip spaces */
  107.                     while(*s1 == ' ') {
  108.                         s1++;
  109.                     }
  110.                     while(*s2 == ' ') {
  111.                         s2++;
  112.                     }
  113.                 }
  114.  
  115.                 /* Convert DELIMITER to '\0' */
  116.                 hold = (*s2 == DELIMITER) ? (char) '\0' : *s2;
  117.  
  118.                 if ( ((igcase) ? toupper(*s1) : *s1) !=
  119.                      ((igcase) ? toupper(hold) : hold)) {
  120.                     equal = FALSE;
  121.                     break;
  122.                 }
  123.                 if (digit) {
  124.                     /* Increment count if we're counting characters */
  125.                     count++;
  126.                 }
  127.  
  128.                 if (*s1 == '\0' || *s2 == '\0' || *s2 == DELIMITER) {
  129.                     break;
  130.                 }
  131.             }
  132.             if (equal) {
  133.                 return(TRUE);
  134.             }
  135.             if (*p == '\0') {
  136.                     break;
  137.             }
  138.             else {
  139.                 q = p + 1;
  140.             }
  141.         }
  142.     }
  143.  
  144.     return(FALSE);
  145. }
  146.  
  147.  
  148.