home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!iWarp.intel.com|inews!at4cad!mwyllie
- From: mwyllie@at4cad.intel.com (Mary Wyllie)
- Newsgroups: comp.lang.c
- Subject: Looking for ideas to translate strings to function calls in C.
- Message-ID: <12619@inews.intel.com>
- Date: 30 Jul 92 21:27:15 GMT
- Sender: news@inews.intel.com
- Followup-To: mwyllie@at4cad.intel.com
- Organization: intel
- Lines: 59
- Originator: mwyllie@at4cad
-
- I am looking for a way to translate a string message to a function call in C.
- I'm currently looking at a scheme where I build a table of function information
- for the functions I match which includes a string to match, a function pointer
- and a list of parameters to be passed. For example:
-
- void func1();
- void func2();
-
- Tfunc_info function_table[] =
- {
- "func1", func1, { aString, aInteger, aDouble }, /* types are enum. */
- "func2", func2, { aInteger, aDouble },
- }
-
- The message might look like: "func1 hello! 4 6.78"
-
- I then parse the string, match the function, and read in the proper type of
- variables. I am having difficulty, however, finding an appropriate way to
- make a generic function call with the function pointer, passing parameters
- that may be any number and combination of types (ie. the call cannot be
- dependent on the types of the parameters).
-
- I have tried building the parameter list from a single pointer.
-
- CALL: (*function_ptr)(parms);
-
- Some values look good, some are garbage. I suspect I'm running into
- inconsistencies between how the parameters get stacked, and how I'm writing
- them to memory.
-
- I have also tried keeping an array of generic pointers to the parameters, and
- then dereferencing the pointers to make the function call. In this case, my
- function call is dependent on the number of parameters.
-
- CALL: switch(num_parms)
- {
- case 0: (*function_ptr)();
- break;
- case 1: (*function_ptr)(*(parms[0]));
- break;
- case 1: (*function_ptr)(*(parms[0]), *(parms[1]));
- break;
- etc.
- }
-
- However, if I do this, I cannot (and still keep the call generic) make sure I
- properly typecast the values when they get passed to the function.
-
- Worst possible case is that each function I need to call, has a wrapper
- function around it to take care of the appropriate parameters. However, I
- would like to create a more general purpose mechanism.
-
- I hope I have described the problem well enough for you to understand what I'm
- trying to do. Does anyone have any ideas of how to make this work?...or
- a better way to solve the problem?
-
- Thanks.
-
- Mary
-