Dec 082017
C++ source code for a command lookup table. Turbo C++ compatible.
File LOOKUP.ZIP from The Programmer’s Corner in
Category C Source Code
C++ source code for a command lookup table. Turbo C++ compatible.
File Name File Size Zip Size Zip Type
LOOKUP.CPP 692 303 deflated
LOOKUP.HPP 420 246 deflated
LOOKUP2.CPP 283 183 deflated
MAKEFILE 457 215 deflated
READ.ME 2918 1433 deflated
TEST.CPP 931 490 deflated
TESTCMD.CPP 356 133 deflated
TESTHELP.CPP 365 243 deflated

Download File LOOKUP.ZIP Here

Contents of the READ.ME file


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 *);

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 *":


Now note that you can do both of these at once:


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.


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.


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.


I the author hereby place this code in the public domain. Make enough
money to buy Canada and hire me as Prime Minister!


Reid Ellis
176 Brookbanks Drive
Don Mills, Ontario
M3A 2T5
+1 416 446 1644
[email protected] if you're lucky, or else [email protected]

 December 8, 2017  Add comments

Leave a Reply