473,804 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jun 27 '08 #1
4 2645
Marco Trapanese <ma************ ******@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
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
3780
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 the input string itself, and as long as a variable string was passed to the function, everything was hunky dory. Then one day somebody decided to pass a constant string to my code ---- and everything came collapsing down like a domino. I...
4
2904
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
25147
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 a null pointer for the first parameter. My confusion is that this behavior is same as strtok. So I assume strtok_r must also be using any function static variable to keep the information about s1. If this is the case then how strtok_r is re-...
1
9033
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 it a windows strtok_r equivalent?
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7623
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2998
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.