doing a function according to parsed text

A

Anna

Hi.

I want to write a program which does different functions according to input.
I need to write it as general as possible.

Example:

input:
Beep 2 <should call a function which does the beep (parameter = 2)*/
Funny_text 5 3 <should call a function which writes funny text (parameters: 5,3) */

etc.

There is a large number of such functions.

Can you advice me of a good way to implment it (in c)?

Thanks,
 
E

Emmanuel Delahaye

In said:
Hi.

I want to write a program which does different functions according to
input. I need to write it as general as possible.

Example:

input:
Beep 2 <should call a function which does the beep (parameter = 2)*/
Funny_text 5 3 <should call a function which writes funny text
(parameters: 5,3) */

etc.

There is a large number of such functions.

Can you advice me of a good way to implment it (in c)?

I think you want to write a kind of shell.

The general idea is to define some array containing the string and the
function name. The parameters

shell_s tab[]=
{
{"command_a", function_a},
{"command_b", function_b},
/* etc. */
}

If you have it sorted by command names, a binary search will be efficient to
get the function address. Just call it through some pointer to function.

A generic type for such a pointer could be the same than for 'main()' with
arguments

typedef int function_f (int argc, char ** argv);

Hence type_s would be defined as follow (at minimum):

typedef struct
{
char const *s_command;
char function_f *function;
}
shell_s;

A common implementation adds a pointer to some help string:

typedef struct
{
char const *s_command;
char const *s_help;
char function_f *function;
}
shell_s;
 
J

Julian V. Noble

Anna said:
Hi.

I want to write a program which does different functions according to input.
I need to write it as general as possible.

Example:

input:
Beep 2 <should call a function which does the beep (parameter = 2)*/
Funny_text 5 3 <should call a function which writes funny text (parameters: 5,3) */

etc.

There is a large number of such functions.

Can you advice me of a good way to implment it (in c)?

Thanks,

If all the functions are known and you don't have to define them at
run-time, then set up a jump-table (array of pointers to functions),
and use a SWITCH statement to compare your input with the names of
the functions in your jump table.

Here is a short example of setting up a jump table of functions (they
all have to be the same type, of course):

// ---------------------------------------------
#include <math.h>
#include <stdio.h>

int main(void)
{
int i;
double (*f[])(double) = {sin, cos, log, exp};
for (i=0; i<4; i++) printf("%f\n", f(.5));
return 0;
}
// ---------------------------------------------


Now, if you mean you have to define the functions at run-time, then
you need something that can evaluate a string. Lisp or Forth can do
that, and for all I know, there are other languages with this ability.


--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
D

Dave Thompson

Anna wrote:
If all the functions are known and you don't have to define them at
run-time, then set up a jump-table (array of pointers to functions),
and use a SWITCH statement to compare your input with the names of
the functions in your jump table.
Example[s using names snipped]

Not in C, where the switch statement can only operate on an integer
value, against integer constants (including enum values, which are
compile-time integer constants, and constant expressions). For
strings you must execute a series of strcmp() calls or equivalent,
either written out or from an array/list/etc. Or hash the input
string to an integer, and switch on *that*.
Now, if you mean you have to define the functions at run-time, then
you need something that can evaluate a string. Lisp or Forth can do
that, and for all I know, there are other languages with this ability.

To be picky, Forth can evaluate a string, and IIRC so can APL; LISP
can evaluate an S-expression (= tree) which *may* have been produced
by parsing (reading) a string. perl can also evaluate a string,
though it's arguable whether that's a "real" programming languages,
and the same but more so for many shells.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top