home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d110 / pdc.lha / Pdc / lib / strtok.c < prev    next >
C/C++ Source or Header  |  1987-10-28  |  2KB  |  64 lines

  1. /* strtok.c
  2.  * 
  3.  * Get next token from string s (NULL on 2nd, 3rd, etc. calls),
  4.  * where tokens are nonempty strings separated by runs of
  5.  * chars from delim.  Writes NULs into s to end tokens.  delim need not
  6.  * remain constant from call to call.
  7.  */
  8.  
  9. #define  NULL      0
  10.  
  11. static char *scanpoint = NULL;
  12.  
  13. char *                                 /* NULL if no token left */
  14. strtok(s, delim)
  15. char *s;
  16. register CONST char *delim;
  17. {
  18.          register char *scan;
  19.          char *tok;
  20.          register CONST char *dscan;
  21.  
  22.          if (s == NULL && scanpoint == NULL)
  23.                    return(NULL);
  24.          if (s != NULL)
  25.                    scan = s;
  26.          else
  27.                    scan = scanpoint;
  28.  
  29.          /*
  30.           * Scan leading delimiters.
  31.           */
  32.          for (; *scan != '\0'; scan++) {
  33.                    for (dscan = delim; *dscan != '\0'; dscan++)
  34.                              if (*scan == *dscan)
  35.                                        break;
  36.                    if (*dscan == '\0')
  37.                              break;
  38.          }
  39.          if (*scan == '\0') {
  40.                    scanpoint = NULL;
  41.                    return(NULL);
  42.          }
  43.  
  44.          tok = scan;
  45.  
  46.          /*
  47.           * Scan token.
  48.           */
  49.          for (; *scan != '\0'; scan++) {
  50.                    for (dscan = delim; *dscan != '\0';)    /* ++ moved down. */
  51.                              if (*scan == *dscan++) {
  52.                                        scanpoint = scan+1;
  53.                                        *scan = '\0';
  54.                                        return(tok);
  55.                              }
  56.          }
  57.  
  58.          /*
  59.           * Reached end of string.
  60.           */
  61.          scanpoint = NULL;
  62.          return(tok);
  63. }
  64.