strtok_r and delimiters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Marco Trapanese

    strtok_r and delimiters


    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(c har *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
  • Richard Bos

    #2
    Re: strtok_r and delimiters

    Marco Trapanese <marcotrapanese NOSPAM@gmail.co mwrote:
    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

    Comment

    • Marco Trapanese

      #3
      Re: strtok_r and delimiters

      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

      Comment

      • Antoninus Twink

        #4
        Re: strtok_r and delimiters

        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.

        Comment

        • Marco Trapanese

          #5
          Re: strtok_r and delimiters

          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

          Comment

          Working...