Global Variables

In every CC main program, there are several variables, which are declared GLOBAL (which is actually an empty definition) and are assumed to be located and initailised in the application program for the purpose of dictating to the main program, the files to open and memory to allocated. The task of writing the application is simplified as the task may be performed without having to worry about the housekeeping aspects.

Some of these variables are

1. TEXT *recbuf[]; an array of pointers to record buffers.

2. TEXT *ifile[]; an array of names of index files to open.

3. TEXT *dfile[]; an array of names of data files to open.

4. struct s_ifdblk ifdb[]; an array of file descriptor structures to hold file descriptor information for all the index files to be opened.

5. struct s_fdblk fdb[]; an array of file descriptor structures to hold file descriptor information for all the data files to be opened.

By specifying filenames in ifile, these indices are opened in the array of file descriptors ifdb[], declared to be of size n, where n is the number of indices specified in ifile[].

For example, by declaring

TEXT *ifile[] = { "cred.idx", "stock.idx", NULL};
struct s_ifdblk ifdb[2] = {0};

The two index files are opened and the file descriptor information is loaded into the ifdb array with ifdb[0] holding information on the first file, ifdb[1] holding information on the second file, etc.

When the data files are opened, enough memory to hold one datafile record is automatically allocated by the file opening routines. The memory addresses for these record buffers are recorded in the recbuf array.

Recbuf elements are allocated in pairs starting at recbuf[4] -- the even numbered elements hold the address of the start of the record buffer which is also the start of the static part of the recrd. The odd numbered elements hold the address of the variable part of the record (hence recbuf[5] = recbuf[4] + fdb[0].statsiz).

Thus if

TEXT *dfile[] = { "debtr.dat". "stock.dat", NULL};

is declared then

TEXT *recbuf[8] = {0};

must also be declared. (4 buffers + 2 for debtr +2 for stock)

By specifying 2 filenames in dfile, 2 buffers are automatically allocated: one the size of a debtor record, and one the size of a stock record.

recbuf[4] is initialised to point to to the start of the first buffer, and recbuf[6] to the start of the second buffer.

recbuf[5] and recbuf[7] are pointers to the start of the variable part of each record buffer.

By convention, if other record buffers are required, these should be pointed to by additional elements of recbuf. Hence

TEXT recbuf[10] = {0}; /* declare 10 buffer pointers */
#define DRCONT 9 /* pointer to buffer for debtor control record */

spclinit()
    {
    recbuf[DRCONT] = alloc(fdb[DEBTR].bucsiz, 0);
    }

What also occurs sometimes is that record buffers will be dynamically allocated and these will replace the previously allocated buffer so that the data access will reference the new buffer with the previous buffer being preserved in a temporary variable until being restored. Remember that recbuf is an array of data pointers.

CONTROL/Software/Development/GlobalVariables (last edited 2017-01-09 04:04:20 by thog)