Calling functions from text files.

Started by
6 comments, last by poly-gone 20 years, 3 months ago
Just a crazy idea, but is there a way to call functions from text files? What I mean is, if I have a program that has a function called, say, "Print", which just prints a string ////////////// // print.cpp ////////////// void print( char* string ) { printf( "%s", string ); } and I wanna call this function from a text file by reading it, could I do that? ////////////////// // mytextfile.txt ////////////////// print( "Hello" ) Thanks.
Advertisement
Umm.

You are using C so the short answer is: no. Code needs to be compiled before it can be run.

If you were using a scripting language (Perl is one that comes to mind), then yeah you could do that.

Or, if you had a scripting engine built into your program, you could use that.
In general, this is no easy way. What you'd probably end up doing is using a map that associates function names with function pointers, and calling the function pointer with whatever arguments are in the file. Problems with this approach are that functions with different parameters and return types have different signatures, so you could only store common function prototypes in the same map (ie. all void return, no parameter functions in one map, etc.)

[edited by - Zipster on January 5, 2004 10:18:33 PM]
It''s possible, but you would have to parse the text file and call the function in your code anyways. What you described is called a scripting language, and is often used in games so that variables and such can be changed on the fly without having to recompile (which can take many hours for large games).

--------
"Hey, what about those baggy pants you kids are wearin'' these days? Aren''t they hip and cool and poppin'' fresh?"-Peter Griffin
"Everytime I see an M followed by a dollar sign I just say "oh god" and go to the next post."-Neurokaotix
the D programming language|google|msdn|XLib reference manual
It is possible in C# (and vb.net)
Look up reflection. Because the .net framework includes the compilers it can compile .cs files on the fly when you read them in and call functions or class member functions defined as text.
Thanks guys. I know it can be done with a scripting language, but I wanted to know if I could do it without using one.
How could it be done without scripting languages if this is exactly what scripting languages are for. I mean they wouldn''t have existed otherwise you know.
This is what sripting languages are for but sometimes you might want to do something like this yourself. What it boils down to is benig able to parse a text file correctly. If somewhere in your text file you have a list of commands to be interpreted such as "print xxxxxxx" or "createfile C:\xxx\x" or whatever, it helps to think of these like a user typed command instead of reading from a text file. If you only plan on having a few of these commands, it will probably be easier for you to write your own functions and map them to functions probably in some sort of table. But if you are using a bunch of them, you probably want to use a scripting language.

In any case, a user typed command is usually handled in 2 parts, the command and the arguments following the command. Like in old text based MUDs you would type "tell bob something". The command being bob and the arguments being bob and whatever else after bob. The function prototype for the tell command might look something like:
void do_tell( THE_PLAYER *pPlayer, char *arguments );

and a command table entry might look something like:
{ "tell", do_tell }, ...

when you typed a command line in, the first word would be the command. Lookup the command in the command table and execute the function associated with it with a reference to the player who typed it and the rest of the line as the arguments. The biggest drawback to this is that inside each command function you will have to parse the arguments accordingly which can be a pain if you are trying to do too much and is also time consuming and very error prone.

Depending on what you are doing, something similar to the above may be a good choice or a scripting language may just make your life a whole lot easier in the long run. Ultimately it is your choice and if your not happy with one, you can always choose another.



Evillive2
E-Mail
Evillive2

This topic is closed to new replies.

Advertisement