Web lists-archives.com

Re: [Mingw-users] strtok_r() not implemented




I wrote:

> I  am  trying to compile babl, which relies on the
> strtok_r function:
>
>   https://manned.org/strtok.3
>
> but it seems absent in MinGW.

The interface of strtok_r()  is  rather  ugly.   Its
first  invocation shall differ from subsequent ones,
whereas the task performed is the same.

I have come up with a more consistent version:

/* returns the index of c in s, or -1 if not found */
/* s shall be a NULL-terminated string */
int chrind( const char * s, const char c )
{  int res = -1;
   int ind =  0;
   while( s[ind] != ' ' )
   {  if( s[ind] == c )
      {  res = ind; break;  }
      ind++;
   }
   return res;
}

/* Re-entrant version with consistent invocation */
/* str and delim shall be NULL-terminated string */
char* strtok_r2( char** str, const char *delim )
{  char  tok_found = 0;
   char  is_del    = 0;
   char* res       = NULL;

   while( **str != ' ' )
   {  is_del = chrind( delim, **str ) >= 0;
      if( tok_found && is_del )
      {  **str = ' '; /* NULL-terminate in place */
         ( *str )++;   /* and wind on to the next symbol */
         break;
      }
      if( !tok_found && !is_del )
      {  tok_found = 1;
         res       = *str; /* save start of token */
      }
      ( *str )++;
   }

   return res;
}

-- 
Please, do not forward replies to my e-mail.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

This list observes the Etiquette found at 
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe