Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

parse cmd string in C

Options
  • 26-02-2012 9:30pm
    #1
    Registered Users Posts: 307 ✭✭


    Hi folks

    Im having a bit of a problem trying to implement a parser for an SCPI ASCII string command. I can sperate out cmds keyword, and parameters OK using strtok and strcmp, however from there to making the command do something becomes very non-modular.

    for example if I wish to change a setting called OCP by setting it to 2
    then program flow as I currently think about it goes something like the below

    {
    tokenise command line
    first token...

    While (unused_tokens) {
    if command
    get key root : "SET "

    change settings,
    get key child : "OCP"

    > change OCP settings,
    get value : "2"
    get unit : "k"

    change OCP to 2 000

    next token...
    }
    }

    From about where i have put the ">" functions downwards get very use specific, meaning that for all the different commands that could be at this level of the command string I would need to write a new function...

    I am considering open sourcing the parser once completed so Id like it to be as decoupled as possible (but still working) as it could be from my specific application.... I keep getting stuck thinking that this would require the ability to be able to refer to a variable name (say the OCP setting var) from a string, which of course isnt possible in C :(

    If anyone has any suggestions Im all ears, many thanks :)


Comments

  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Any specific reason you use c? C++ has built-in support for (...) multiple arguments. OS usually has some functions to help with args too I seem to remember.


  • Registered Users Posts: 2,017 ✭✭✭Colonel Panic


    How about making a structure that represents the mapping between a command and a specific function then passing an array of those to your parser? You could take case of the use specific stuff by including the number of params and their type in the structure too.

    Since you're talking C, you could use the macros to build the table for the sake of clarity.

    That would make your parser decoupled from the application.

    I've used a table driven approach like this with great success in a debug console subsystem for a game I'm working on albeit in C++ (well, a subset thereof).


  • Registered Users Posts: 307 ✭✭wolf99


    Hi Folks

    @srsly78 C is the language I speak... you might very well ask why I didnt write my OP in Irish... ;) but thanks for the info.

    @Colonel Panic that sounds interesting, but Im not entirely sure what you mean by
    "a structure that represents the mapping between a command and a specific function"
    Could you elaborate some?

    Thanks


  • Registered Users Posts: 2,017 ✭✭✭Colonel Panic


    My understanding if you want to avoid a situation where you have some megaswitch to parse your command line stuff and decouple it from your app somewhat. I would do something like the this, although in reality it will be much more complicated:
    typedef void (*handler)(char *string);
    
    void OptionOneCallback(char *string)
    {
    	// Do something with the string
    }
    
    void OptionTwoCallback(char *string)
    {
    	// Do something with the string
    }
    
    typedef struct
    {
    	char *paramater;
    	hander callback;
    } CommandLineOption;
    
    CommandLineOption commandOptions[] =
    {
    	{ "OptionOne", OptionOneCallback },
    	{ "OptionTwo", OptionTwoCallback },
    	{ "\0", NULL }
    };
    
    void ParseCommandLine(char *commandLine, commandOptions *options)
    {
    	// Split up command line
    	// For each command line option, find the entry in the table and call the desired callback
    }
    

    The obvious downsides to this are multiple parameters, but you could cater for that sort of thing in the structure and make your command line processor a function that splits out a command line into functions and params then calls the functions.


  • Registered Users Posts: 307 ✭✭wolf99


    Many thanks Colonel Panic, I did a bit of googling based on your last post and came up with a handy post (http://www.newty.de/fpt/intro.html#what) that does basically the same thing you just posted in your explanation!

    Very kind of you to take the time as I probably would have made it as far as function pointers by myself... I program at a pretty low level (8051, etc) and dont usually have much use for the more intricate functionality of C such as function pointers.

    Thank you


  • Advertisement
  • Registered Users Posts: 2,017 ✭✭✭Colonel Panic


    No problem. It's a pretty common approach to avoiding unmaintainable switch statements and nests of if/else blocks! I tend to rely a lot on that sort of data driven approach even in more object oriented languages. It's something I certainly picked up from learning reading lots of C source when I was learning how to program!


Advertisement