home *** CD-ROM | disk | FTP | other *** search
- DESCRIPTION
-
- Using the files lookup.c [or lookup2.c] and lookup.h, you can easily maintain
- a command lookup table. The table must be of type "entry *", and you can see
- in the example code [test.c, testcmd.c, testhelp.c] that I used an aggreagate
- array of the form:
-
- entry list[] = { ... };
-
- The first "entry" in the list must have a count and a pointer to an error
- function. If you have no error function, create a null function, which
- does nothing, and use a pointer to it. Since the number occupies the
- position of a char*, you have to cast it to such. i.e., if you have
- #define'd NUM to the number of elements, you would say
-
- entry list[] = {
- (char *)NUM, &err,
- ...
- };
-
- where err is the error-handling function. err would be called when a string
- not in the list is referenced.
-
- Now, the list is not the object that does the work. Another object, of class
- "lookup" is initialized with a pointer to an "entry *". e.g., given the
- above example, you could say:
-
- lookup cmd(list);
-
- Thereafter, to retrieve the function pointer desired, you simply reference
- "cmd" with a string:
-
- int (*proc)(const char *);
- proc=cmd["query"];
-
- This would cause "proc" to point to the function associated with the
- word "query", if any; otherwise, it would point to the error function,
- which we designated above to be "err". Now you can happily dereference the
- pointer and pass it a "const char *":
-
- proc("testing");
-
- Now note that you can do both of these at once:
-
- cmd["query"]("Testing");
-
- Isn't that special?
-
- The only difference between lookup.c and lookup2.c is the implementation.
- Each gives you an advantage. If your list of names is sorted, use
- lookup.c since it uses a binary search. Obviously, if they are not
- in any order, a binary search will not work, so lookup2.c just does
- a linear search. This is the only difference between the two.
-
-
- ARGUMENT TYPE
-
- All of the function pointers in the "entry" list must take the same
- argument[s], if any, and return the same value type. I haven't built
- any elegant macros, but there are #defines in "lookup.h" to handle both
- of these problems in a less than elegant manner.
-
- The two macros ARG_TYPE and RET_TYPE define the argument type and value
- value returned by the function pointers. Just change these to handle whatever
- types you like.
-
-
- CAVEATS
-
- This is one of my first attempts at C++; I hope you find it useful. If
- you make any major modifications, or even more importantly, if you find any
- bugs, please let me know so I can fix my source and post the patches for
- one and all.
-
-
- COPYRIGHT
-
- I the author hereby place this code in the public domain. Make enough
- money to buy Canada and hire me as Prime Minister!
-
-
- AUTHOR
-
- Reid Ellis
- 176 Brookbanks Drive
- Don Mills, Ontario
- M3A 2T5
- CANADA
- +1 416 446 1644
- rae@geaclib.uucp if you're lucky, or else geaclib!rae@geac.uucp
-