program Example2;             { (C) 1991 John C. Leon   last updated 11/4/91 }

USES
    BTP;

TYPE
   ExampleFields  = record
                       case integer of
                       1: (First   : array[1..10] of char;
                           Last    : array[1..20] of char;
                           KeyBuf  : array[1..20] of char);
                       2: (DBuffer : array[1..30] of char);
                       3: (Position: array[1..2] of word);
                       end;
   PExample      = ^ExampleFile;
   ExampleFile   = object(BFileExt)
                      Fields: ExampleFields;
                      function BT(OpCode, Key: integer): integer; virtual;
                      function BTExt(OpCode, Key: integer): integer; virtual;
                      function GetNumRecs:longint;
                      function GetName: string;
                      end;


VAR
   Example         : PExample;
   NumberRecords   : longint;
   x               : string;
   Value           : TCharArray;
   Counter         : integer;

(* Define methods of ExampleFile *)
(* ------------------------------------------------------------------------ *)

function ExampleFile.BT(OpCode, Key:integer):integer;
begin
   DBufferLen := Specs.RecLen;
   BT := Btrv(OpCode, PosBlk, Fields, DBufferLen, Fields.KeyBuf, Key);
end;

{Despite the use of two different functions, thanks to OOP, we are assured
 of using the precise position block, data buffer and key buffer required.}
function ExampleFile.BTExt(OpCode, Key: integer): integer;
begin
   BStatus := BFileExt.BTExt(OpCode, Key);
   BTExt := Btrv(OpCode, PosBlk, ExtDBuffer^.Entire, DBufferLen, Fields.KeyBuf,
                 Key);
end;

function ExampleFile.GetNumRecs: longint;
begin
   GetNumRecs := NumRecs;
end;

function ExampleFile.GetName: string;
var
   First, Last : string;
begin
   First := RTrim(Fields.First);
   Last  := RTrim(Fields.Last);
   GetName := First + ' ' + Last;
end;


(* begin MAIN PROGRAM code *)
(* ------------------------------------------------------------------------ *)
BEGIN

Example := new(PExample, Init('Example', ReadOnly));
if Example^.NumRecs = 0 then
   begin
   writeln('Please run Example2.EXE at least once to put a record in file.');
   halt;
   end;

with Example^ do
   begin
   Filter.MaxSkip := 50;
   Filter.NumLogicTerms := 2;
   Extractor.NumRecords := 5;
   Extractor.NumFields  := 1;
   end;

X := 'Leon';
for Counter := 1 to length(X) do
   Value[Counter] := X[Counter];
for Counter := (length(X)+1) to 255 do
   Value[Counter] := ' ';

with Example^.FilterSpec^ do
   insert(new(PFilterSpec, InitV(BString, 20, 10, Equal, NextTermAnd, Value)));

X := 'John';
for Counter := 1 to length(X) do
   Value[Counter] := X[Counter];
for Counter := (length(X)+1) to 255 do
   Value[Counter] := ' ';

with Example^.FilterSpec^ do
   insert(new(PFilterSpec, InitV(BString, 10, 0, Equal, LastTerm, Value)));

with Example^.ExtractorSpec^ do
   insert(new(PExtSpec, Init(Example^.Specs.RecLen, 0)));

BStatus := Example^.BT(BGetFirst, Zero);
writeln('Status of get first is ', BStatus);

BStatus := Example^.BTExt(BGetNextExt, Zero);
writeln('Status of ext get next is ', BStatus);

writeln('Number of records with name ''John Leon'' is ',
        Example^.ExtDBuffer^.NumRecs);

BStatus := Example^.Close;
if BStatus = 0 then
   writeln('EXAMPLE closed successfully.')
   else
   writeln('Problem closing EXAMPLE.  Status = ', BStatus);


dispose(Example, Done);

END.


