473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2615
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: kbhat | last post by:
I had written some code using strtok_r for parsing strings with two "levels" of delimiters. The code was working like a charm, until it suddenly broke one day. You see, I was applying strtok on...
4
by: g | last post by:
hello! I get a segm fault when I use strtok_r function the code : #ifndef CLIENT_MSG_H_ #define CLIENT_MSG_H_ #include <string> class Client_msg
75
by: siddhu | last post by:
Dear experts, As I know strtok_r is re-entrant version of strtok. strtok_r() is called with s1(lets say) as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with...
1
by: randyyork | last post by:
OK, I have used the strtok_r for the re-entrant posix capability, I just noticed that VC 2008 has a strtok_s what is this? It works the same but does not mention the reason for the change. Is...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.