home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / c / string.arc / STRTOK.C < prev    next >
Text File  |  1984-12-31  |  2KB  |  59 lines

  1. /*  File   : strtok.c
  2.     Author : Richard A. O'Keefe.
  3.     Updated: 11 April 1984
  4.     Defines: istrtok(), strtok()
  5.  
  6.     strtok(src, set)
  7.     skips over initial characters of src[] which occur in set[].
  8.     The result is a pointer to the first character of src[]
  9.     which does not occur in set[].  It then skips until it finds
  10.     a character which does occur in set[], and changes it to NUL.
  11.     If src is NullS, it is as if you had specified the place
  12.     just after the last NUL was written.  If src[] contains no
  13.     characters which are not in set[] (e.g. if src == "") the
  14.     result is NullS.
  15.  
  16.     To read a sequence of words separated by spaces you might write
  17.     p = strtok(sequence, " ");
  18.     while (p) {process_word(p); p = strtok(NullS, " ");}
  19.     This is unpleasant, so there is also a function
  20.  
  21.     istrtok(src, set)
  22.     which builds the set and notes the source string for future
  23.     reference.  With this function, you can write
  24.  
  25.     for (istrtok(wordlist, " \t"); p = strtok(NullS, NullS); )
  26.         process_word(p);
  27. */
  28.  
  29. #include "strings.h"
  30. #include "_str2set.h"
  31.  
  32. static    char    *oldSrc = "";
  33.  
  34. void istrtok(src, set)
  35.     char *src, *set;
  36.     {
  37.     _str2set(set);
  38.     if (src != NullS) oldSrc = src;
  39.     }
  40.  
  41.  
  42. char *strtok(src, set)
  43.     register char *src;
  44.     char *set;
  45.     {
  46.     char *save;
  47.  
  48.     _str2set(set);
  49.     if (src == NullS) src = oldSrc;
  50.     while (_set_vec[*src] == _set_ctr) src++;
  51.     if (!*src) return NullS;
  52.     save = src;
  53.     while (_set_vec[*++src] != _set_ctr) ;
  54.     *src++ = NUL;
  55.     oldSrc = src;
  56.     return save;
  57.     }
  58.