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

Jeff Dike jdike at karaya.com
Sat Jun 30 20:05:04 EST 2001


jas88 at cam.ac.uk said:
> Arguments? No. I just used arguments as a simple example. Conor
> mentioned he was parsing some sort of configuration file, though.

I missed the config file part of it.  But the original message talked about 
both config files and command line options.

> If you're running from inetd, or as a short-lived utility, this could
> well be an issue.

It would have to be very short-lived with a huge config file.

> Also, from an aesthetic point of view, a single block of strcmp()s
> will bring my lunch back.

A chain of strcmps has one huge advantage - it's basically impossible to get 
wrong once one has been done right.  Aesthetics don't matter to me as much as 
code that's obviously right.

> 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... 

If it really is performance critical to switch on strings, it's fairly likely 
that it's because you're having to continuously switch on the same strings.  
In that case, converting the strings to tokens once and switching on the 
tokens is probably a decent way to go.

				Jeff






More information about the Techtalk mailing list