Re: useful string utility

From: Mark Montague <monty_at_gg.caltech.edu>
Date: Fri, 19 Sep 1997 17:46:01 PDT

> From: Erick Gallesio <eg_at_kaolin.unice.fr>
> To: andrewd_at_eng.uts.edu.au
> Date: Fri, 19 Sep 1997 22:23:16 +0200
> cc: stk_at_kaolin.unice.fr
> Subject: Re: useful string utility

> That's general purpose enough to be in the core release and I have
> already integrated it in my source tree. However I have implemented
> it without strtok, because strtok patches the original string by
> placing null characters into it.

It also has the annoying property that it is not reentrant; this
doesn't work in a reasonable way; this would be really bad for STk
for, e.g. parsing a string to evaluate which then parses another
string.

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

char a[40];

void tokenize(char *s, void (*action)(char *))
{
  char *tok;

  tok= strtok(s," ");
  while (tok)
    {
      (*action)(tok);
      tok=strtok(NULL," ");
    }
}

void action0(char *s)
{
  printf("%s**",s);
}

void action1(char *s)
{
  char *tok;

  tok= strtok(s,"x");
  while (tok)
    {
      tok=strtok(NULL," ");
      printf("%s**",tok);
    }
}

void action2(char *s)
{
  char *tok;

  tok= strtok(s,"x");
  printf("%s**",tok);
}

main()
{
  printf("action0:\n");
  strcpy(a,"axbxc 1x2x3 fxoxo bxaxr");
  tokenize(a, action0);
  printf("\n");

  printf("action1:\n");
  strcpy(a,"axbxc 1x2x3 fxoxo bxaxr");
  tokenize(a, action1);
  printf("\n");

  printf("action2:\n");
  strcpy(a,"axbxc 1x2x3 fxoxo bxaxr");
  tokenize(a, action2);
  printf("\n");
}

                        strtok considered harmful

                                        - M

-=-=-=-=-=-=-=-=-=-=-=+=-=-=-=-=-=-=-=-=-=-=-=+=-=-=-=-=-=-=-=-=-=-=-=-
Mark "Monty" Montague | monty_at_gg.caltech.edu | I don't do Windows(tm)
-=-=-=-=-=-=-=-=-=-=-=+=-=-=-=-=-=-=-=-=-=-=-=+=-=-=-=-=-=-=-=-=-=-=-=-
DON'T PANIC! I'm a trained professional, and far more | *Why* question
  qualified to panic in this situation than you are. | authority?
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Received on Sat Sep 20 1997 - 02:43:49 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST