Go Down

Topic: String Handling Problem (Read 823 times) previous topic - next topic

andreotti09

I could still be wrong here, iv'e only been learning C for around 3 months, but i copied it into another char array first then pointed to that as strtok_r takes a pointer for its first argument? char copiedstr[100];, or have I misunderstood something, I just read up a little on regex's and the search and match functions are a little out of context, maybe I was a bit hasty when i quoted "should do the trick"  XD is the only documentation available included in the link you posted?
Repetition Is Key For Learning

PaulS

Quote
I Have tried to implement a function which splits an incoming string into 5 arguments using the strtok_r function

The strtok_r() function is the thread safe version of the strtok() function. It is much more complicated to use than the strtok() function.

On a system that can not execute multiple threads, why do you want/need the added complexity/code size of the thread-safe version?

Use the proper function!

Nick Gammon


I could still be wrong here, iv'e only been learning C for around 3 months, but i copied it into another char array first then pointed to that as strtok_r takes a pointer for its first argument? char copiedstr[100];, or have I misunderstood something,


I don't think you are using strtok_r the right way. The third argument is supposed to be:

Quote
s3 :  Is a value-return parameter used by strtok_r() to record its progress through s1.


You aren't using it like that.

Code: [Select]
  char *saveptr1, *saveptr2, *saveptr3, *saveptr4, *saveptr5;
  str1 = sstring;
  cmd = strtok_r(str1, &delim, &saveptr1);
...
  arg1 = strtok_r(str1, &delim, &saveptr2);
...
  str1 = saveptr2;
  arg2 = strtok_r(str1, &delim, &saveptr3);
...
  str1 = saveptr3;
...
  arg3 = strtok_r(str1, &delim, &saveptr4);
...
  str1 = saveptr4;
  arg4 = strtok_r(str1, &delim, &saveptr5);


According to the docs:

Quote
To get the first token from s1, strtok_r() is called with s1 as its first parameter. Remaining tokens from s1 are obtained by calling strtok_r() with a null pointer for the first parameter.


You aren't calling strtok_r with NULL as the first parameter. You are somehow combining both ideas.

Quote
I just read up a little on regex's and the search and match functions are a little out of context, maybe I was a bit hasty when i quoted "should do the trick"  XD is the only documentation available included in the link you posted?


There's lots of documentation on regular expressions, and examples in the library. Perhaps if you showed an example of the incoming string, and what you expect to extract from it (by example). I found your description:

Quote
... it first checks the next character in the original array to see if its a '"' (quote) or not, if its a quote it sets the delimiter as a quote and calls strtok_r, and places the quoted string into 1 argument, then i change the delimiter back to a space and before calling strtok_r again it checks if the next character is an quote ...


... confusing. I can't visualize what you are passing in, and what you expect to get out.

andreotti09

#8
Jan 28, 2012, 03:15 pm Last Edit: Jan 28, 2012, 03:53 pm by andreotti09 Reason: 1
It worked as I expected until I added the if statement, what I want is to split the words into a separate variable as to obtain arguments like argv[0]-argv[4] and that worked but then I wanted to check if there was a quote and if so put whatever was between the quotes into argv[1] it worked up until this point, but when I try and check for the quote with if statement after this point it does not work as expected, the output of the above (modified) code with this input '@echo "hello world" "this string"' to the serial is

----------------
Hello World
echo
4
hello world
11
"this
5
you said: echo "hello world" "this string"
----------------
and with the input "@echo "hello world" this string" I get
----------------
Hello World
echo
4
hello world
11
this
4
you said: echo "hello world" this string
----------------
Repetition Is Key For Learning

Nick Gammon

I don't think strtok will work easily for you, nor in this case will regular expressions without some fiddling around.

What you can do is "manually" parse the string, it isn't that hard.

Something like:

1. Skip leading spaces.

2. If we have a quote:
  a. bypass it (add 1 to pointer)
  b. remember where we are
  c. scan forwards until we get the same quote again
  d. The "word" is what was between the two quotes
  e. Go back to 1

3. No quote:
  a. Remember where we are
  b. Scan forwards until we get a space
  d. The "word" is what was between the two spaces
  e. Go back to 1


You could probably write that in less lines than all the strtok stuff.

Go Up