Here's something which you might have to do as a big series of if/else or as a case statement, if it were not for function pointers.
Code:
#include <stdio.h>
#include <string.h>
#define ASIZE(x) (sizeof(x)/sizeof(x[0]))
/* a typedef to simplify the pointer to a function declaration */
typedef void (*fnptr)(char *);
/* associate a word with a function */
typedef struct {
char *keyword;
fnptr func;
} table_st;
/* the functions we want to call */
void do_file ( char *line ) {
printf( "do_file %s\n", line );
}
void do_edit ( char *line ) {
printf( "do_edit %s\n", line );
}
void do_print ( char *line ) {
printf( "do_print %s\n", line );
}
/* associate keywords with actions */
table_st table[] = {
{ "file", do_file },
{ "edit", do_edit },
{ "print", do_print },
};
/* search the table, and call the associated function */
void lookup_function ( char *keyword, char *param ) {
int i;
for ( i = 0 ; i < ASIZE(table) ; i++ ) {
if ( strcmp( table[i].keyword, keyword ) == 0 ) {
table[i].func( param );
}
}
}
int main ( ) {
lookup_function( "file", "foo.txt" );
lookup_function( "edit", "bar.txt" );
return 0;
}