home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Applications / Mathematiques / Newton.lha / Newton / strtok.c < prev   
C/C++ Source or Header  |  1988-11-22  |  1KB  |  62 lines

  1. /* strtok.c:    3 very useful string-handling functions.
  2.  *        You don't need this file if you are using Lattice C.
  3.  *        Manx C does not provide them.
  4.  *
  5.  * Written by Daniel Barrett.  100% PUBLIC DOMAIN. */
  6.  
  7.  
  8. #include "decl.h"
  9. #define STRING_END    '\0'
  10.  
  11. /* Return the next string "token" in "buf".  Tokens are substrings
  12.  * separated by any characters in "separators". */
  13.  
  14. char *strtok(buf, separators)
  15. char *buf, *separators;
  16. {
  17.     register char *token, *end;    /* Start and end of token. */
  18.     extern char *strpbrk();
  19.     static char *fromLastTime;
  20.  
  21.     if (token = buf ? buf : fromLastTime) {
  22.         token += strspn(token, separators);    /* Find token! */
  23.         if (*token == STRING_END)
  24.             return(NULL);
  25.         fromLastTime = ((end = strpbrk(token,separators))
  26.                 ? &end[1]
  27.                 : NULL);
  28.         *end = STRING_END;            /* Cut it short! */
  29.     }
  30.     return(token);
  31. }
  32.  
  33.     
  34. /* Return a pointer to the first occurance in "str" of any character
  35.  * in "charset". */
  36.  
  37. char *strpbrk(str, charset)
  38. register char *str, *charset;
  39. {
  40.     extern char *index();
  41.  
  42.     while (*str && (!index(charset, *str)))
  43.         str++;
  44.     return(*str ? str : NULL);
  45. }
  46.  
  47.     
  48. /* Return the number of characters from "charset" that are at the BEGINNING
  49.  * of string "str".
  50. */
  51.  
  52. int strspn(str, charset)
  53. register char *str, *charset;
  54. {
  55.     register char *s;
  56.     s = str;
  57.     while (index(charset, *s))
  58.         s++;
  59.     return(s - str);
  60. }
  61.  
  62.