From: Peter Nilsson on
On Feb 3, 8:45 am, Kaz Kylheku <kkylh...(a)gmail.com> wrote:
> On 2010-02-02, Christian Freund <christian.fre...(a)wrz.de> wrote:
> > The only reasonable reason provided there, for tokenizing a
> > filename with always the same delmiter between directories,
> > is the last one. That is because it uses static buffers
> > internally. -> So use strtok_s instead, in your program that
> > runs strtok in several instances parallely.
>
> The ISO C strtok function can be written as a wrapper around
> the function strcspn and strspn.
>
> See:
>
> <http://groups.google.com/group/comp.lang.c/msg/a97f2432bd5e8efd%3Fdmode%3Dsource&usg=AFQjCNEy8VQYaKK08A2lGe_3RqizHAF15Q>

There are also times when you don't want to (or can't) write to
the string being parsed.

#include <stdio.h>
#include <string.h>

#define WHITESPACE " \t\r\n\v!#%^&*()-+_=[]{}\\/'\"<>,.:;"

struct span
{
char *from;
char *to;
};

struct span *spantok( struct span *scan,
const char *del,
struct span *tok )
{
if (!scan->to) scan->to = strchr(scan->from, 0);
scan->from += strspn(scan->from, del);
if (scan->from >= scan->to) return 0;
tok->from = scan->from;
tok->to = scan->from + strcspn(scan->from, del);
scan->from = tok->to;
if (tok->to > scan->to) tok->to = scan->to;
return tok;
}

int main(int argc, char **argv)
{
struct span scan; /* sequence to scan */
struct span tok; /* token: sub-sequence of scan */

if (argc)
while (argv++, --argc)
{
scan.from = *argv;
scan.to = 0;

while (spantok(&scan, WHITESPACE, &tok))
printf( "\"%.*s\"\n", (int) (tok.to - tok.from), tok.from);
}

return 0;
}

--
Peter