home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cproto.zip / cproto46 / strkey.c < prev    next >
C/C++ Source or Header  |  1998-01-18  |  1KB  |  57 lines

  1. /* $Id: strkey.c,v 4.2 1998/01/19 00:49:28 cthuang Exp $
  2.  *
  3.  * Some string handling routines
  4.  */
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <string.h>
  8. #include "cproto.h"
  9.  
  10. #define    LETTER(c) (isalnum(c) || (c == '_') || (c == '$'))
  11.  
  12. /*
  13.  * Return a pointer to the first occurence of the given keyword in the string
  14.  * or NULL if not found.  Unlike 'strstr()', which verifies that the match is
  15.  * against an identifier-token.
  16.  */
  17. char *
  18. strkey (src, key)
  19. char *src, *key;
  20. {
  21.     if (src != 0 && key != 0) {
  22.     register char *s  = src, *d;
  23.     register size_t len = strlen(key);
  24.  
  25.     while (*s) {
  26.         if (!LETTER(*s)) {
  27.         s++;
  28.         } else {
  29.         for (d = s; LETTER(*d); d++)
  30.             ;
  31.         if ((d - s) == len && !strncmp(s, key, len))
  32.             return s;
  33.         s = d;
  34.         }
  35.     }
  36.     }
  37.     return NULL;
  38. }
  39.  
  40. /*
  41.  * Delete a specified keyword from a string if it appears there
  42.  */
  43. void
  44. strcut (src, key)
  45. char *src, *key;
  46. {
  47.     register char *s, *d;
  48.  
  49.     if ((s = strkey(src, key)) != '\0') {
  50.     d = s + strlen(key);
  51.     while (*d != '\0' && !LETTER(*d))
  52.         d++;
  53.     while ((*s++ = *d++) != '\0')
  54.         ;
  55.     }
  56. }
  57.