home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / BTP20.ZIP / EXAMPLE1.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1993-06-10  |  5.4 KB  |  158 lines

  1. PROGRAM Example1;             { (C) 1993 John C. Leon   last updated 6/10/93 }
  2.  
  3. {Illustrates use of BTP in a real, though simple, program.  Uses the LString
  4.  key type to facilitate use of Pascal strings.  Note that the key length must
  5.  include room for the length byte.  Thus, the key fields below (Pascal
  6.  strings) are sized to one less than the key length, making room for the
  7.  length byte.  Note also that the key buffer must contain the length byte as
  8.  well, and since we may have keys that are NOT strings, we have to actually
  9.  do a MOVE to get the field(s) into the key buffer to avoid a type mismatch
  10.  error.  This is because the key buffer is simply an array of char.
  11.  
  12.  Note filling of data buffer with nulls before each insertion.  While not
  13.  strictly necessary (the length byte will force ignoring garbage after last
  14.  significant char in any LString field), this guarantees that each field will
  15.  contain only the characters desired.
  16.  
  17.  Recall that this program expects to find a Btrieve file named EXAMPLE in
  18.  the current directory.  Use the CREATE1 program to create the EXAMPLE file,
  19.  which has three keys: Key 0: 20 positions, an LString (Pascal string),
  20.                               representing LAST NAME in this program;
  21.                        Key 1: 10 positions, an LString (Pascal string),
  22.                               representing FIRST NAME in this program;
  23.                        Key 2:  8 positions, a Float (Pascal double),
  24.                               representing Salary in this program
  25. }
  26.  
  27.  
  28. {$IFDEF production} {$D-,R-,L-,S-} {$ENDIF}
  29. {$N+,E+,A-}
  30.  
  31. USES
  32.    BTP;
  33.  
  34. TYPE
  35.    MyFields = record
  36.                  case integer of
  37.                  1: (First   : string[9];  {Note keylen is 10, so string is}
  38.                      Last    : string[19]; {sized to 1 less-room for len byte}
  39.                      Salary  : double;
  40.                      KeyBuf  : array[1..20] of char);{size to largest key }
  41.                  2: (DBuffer : array[1..38] of char);{size to rec length  }
  42.                  3: (Position: array[1..2] of word); {high word first!    }
  43.                  end;                                {useful after GETPOS }
  44.  
  45.    TMyFile = object(BFile)
  46.                 Fields: MyFields;
  47.                 constructor Init;
  48.                 function BT(OpCode, Key: integer): integer; virtual;
  49.                 function GetPosition: longint;
  50.                 end;
  51.  
  52. VAR
  53.    MyFile  : TMyFile;
  54.    First   : string[9];
  55.    Last    : string[19];
  56.    CurrPosition: longint;
  57.  
  58. constructor TMyFile.Init;
  59. begin
  60.    inherited Init('example', Accel, '');              {Open file in accelerated mode.}
  61. end;
  62.  
  63. function TMyFile.BT(OpCode, Key:integer):integer;
  64. begin
  65.    {Whatever you do, do NOT use Specs.RecLen as the 4th parameter in the
  66.     Btrv call below, as it is a VAR parameter, and could be changed by
  67.     Btrieve on return from the Btrv call!!  Thus we use a data member that is
  68.     not a part of our encapsulated file stats.}
  69.    DBufferLen := Specs.RecLen;
  70.    BT := Btrv(OpCode, PosBlk, Fields, DBufferLen, Fields.KeyBuf, Key);
  71. end;
  72.  
  73. function TMyFile.GetPosition: longint;
  74. begin
  75.    GetPosition := Fields.Position[2] + Fields.Position[1] * 65536;
  76. end;
  77.  
  78.  
  79. (* begin main program code *)
  80. BEGIN
  81.    if not IsBtrieveLoaded then
  82.       begin
  83.       writeln('Please load Btrieve before running this program ...');
  84.       halt(1);
  85.       end;
  86.  
  87.    MyFile.Init;
  88.    if BStatus <> 0 then
  89.       begin
  90.       writeln('Error initializing ... Status: ', BStatus);
  91.       halt(2);
  92.       end;
  93.  
  94.    with MyFile.Fields do
  95.       begin
  96.  
  97.       fillchar(DBuffer, sizeof(DBuffer), 0);
  98.       First := 'Sara';
  99.       Last  := 'Leon';
  100.       Salary := 20000;
  101.       move(Last[0], KeyBuf, sizeof(Last)+1);
  102.       BStatus := MyFile.BT(BInsert, Zero);
  103.  
  104.       fillchar(DBuffer, sizeof(DBuffer), 0);
  105.       First := 'John';
  106.       Last  := 'Leon';
  107.       Salary := 10000;
  108.       move(Last[0], KeyBuf, sizeof(Last)+1);
  109.       BStatus := MyFile.BT(BInsert, Zero);
  110.  
  111.       end;
  112.  
  113.    MyFile.BT(BGetFirst, 1);
  114.    with MyFile.Fields do
  115.       begin
  116.       writeln;
  117.       writeln('Status of Get First on First Name index: ', BStatus);
  118.       writeln('Last Name retrieved: ', Last);
  119.       writeln('First Name retrieved: ', First);
  120.       writeln('Salary retrieved: ', Salary:5:2);
  121.       end;
  122.  
  123.    MyFile.BT(BGetNext, 1);
  124.    with MyFile.Fields do
  125.       begin
  126.       writeln;
  127.       writeln('Status of Get Next: ', BStatus);
  128.       writeln('Last Name retrieved on a Get Next: ', Last);
  129.       writeln('First Name retrieved on a Get Next: ', First);
  130.       writeln('Salary retrieved on a Get Next: ', Salary:5:2);
  131.       end;
  132.  
  133.    MyFile.BT(BGetFirst, 2);
  134.    with MyFile.Fields do
  135.       begin
  136.       writeln;
  137.       writeln('Status of Get First on Salary Index: ', BStatus);
  138.       writeln('Last Name retrieved: ', Last);
  139.       writeln('First Name retrieved: ', First);
  140.       writeln('Salary retrieved: ', Salary:5:2);
  141.       end;
  142.  
  143.    MyFile.BT(BGetLast, 1);
  144.    MyFile.BT(BGetPos, 1);
  145.    with MyFile.Fields do
  146.       begin
  147.       CurrPosition :=  MyFile.GetPosition;
  148.       writeln;
  149.       writeln('Physical position of last record in First Name index: ', CurrPosition);
  150.       end;
  151.  
  152.    MyFile.Close;
  153.    MyFile.Init;  {Recall that on initialization, we read all stats into the}
  154.    writeln;      {object, among other things.}
  155.    writeln('Total number of records: ', MyFile.NumRecs);
  156.    MyFile.Close;
  157.  
  158. END.