The functions that make up a text-file device driver are described below.
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.
The InOut
function is called by the Read, Readln, Write, Writeln,
Eof, Eoln, SeekEof, SeekEoln, and CloseFile standard
routines whenever input or output from the device is required.
When Mode
is fmInput, the InOut function reads up to BufSize
characters into BufPtr^, and returns the number of characters read in BufEnd.
In addition, it stores zero in BufPos. If the InOut function
returns zero in BufEnd as a result of an input request, Eof
becomes True for the file.
When Mode is fmOutput, the InOut function writes BufSize characters from BufPtr^, and returns zero in BufPos.
The Flush
function is called at the end of each Read, Readln, Write,
and Writeln. It can optionally flush the text-file buffer.
If Mode
is fmInput, the Flush function can store zero in BufPos
and BufEnd to flush the remaining (unread) characters in the buffer.
This feature is seldom used.
If Mode is fmOutput, the Flush function can write the contents of the buffer exactly like the InOut function, which ensures that text written to the device appears on the device immediately. If Flush does nothing, the text doesn’t appear on the device until the buffer becomes full or the file is closed.
The Close function is called by the CloseFile standard procedure to close a text file associated with a device. (The Reset, Rewrite, and Append procedures also call Close if the file they are opening is already open.) If Mode is fmOutput, then before calling Close, the file system calls the InOut function to ensure that all characters have been written to the device.
构造文本文件设备驱动程序的函数描述如下:
Open函数由Reset、Rewrite和Append等标准过程调用,用来打开与设备相关的文本文件。在函数入口用包含fmInput、fmOutput或fmInOut值的Mode字段表示Open函数是否是从Reset、Rewrite或Append函数被调用。
Open函数根据Mode值为文件输入或输出做准备。如果Mode指定为fmInOut(表示Open是从Append被调用),那么,在Open函数返回之前,它必须被改变成fmOutput。
Open函数总是在任何其他设备接口函数之前被调用。由于这个原因,AssignDev仅初始化了OpenFunc字段,而离开剩余向量的初始化直到Open函数被调用。基于Mode,Open函数可以建立指针指向面向输入或面向输出的函数。这里由当前模式的确定来保存InOut、Flush函数和CloseFile过程。
InOut函数由Read、Readln、Write、Writeln、Eof、Eoln、SeekEof、SeekEoln和CloseFile等标准例程调用,在设备随时请求输入或输出时。
当Mode为fmInput时,InOut函数读出直到BufSize个字符到BufPtr^中,并且返回读到BufEnd中的字符个数。此外,该函数存储零到BufPos。如果InOut函数在BufEnd中返回零作为输入请求的结果,那么文件的Eof(文件结束)状态变成True。
当Mode为fmOutput时,InOut函数从BufPtr^中写BufSize个字符,并在BufPos中返回零。
Flush函数在每个Read、Readln、Write和Writeln等例程结束时被调用。它可以随意刷新文本文件缓冲区。
如果Mode为fmInput,那么Flush函数可以在BufPos和BufEnd中存储零,以此来刷新缓冲区中剩余的(未读的)字符。这一特性很少被使用。
如果Mode为fmOutput,那么Flush函数完全可以象InOut函数那样写缓冲区的内容,以确保写入设备的文本即刻出现在设备上。如果Flush不做任何处理,那么文本将不会出现在设备上,直到缓冲区满或文件被关闭。
Close函数由CloseFile标准函数调用,用来关闭与设备相关联的文本文件。(Reset、Rewrite或Append等过程也会调用Close函数,如果正在打开的文件已经是打开的。)如果Mode为fmOutput,那么在调用Close之前,文件系统将调用InOut函数,以确保所有的字符都已经被写入设备。