Reputation: 109
Say I have a file that read like this:
rotate -45
move 30
Where rotate
and move
are two functions that I wrote.
But I'm reading these from a text file.
So, how would I be able to use these as commands for my program?
I was thinking about using strcmp
, for example. So I'd compare the string I read with what I know as possible commands. Then, if they match, I want to call the requested function.
I would really like to see some example code to help understanding.
Thanks for the tips. so using brunobeltran0's first method, would I do these:
char*next_command;
char* get_next_command;g = fopen("b", "r");/*b is the name of the file*/
while(!feof(g))
{
get_next_command=fgetc(g);
next_command = strtok(get_next_command, " ");
while (next_command != NULL) {
printf("%s\n", next_command);
next_command = strtok(NULL, " ");
if (!strcmp(next_command, "rotate"))
{
rotate (/*how would I get the number to be in here*/ )
}`
this don't look right. Did I miss understand you guys?
Upvotes: 2
Views: 2312
Reputation: 67713
Try to decouple the parser/dispatch code from the functions as much as possible. This shows an example of a dispatcher which will work for your sample cases:
typedef int (*Command)(double);
struct CommandEntry {
const char *name;
Command function;
};
int dispatch_commands(struct CommandEntry *commands, int num_commands,
const char *name, double param)
{
/* loop over the arguments, look for a CommandEntry with a matching name,
convert its parameter and then call it */
int i;
for (i = 0; i < num_commands; ++i) {
if (!strcmp(commands[i].name, arg))
return commands[i].*function(param);
}
return -1;
}
struct CommandEntry commands[] = {
{"rotate", &my_rotate_func},
{"move", &my_translate_func}
};
This assumes all commands take a single argument of type double
, of course ... if all your commands are this homogenous, you can make it work. Otherwise, you'll need to either specify the number of arguments in each CommandEntry
(and pass an array, since the function types still need to be the same), or give each command function the whole incoming token stream, and allow it to consume as many tokens as it wants.
Upvotes: 0
Reputation: 74
I'm going to assume that you mean that you want to write a program that, given an input file full of commands it understands, reads from said input file and performs those commands. Depending on how many commands you have, and what you know about the formatting of these functions as they come in, the surrounding code could be markedly different, but in the general case, the logic would be something along the lines of (ignoring memory management)
char *next_command = get_next_command(...); // reading the commands is really specific to the input you expect
if (!strcmp(next_command, "some_command"))
{
void *param_arr[PARAM_CNT_FOR_SOME_COMMAND] = get_params_for_some_command();
some_command(param_arr[0], param_arr[1], param_arr[2]); // assume some_command takes 3 arguments
}
else if (!strcmp(next_command, "some_other_command"))
...
For example, if you wanted to rotate -45,
char *next_command = get_next_command(...); // reading the commands is really specific to the input you expect
if (!strcmp(next_command, "rotate"))
{
void *param_arr[1] = get_rotation_angle();
rotate((int *)param_arr[0]); // assume some_command takes 3 arguments
}
should work.
If you have a map available, then mapping from the possible input commands to their respective functions and allowing the function to read from the file itself to look for its arguments would probably be more efficient.
For example:
char *next_command = get_next_command(file_pointer);
(*get_func_pointer(next_command))(file_pointer); // where get_func_pointer is a function that
// returns the function pointer assoc. with 'next_command'
/* somewhere else in the code */
void func_returned_by_get_func_pointer(FILE *fp)
{
read_params_from(fp);
do_everything_as_usual();
}
Upvotes: 2
Reputation: 24124
I would do it as follows:
HTH.
Upvotes: 1