Skip navigation.
 
mlC strtok question
FROM : János
DATE : Thu Apr 05 19:10:56 2007

Hi,

I am trying to parse a string with the C library string function 
strtok.  The string can have the value from:

"  -t any -j 1 -q 17"

to

"  -t any -j 1 -q 17 -n 3 -d 10 -k 3 -m "

If the -m flag is present it always at the end of the string.  The 
delimiter is a single space.  Here are the declarations for variables 
I use.

        uint16 i, j ;
        uint16 n, om, qs, nr, sk, mr;
        uint32 lj, index, cst ;
        const char *cflags ;
        char *flg;
        char *token;
        char *tokenval;
        char *code;
        const char *delim;
        delim = " ";

I use this while loop to parse the string that is in flg

        flg = (char *)cflags;
        token = (char *)delim;
        tokenval = (char *)delim;
        j = 1;
        while (token !=NULL || tokenval !=NULL) {
             if (j==1)    
              token = strtok(flg,delim);
           else
              token = strtok(NULL,delim);    
           if ( strstr( token,"-t") != NULL)    {
               tokenval = strtok(NULL,delim);
              code = tokenval;
           }
           if ( strstr( token,"-j") != NULL)    {
               tokenval = strtok(NULL,delim);
              lj = (uint32)tokenval;
           }
           if ( strstr( token,"-q") != NULL)    {
               tokenval = strtok(NULL,delim);
              cst = (uint32)tokenval;
              qs = (uint16)cst;
           }
           if ( strstr( token,"-n") != NULL)    {
               tokenval = strtok(NULL,delim);
              cst = (uint32)tokenval;
              nr = (uint16)cst;
           }
           if ( strstr( token,"-d") != NULL)    {
               tokenval = strtok(NULL,delim);
              cst = (uint32)tokenval;
              om = (uint16)cst;
           }
           if ( strstr( token,"-k") != NULL)    {
               tokenval = strtok(NULL,delim);
              cst = (uint32)tokenval;
              sk = (uint16)cst;
           }
           if ( strstr( token,"-m") != NULL)    {
              mr = 1;
              break ;
           }
           fprintf(stderr,    "token=%s  tokenval=%s\n", token,tokenval);
   
           j++;
       }
        fprintf(stderr,    "-t=%s -j=%d -q=%d -n=%d -d=%d -k=%d -m=%d
\n", code,lj,qs,nr,om,sk,mr);

I never get to the second fprint, after the while.  Here is a typical 
output by the first fprint if I parse the "  -t any -j 1 -q 17" string

token=-t  tokenval=any
token=-j  tokenval=1
token=-q  tokenval=17
Bus error

I am following this example:
http://math1.unice.fr/laboratoire/help/info/glibc/libc_65.html

What am I doing wrong ?

Thanks ahead,

János
P.S.  If I comment out the second fprint I got the same output, so 
the program dies in the while  when the value of token becomes NULL. 
Before the second fprint.
I do the (uint32) and (uint16) castings to avoid a warning of "cast 
from pointer to integer of different size"._______________________________________________
MacOSX-dev mailing list
<email_removed>
http://www.omnigroup.com/mailman/listinfo/macosx-dev

Related mailsAuthorDate
mlC strtok question János Apr 5, 19:10
mlRe: C strtok question Norman Gray Apr 5, 19:23
mlRe: C strtok question Peter Ilberg Apr 5, 20:03
mlRe: C strtok question János Apr 5, 20:49
mlRe: C strtok question Andy Armstrong Apr 5, 20:57
mlRe: C strtok question Uli Kusterer Apr 6, 12:26
mlRe: C strtok question Koryn Grant Apr 11, 08:35
mlRe: C strtok question Uli Kusterer Apr 11, 11:27
mlRe: C strtok question George Warner Apr 11, 18:10
mlRe: C strtok question Graham J Lee Apr 11, 18:40
mlRe: C strtok question János Apr 11, 18:57
mlRe: C strtok question Koryn Grant Apr 11, 21:36