home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / os / msdos / programm / 8663 < prev    next >
Encoding:
Text File  |  1992-08-20  |  2.8 KB  |  65 lines

  1. Newsgroups: comp.os.msdos.programmer
  2. Path: sparky!uunet!utcsri!torn!news.ccs.queensu.ca!mast.queensu.ca!dmurdoch
  3. From: dmurdoch@mast.queensu.ca (Duncan Murdoch)
  4. Subject: Re: Table lookup ...
  5. Message-ID: <dmurdoch.58.714323791@mast.queensu.ca>
  6. Lines: 52
  7. Sender: news@knot.ccs.queensu.ca (Netnews control)
  8. Organization: Queen's University
  9. References: <LUBKT.92Aug20094704@synergy.CC.Lehigh.EDU>
  10. Distribution: comp.os.msdos.programmer
  11. Date: Thu, 20 Aug 1992 15:16:31 GMT
  12.  
  13. In article <LUBKT.92Aug20094704@synergy.CC.Lehigh.EDU> lubkt@synergy.CC.Lehigh.EDU (Binod Taterway) writes:
  14. >
  15. >struct p_table {
  16. >    char    *parameter_name;/* name of the parameter */
  17. >    bool    specified;     /* whether or not it is user-specified */
  18. >    short    type;         /* Integer, string, long, etc. */
  19. >    char    *default_value; /* Default value */
  20. >    char    *value;     /* User-specified value */
  21. >} P_TABLE;
  22. >
  23. >P_TABLE p_table[] = {
  24. >    {"xyz_directory", TRUE, TYPE_STRING, "c:\xyz_orig", "c:\xyz"},
  25. ..
  26. >}
  27. >
  28. >Though not implemented yet, I am not convinced if this the best
  29. >approach. If you know of any other approaches to reading a
  30. >configuration file, please throw in your two cents. Thanks.
  31.  
  32. I haven't done this for configuration files, but a very nice way to 
  33. implement command line parameters that are of lots of different types and 
  34. effects is to use a message-passing strategy.  In your case, this would mean 
  35. something like this:
  36.  
  37. 1.  Create a collection of objects or whatever that need to know about the
  38. parameters.  I don't know how you'd implement this in C, but an array of 
  39. structures like you had is probably as good a way as any.  In an OOP 
  40. language, a polymorphic collection is ideal.  Give each of them some default 
  41. behaviour, in case the parameter is never specified.
  42.  
  43. 2.  Do minimal parsing of the config file line, probably just breaking it up 
  44. into parameter name and parameter value (as a string).
  45.  
  46. 3.  Pass a message containing each parsed line to every object in the 
  47. collection.  If they care about that particular parameter, they'll act on 
  48. the value; if they don't, they'll just ignore it.
  49.  
  50. The advantage of this approach is that you can have quite sophisticated 
  51. parsing for particular parameters localized in the code that deals with 
  52. those parameters, but you don't need to write one monster parser that 
  53. handles every possible case when it reads the config file.
  54.  
  55. In case you're interested, the application where I did use this approach was 
  56. the not-yet-released-but-maybe-sometime update of my disassembler DUMPPROG.
  57. You can specify a range of addresses to disassemble in a quite general 
  58. format, because the various kinds of objects it knows about can each 
  59. recognize references to themselves.  The delays on release don't come from 
  60. using this approach :-), but rather from indecision on how much to put in 
  61. the update.
  62.  
  63. Duncan Murdoch
  64. dmurdoch@mast.queensu.ca
  65.