Using sscanf for parsing strings to unt commands

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using sscanf for parsing strings to unt commands

2,674 Views
Ricardo_RauppV
Contributor I
Hi people
 
I´m build a string interpreter for serial console ends, and I´m trying to devode this strings into commands, as well its values, as parameters for such sommands.
I think I could use this approach to decode XML frames also ...

Description:
I want to use the sscanf function to detect certain incoming strings and its variable fileds value, working as a parser of string data to decimal commands.For example:
uint16 parse_string_x_commands((uint32*)info)

if(sscanf( (char8*)buffer[],"relay%1=on",&(*info))==1)
{
   return(command_1)
}
else if(sscanf( (char8*)buffer[],"relay%1=off",&(*info))==1)
{
   return(command_2)
}

The problem is that the first sscanf string comparsion accept the string:
relay1=off, and so I got a wrong interpretation.Summarizing, my idea is to decode lots of strings and its value to decimal value I can use as commands and data.O thought sscanf would reject different strings, working as a filter (like strcmp() )so the only extra work I would need is to obtain the variable value fields.Some suggsetion toaccomplish this parsing task?I would really be happy if sscanf could accomplish it ..its very easy ...if works fine...sure
Ricardo Raupp
Labels (1)
0 Kudos
2 Replies

340 Views
mccPaul
Contributor I
Hi Ricardo,
 
I don't use CodeWarrior so I can't check this for myself, but most libraries I have use need a format specifier like this:
 
%[*][width]type
 
In the line:
 
Code:
if(sscanf( (char8*)buffer[],"relay%1=on",&(*info))==1)
 
it doesn't look like you have specified the type, should it be %1d? Maybe CodeWarrior assumes d if you omit a type specifier, but you should probably be more defensive than that for a command parser.
 
Also, sscanf returns the number of fields successfully converted, your code will always stop at the first line because the first field will always be converted. You could do this instead:
 
Code:
char cmd[4] = {0, 0, 0, 0};if (sscanf((char8*)buffer[], "relay%1d=%3s", &(*info), cmd) == 2){   if (strcmp(cmd, "on"))   {      return(command_1);   }   else if (strcmp(cmd, "off"))   {      return(command_2);   }}

 


Hope this helps,
 
Paul.
 
(Edit: The stupid forum software insists on inserting childish smiley icons, I apologise for that!)

Message Edited by mccp on 2007-05-08 09:54 AM
--
Alban Edit: used SRC to remove childish smiley icons


Message Edited by Alban on 2007-05-08 01:59 PM
0 Kudos

340 Views
Ricardo_RauppV
Contributor I
 
Hi Paul
 
Although the roiginal is right, In fact I forgot to type %1u instead of %1 ....sorry..
Good idea to solve ths issue by the way you showed...no I have na pretty simple  solution to it.
I undesrtood thar sscanf would compare the whole string bufre to the one I passed between "", and if ANY latter desn´t match, I could have a message for it..I mean..I thought sscanf also work as strcmp for the string part of parameter...
Am I totally wrong?
It seems to be....At test time, I typed (via hyperterminal)  "relai" instead of "relay" and I got the same result as typing "relay" ....so I could know that  "on" and "off" could also be missinterpreted...
 
Thank you very much !!!!
0 Kudos