home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3421 / mystrtok.c
Encoding:
C/C++ Source or Header  |  1991-05-25  |  1.7 KB  |  81 lines

  1. #include <string.h>
  2. /*
  3. ** $Id: strtoknst.c,v 1.1 1991/03/13 22:11:06 robert Exp $
  4. **
  5. **  This is a clone of strtok that takes another argument
  6. **  a char pointer (assumed unchanged between calls!)
  7. **  that allows nested use)
  8. **
  9. **  I hereby release this function into the public domain.
  10. **    Robert Osborne, 1990.
  11. */
  12. char *
  13. strtoknst(s1, s2, ptr)
  14.     char *s1;
  15.     char *s2;
  16.     char **ptr;
  17. {
  18.     register char *first, *second, *third;
  19.     if( s1 ) {
  20.         *ptr = s1;
  21.     }
  22.     if( !*ptr ) return (char *) 0;
  23.     first = *ptr + strspn(*ptr, s2);
  24.     if( *first == '\0' ) return (char *)0;
  25.     second = first + strcspn(first, s2);
  26.     if( *second == '\0' ) {
  27.         *ptr = (char *) 0;
  28.     }
  29.     else {
  30.         *ptr = second + strspn(second, s2);
  31.         if( **ptr == '\0' ) {
  32.             *ptr = (char *) 0;
  33.         }
  34.         *second = '\0'; 
  35.     }
  36.     return first;
  37. }
  38. #ifdef TESTING
  39.  
  40. main()
  41. {
  42.     char *tmp, *tmp2;
  43.     char *str, *str2;
  44.     
  45.     printf("test 1\n");
  46.     for(str = strtoknst("  aaaa\t bbbb cccc\tdddd    ", "\t ", &tmp);
  47.         str;
  48.         str = strtoknst((char *) 0, "\t ", &tmp) )
  49.     {
  50.             printf("\"%s\"\n", str);
  51.     }
  52.     printf("test 2\n");
  53.     for(str = strtoknst("aaaa\t bbbb cccc\tdddd    ", "\t ", &tmp);
  54.         str;
  55.         str = strtoknst((char *) 0, "\t ", &tmp) )
  56.     {
  57.             printf("\"%s\"\n", str);
  58.     }
  59.     printf("test 3\n");
  60.     for(str = strtoknst("  aaaa\t bbbb cccc\tdddd", "\t ", &tmp);
  61.         str;
  62.         str = strtoknst((char *) 0, "\t ", &tmp) )
  63.     {
  64.             printf("\"%s\"\n", str);
  65.     }
  66.     printf("test 4\n");
  67.     for(str = strtoknst("  aaaa\t bbbb  \t\t  \tcccc\tdddd", "\t ", &tmp);
  68.         str;
  69.         str = strtoknst((char *) 0, "\t ", &tmp) )
  70.     {
  71.         printf("\"%s\"\n", str);
  72.         for(str2 = strtoknst("  AAAA\t BBBB  \t\t  \tCCCC\tDDDD", "\t ", &tmp2);
  73.                     str2;
  74.             str2 = strtoknst((char *) 0, "\t ", &tmp2) )
  75.         {
  76.             printf("\"%s\"\n", str2);
  77.         }
  78.     }
  79. }
  80. #endif
  81.