Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C Programming

Name: Anonymous 2012-04-19 13:05

Hi /prague/
I'm programming in C and writing a little interactive program.
What's the best way to execute a function / piece of code depending on a string ? I could write a bunch of }else if(!strcmp(s, foo)){ but I find this hideous.
Any suggestions ?

Name: Anonymous 2012-04-19 15:01

if you have a shitton of strings:


struct node {
  struct node * children[256];
  char current_index;
  char * rest;
  void (*handler)();
};


Now simply walk down the tree by indexing the structs children with the first char of the string, stop when you reach a NULL and compare the rest of the input with the rest-field of the node.

So, rest and handler should be set only on leaf nodes. I guess the top of your mapping would be a node without an index.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List