Quick start manual

Standard routines and I/O
8-5
Text file device drivers
To associate the device-interface functions with a specific file, you must write a
customized Assign procedure. The Assign procedure must assign the addresses of the
four device-interface functions to the four function pointers in the text file variable. In
addition, it should store the fmClosed “magic” constant in the Mode field, store the
size of the text file buffer in BufSize, store a pointer to the text file buffer in BufPtr, and
clear the Name string.
Assuming, for example, that the four device-interface functions are called DevOpen,
DevInOut, DevFlush, and DevClose, the Assign procedure might look like this:
procedure AssignDev(var F: Text);
begin
with TTextRec(F) do
begin
Mode := fmClosed;
BufSize := SizeOf(Buffer);
BufPtr := @Buffer;
OpenFunc := @DevOpen;
InOutFunc := @DevInOut;
FlushFunc := @DevFlush;
CloseFunc := @DevClose;
Name[0] := #0;
end;
end;
The device-interface functions can use the UserData field in the file record to store
private information. This field isn’t modified by the product file system at any time.
Device functions
The functions that make up a text file device driver are described below.
The Open function
The Open function is called by the Reset, Rewrite, and Append standard procedures to
open a text file associated with a device. On entry, the Mode field contains fmInput,
fmOutput, or fmInOut to indicate whether the Open function was called from Reset,
Rewrite, or Append.
The Open function prepares the file for input or output, according to the Mode value.
If Mode specified fmInOut (indicating that Open was called from Append), it must be
changed to fmOutput before Open returns.
Open is always called before any of the other device-interface functions. For that
reason, AssignDev only initializes the OpenFunc field, leaving initialization of the
remaining vectors up to Open. Based on Mode, Open can then install pointers to either
input- or output-oriented functions. This saves the InOut, Flush functions and the
CloseFile procedure from determining the current mode.