strtok_r and delimiters

M

Marco Trapanese

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
 
R

Richard Bos

Marco Trapanese said:
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
 
M

Marco Trapanese

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
 
A

Antoninus Twink

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.
 
M

Marco Trapanese

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top