Originale-mail to me for new edition

 

Text-file device drivers

 

You can define your own text-file device drivers for your programs. A text-file device driver is a set of four functions that completely implement an interface between Object Pascal’s file system and some device.

The four functions that define each device driver are Open, InOut, Flush, and Close. The function header of each function is

function DeviceFunc(var F: TTextRec): Integer;

where DeviceFunc is the name of the function (that is, Open, InOut, Flush, or Close). The return value of a device-interface function becomes the value returned by IOResult. If the return value is zero, the operation was successful.

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

 

Topic groups

 

See also

File types

Standard routines and I/O: Overview

TTextRec type

 

 

译文

 

文本文件设备驱动程序

 

可以为程序定义自身的文本文件设备驱动程序。文本文件设备驱动程序是一套四个函数,这些函数完整地实现了Object Pascal文件系统和某个设备之间的接口。

用于定义每个设备驱动程序的四个函数是OpenInOutFlushClose。每个函数的首部都是

function DeviceFunc(var F: TTextRec): Integer;

这里的DeviceFunc是函数名称(即OpenInOutFlushClose)。设备接口函数的返回值将成为IOResult函数的返回值。如果返回值是零,那么操作是成功的。

要关联设备接口函数到指定的文件,必须编写定制的Assign过程。Assign过程必须将四个设备接口函数的地址指定到在文本文件变量中的四个函数指针。此外,还应当存储fmClosed常量到Mode字段,存储文本文件缓冲区的尺寸到BufSize,存储指向文本文件缓冲区的指针到BufPtr,并且清除Name串。

例如,假定四个设备接口函数依次为DevOpenDevInOutDevFlushDevClose,那么Assign过程可以如下定义:

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;

设备接口函数可以使用文件记录中的UserData字段来存储私有信息。该字段在任何时候都不能由产品文件系统修改。

设备函数

 

主题组

 

相关主题

文件类型

标准例程和I/O:概述

TTextRec类型(详细信息见联机帮助)