Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok_r and delimiters

Reply
Thread Tools

strtok_r and delimiters

 
 
Marco Trapanese
Guest
Posts: n/a
 
      05-20-2008

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
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      05-20-2008
Marco Trapanese <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Marco Trapanese
Guest
Posts: n/a
 
      05-20-2008
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
 
Reply With Quote
 
Antoninus Twink
Guest
Posts: n/a
 
      05-20-2008
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.

 
Reply With Quote
 
Marco Trapanese
Guest
Posts: n/a
 
      05-20-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strtok and strtok_r siddhu C Programming 110 09-19-2007 05:24 PM
strtok and strtok_r siddhu C++ 6 09-15-2007 09:21 AM
what's wrong with this strtok_r() g C++ 4 10-30-2006 11:47 PM
strtok/strtok_r woes kbhat@kaxy.com C Programming 23 01-28-2005 08:55 PM
overloaded >> and istream delimiters Todd Beauchemin C++ 1 07-31-2003 05:53 PM



Advertisments