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

Conor Daly conor.daly at oceanfree.net
Thu Jul 5 11:32:48 EST 2001


On Thu, Jul 05, 2001 at 04:06:57PM +1200 or so it is rumoured hereabouts, 
Penguina thought:
> 
> 
> 
> "Tokenizing" is the right term.
> 
> My response was to CD's token defs
> 
> >CD>So, I can do something like:
> >CD>
> >CD>#define FORCE_SWITCH "--force"
> >CD>#define CONFIG_SWITCH "-C"
> >CD>
> >CD>and so on?
> 
> > penguina at cosyn.co.nz said:
> >P> No.
> 
> CD asked "Can I do X" this is P answering "No" to CD.
> But P (me) wasn't entirely correct, musta been...tokin'

So I guess, as Almut said, we need some kinda... hash?

> >P> What this will accomplish is setting the constants
> >P> to be strings, which will then require slow strcmp
> >P> processing as above.  When you tokenize something,
> >P> you come up with a unique *numerical* constant to
> >P> represent a particular string.  You can also have
> >P> several different strings parse to the same token.
> >P> So I could say
> >P> #define HOLA   23
> >P> #define HELLO  23
> >P> #define AMIGO  24
> >P> #define FRIEND 24
> 
> On Wed, 4 Jul 2001, Jeff Dike wrote:
> J> Exactly, that's what I meant.  I should have been more clear about that.
> J> Also, "tokenizing" might be the wrong term, but I don't know of a better one
> J> offhand.
> 
> But actually a token doesn't necessarily need to be a number.
> It's just a lot clearer what's going on when numbers are used,
> and they're faster and easier to perform operations on.
> 
> Technically, there's nothing wrong with Conor's defining
> literal string tokens as above (if she doesn't mind processing
> them as such) but they wouldn't work in the following context:
> 
> >> >CD>
>         case FORCE_SWTICH:
>                 force=1;
>                 break;
>         case CONFIG_SWITCH:
>                 strcpy(config_file_name, argv[2]);
>                 break;
> >CD> <<
> 
> in the above, had FORCE_SWITCH and CONFIG_SWITCH been tokenized
> as ints, it would be OK, but in this example, they're strings.
> 

So, the upshot is that to use a switch on string values, we need to use a
hash table to tokenise the strings.  After that, the switch works as:

switch(hash("String value")) {
	case FORCE:
		force=1;
		break;
	case CONFIG_SWITCH:
		strcpy(config_file_name, "String Value");
		break;

where hash("String value") returns the token (int) associated with 
"String value"


> Strings.  A Parable.
> 
> Three strings were walking down the road one day, and were very
> thirsty.  After being rejected from two watering-holes, the third
> string gets an idea.  He unravels all his strands and gets them in
> an enormous tangle.  They enter the third watering hole.  The
> publican looks down at them and says "You know, we don't serve
> strings here.  You wouldn't happen to be a string, would you?"
> 
> And the third string answers, "No.  I'm afraid not."

Hur Hur! 

Conor
-- 
Conor Daly <conor.daly at oceanfree.net>

Domestic Sysadmin :-)
---------------------
Faenor.cod.ie
 11:20am  up 20 days, 11:37,  0 users,  load average: 0.08, 0.02, 0.01
Hobbiton.cod.ie
 11:28am  up 20 days, 11:46,  1 user,  load average: 0.06, 0.03, 0.01




More information about the Techtalk mailing list