[techtalk] switch function in C (or how to read commandline args?)

JamesA.Sutherland JamesA.Sutherland
Sat Jun 30 23:18:00 EST 2001


On Sat, 30 Jun 2001 17:56:28 -0500, you wrote:

>jas88 at cam.ac.uk said:
>> What you can do is switch() on the FIRST character of the word, then
>> use strcmp() to check for each possibility - something like this:
>
>Yuck.  Argument parsing is not a performance problem. 

Arguments? No. I just used arguments as a simple example. Conor
mentioned he was parsing some sort of configuration file, though. If
you're running from inetd, or as a short-lived utility, this could
well be an issue.

Also, from an aesthetic point of view, a single block of strcmp()s
will bring my lunch back. I like to have a nice simple control flow,
instead of having massive fanouts with lots of duplicated effort...

> If you want to roll 
>your own code, then just do a chain of strcmps (and if you feel like 
>optimizing that, stick the commonly used options first).  If you don't,
>
>> You can do something similar using the GNU getopt library

getopt is OK for handling arguments in most cases (being LGPLed), but
not for configuration files etc. Besides, using someone else's library
doesn't answer the more general question of "how do I do a
switch()-like operation on a set of strings?" - a question I'm dealing
with in another context at the moment...


James.




More information about the Techtalk mailing list