strtok_r and delimiters

Discussion in 'C Programming' started by Marco Trapanese, May 20, 2008.

  1. Hi,

    I'm trying to parse strings on an Atmel AVR device. I use the WinAVR C
    Compiler (GCC)
    The strings to parse are like this:

    command -par0 -par1 -parn

    I use strok_r function:

    uint8_t sli_parseline(char *line, char *cmd, uint8_t *argc, char
    argv[][ARG_MAX_LENGHT + 1]) {

    const char delimiters[] = " -";
    char *token;
    char *ptr;

    *argc = 0;

    token = strtok_r(line, delimiters, &ptr);
    strcpy(cmd, token);

    for (;;) {
    token = strtok_r(NULL, delimiters, &ptr);
    if (token == NULL) break;
    strcpy(argv[(*argc)++], token);
    }

    }


    It works like a charm but strtok_r parses any char which is in the
    delimiters string. Instead I want the delimiter is actually the string
    itself. An example:

    load -my file

    leads to:

    cmd = load
    arg0 = my
    arg1 = file

    I want:

    cmd = load
    arg0 = my file

    This because the delimiter actually is " -" and not either ' ' or '-'.

    I'm sorry for my bad English I hope I can explain my problem.

    Thanks in advance for any advice.
    Marco / iw2nzm
     
    Marco Trapanese, May 20, 2008
    #1
    1. Advertising

  2. Marco Trapanese

    Richard Bos Guest

    Marco Trapanese <> wrote:

    > I use strok_r function:


    strtok_r isn't an ISO C function, but if it works sufficiently similarly
    to strtok() (which is ISO), it won't be able to do what you want to do.

    > It works like a charm but strtok_r parses any char which is in the
    > delimiters string.


    This is how strtok(), and presumably strtok_r() as well, is required to
    work.

    > Instead I want the delimiter is actually the string itself.


    There is no single ISO C function which will do that for you, but it's
    easy enough to write one yourself, with the help of strstr() and some
    pointer manipulation.

    Richard
     
    Richard Bos, May 20, 2008
    #2
    1. Advertising

  3. Richard Bos ha scritto:

    > strtok_r isn't an ISO C function, but if it works sufficiently similarly
    > to strtok() (which is ISO), it won't be able to do what you want to do.


    As far as I know strtok_r is a reentrant version of strtok().


    > There is no single ISO C function which will do that for you, but it's
    > easy enough to write one yourself, with the help of strstr() and some
    > pointer manipulation.


    Ah ok, I thought there was a single ISO C function because it's a very
    common behavior of many single line interpreters. Anyway, no problem:
    I'll write my own :)

    Thank for your answer.
    Marco / iw2nzm
     
    Marco Trapanese, May 20, 2008
    #3
  4. On 20 May 2008 at 10:57, Marco Trapanese wrote:
    > Ah ok, I thought there was a single ISO C function because it's a very
    > common behavior of many single line interpreters. Anyway, no problem:
    > I'll write my own :)


    It's the sort of thing that many implementations provide as a standard
    extension or library function - for example, there GNU getopt which is
    highly sophisticated. If your implementation doesn't, then yes, there's
    nothing for it but to roll your own.
     
    Antoninus Twink, May 20, 2008
    #4
  5. Antoninus Twink ha scritto:

    > It's the sort of thing that many implementations provide as a standard
    > extension or library function - for example, there GNU getopt which is
    > highly sophisticated. If your implementation doesn't, then yes, there's
    > nothing for it but to roll your own.



    I got it, thanks.

    Marco / iw2nzm
     
    Marco Trapanese, May 20, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. strtok/strtok_r woes

    , Jan 24, 2005, in forum: C Programming
    Replies:
    23
    Views:
    1,171
    Keith Thompson
    Jan 28, 2005
  2. g
    Replies:
    4
    Views:
    521
    Larry Smith
    Oct 30, 2006
  3. siddhu

    strtok and strtok_r

    siddhu, Sep 14, 2007, in forum: C++
    Replies:
    6
    Views:
    683
    James Kanze
    Sep 15, 2007
  4. siddhu

    strtok and strtok_r

    siddhu, Sep 14, 2007, in forum: C Programming
    Replies:
    110
    Views:
    3,308
    Tor Rustad
    Sep 19, 2007
  5. DFS
    Replies:
    92
    Views:
    382
    BartC
    Jun 17, 2014
Loading...

Share This Page