471,123 Members | 978 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,123 software developers and data experts.

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(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
Jun 27 '08 #1
4 2470
Marco Trapanese <ma******************@gmail.comwrote:
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
Jun 27 '08 #2
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
Jun 27 '08 #3
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.

Jun 27 '08 #4
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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.