Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strtok behaviour with delimiter

Tags:

c

linux

Below is the code snippet.

#define TABLE_DELIMITER "::"
int parse_n_store ( char *line )
{

        int i = 0;
        char *p = NULL;
        CPTR sensor_number = NULL , event_catagory = NULL, sensor_type = NULL, event_state= NULL, assertion = NULL, message_number = NULL, short_text = NULL;

        for (p = strtok(line,TABLE_DELIMITER); p != NULL; p = strtok(NULL, TABLE_DELIMITER), i++ )
        {
                if ( i == 0 )
                        sensor_number=p;
                else if ( i == 1 )
                        sensor_type = p;
                else if ( i == 2 )
                        event_catagory = p;
                else if ( i == 3 )
                        event_state = p;
                else if ( i == 4 )
                        assertion = p;
                else if ( i == 5 )
                        message_number = p;
                else if ( i == 6 )
                        short_text = p;
        }

        printf ("%s %s %s %s %s %s %s\n", sensor_number, event_catagory, sensor_type, event_state, assertion, message_number, short_text);
}

This works fine. But, when the "line" argument is "Front Board Memory status:Correctable ECC / other correctable memory error detected ; sensor (70, Memory)"

The output will be

70 SENSOR_SPECIFIC MEMORY STATE_00 True 8543 Front Board Memory status

where the short_text variable contains only "Front Board Memory status" instead of "Front Board Memory status:Correctable ECC / other correctable memory error detected ; sensor (70, Memory)"

Why strtok considering a single colon as delimiter? Can anyone solve this issue.

like image 485
Nikhil Avatar asked Dec 01 '25 06:12

Nikhil


2 Answers

Why strtok considering a single colon as delimiter?

Because it is specified in the standard(C11):

7.24.5.8 The strtok function

[...]

  1. A sequence of calls to the strtok function breaks the string pointed to by s1 into a sequence of tokens, each of which is delimited by a character from the string pointed to by s2. The first call in the sequence has a non-null first argument; subsequent calls in the sequence have a null first argument. The separator string pointed to by s2 may be different from call to call.
like image 75
Spikatrix Avatar answered Dec 03 '25 18:12

Spikatrix


You could try to use strstr to iterate the string since it can look for a substring.

You could define (beware untested) :

char *strmtok(char *s, char *delim) {
    static char *current = NULL;
    char *ix, *cr;

    if (s != NULL) { 
        current = s;
    }
    ix = strstr(current, delim);
    if (ix == NULL) return NULL;
    cr = current;
    current = ix + strlen(delim);
    *ix = '\0';
    return cr;
}

and use that as replacement for original strtok.

like image 29
Serge Ballesta Avatar answered Dec 03 '25 20:12

Serge Ballesta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!