home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / c / 11794 < prev    next >
Encoding:
Internet Message Format  |  1992-07-30  |  2.5 KB

  1. Path: sparky!uunet!iWarp.intel.com|inews!at4cad!mwyllie
  2. From: mwyllie@at4cad.intel.com (Mary Wyllie)
  3. Newsgroups: comp.lang.c
  4. Subject: Looking for ideas to translate strings to function calls in C.
  5. Message-ID: <12619@inews.intel.com>
  6. Date: 30 Jul 92 21:27:15 GMT
  7. Sender: news@inews.intel.com
  8. Followup-To: mwyllie@at4cad.intel.com
  9. Organization: intel
  10. Lines: 59
  11. Originator: mwyllie@at4cad
  12.  
  13. I am looking for a way to translate a string message to a function call in C.
  14. I'm currently looking at a scheme where I build a table of function information
  15. for the functions I match which includes a string to match, a function pointer
  16. and a list of parameters to be passed.  For example:
  17.  
  18. void func1();
  19. void func2();
  20.  
  21. Tfunc_info function_table[] = 
  22. {
  23.     "func1", func1, { aString, aInteger, aDouble },  /* types are enum. */
  24.     "func2", func2, { aInteger, aDouble },
  25. }
  26.  
  27. The message might look like: "func1 hello! 4 6.78"
  28.  
  29. I then parse the string, match the function, and read in the proper type of 
  30. variables.  I am having difficulty, however, finding an appropriate way to
  31. make a generic function call with the function pointer, passing parameters
  32. that may be any number and combination of types (ie. the call cannot be 
  33. dependent on the types of the parameters).
  34.  
  35. I have tried building the parameter list from a single pointer. 
  36.  
  37.   CALL:  (*function_ptr)(parms);
  38.  
  39. Some values look good, some are garbage.  I suspect I'm running into 
  40. inconsistencies between how the parameters get stacked, and how I'm writing 
  41. them to memory.  
  42.  
  43. I have also tried keeping an array of generic pointers to the parameters, and 
  44. then dereferencing the pointers to make the function call.  In this case, my
  45. function call is dependent on the number of parameters.  
  46.  
  47.   CALL:  switch(num_parms)
  48.          {
  49.          case 0:  (*function_ptr)();
  50.                   break;
  51.          case 1:  (*function_ptr)(*(parms[0]));
  52.                   break;
  53.          case 1:  (*function_ptr)(*(parms[0]), *(parms[1]));
  54.                   break;
  55.            etc.
  56.          }
  57.     
  58. However, if I do this, I cannot (and still keep the call generic) make sure I 
  59. properly typecast the values when they get passed to the function.
  60.  
  61. Worst possible case is that each function I need to call, has a wrapper 
  62. function around it to take care of the appropriate parameters.  However, I 
  63. would like to create a more general purpose mechanism.
  64.  
  65. I hope I have described the problem well enough for you to understand what I'm
  66. trying to do.  Does anyone have any ideas of how to make this work?...or
  67. a better way to solve the problem?
  68.  
  69. Thanks.
  70.  
  71. Mary
  72.