home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / commercial-software / programming / TURBODBT.ZIP / GETKEY.BOX < prev    next >
Text File  |  1996-07-15  |  6KB  |  209 lines

  1.  
  2.  
  3. (*******************************************************)
  4. (*                                                     *)
  5. (*         TURBO-ACCESS Version 1.2 (CP/M-80)          *)
  6. (*                                                     *)
  7. (*                   GETKEY module                     *)
  8. (*                                                     *)
  9. (*         Use with TURBO PASCAL 2.0 or later          *)
  10. (*                                                     *)
  11. (*              Copyright (C) 1984,85 by               *)
  12. (*               Borland International                 *)
  13. (*                                                     *)
  14. (*******************************************************)
  15.  
  16.  
  17. (*$A+,R-,W3*)
  18.  
  19. procedure NextKey(var IdxF       : IndexFile;
  20.                   var ProcDatRef : Integer;
  21.                   var ProcKey                );
  22. var
  23.   PKey   : TaKeyStr absolute ProcKey;
  24.   R      : Integer;
  25.   PagPtr : TaPagePtr;
  26. begin
  27.   with IdxF do
  28.   begin
  29.     if PP = 0 then
  30.       R := RR
  31.     else
  32.     with Path[PP] do
  33.     begin
  34.       TaGetPage(IdxF,PageRef,PagPtr);
  35.       R := PagPtr^.ItemArray[ItemArrIndex].PageRef;
  36.     end;
  37.     while R <> 0 do
  38.     begin
  39.       PP := PP + 1;
  40.       with Path[PP] do
  41.       begin
  42.         PageRef := R;
  43.         ItemArrIndex := 0;
  44.       end;
  45.       TaGetPage(IdxF,R,PagPtr);
  46.       R := PagPtr^.BckwPageRef;
  47.     end;
  48.     if PP <> 0 then
  49.     begin
  50.       while (PP > 1) and
  51.             (Path[PP].ItemArrIndex = PagPtr^.ItemsOnPage) do
  52.       begin
  53.         PP := PP - 1;
  54.         TaGetPage(IdxF,Path[PP].PageRef,PagPtr);
  55.       end;
  56.       if Path[PP].ItemArrIndex < PagPtr^.ItemsOnPage then
  57.         with Path[PP] do
  58.         begin
  59.           ItemArrIndex := ItemArrIndex + 1;
  60.           with PagPtr^.ItemArray[ItemArrIndex] do
  61.           begin
  62.             PKey := Key; ProcDatRef := DataRef;
  63.           end;
  64.         end
  65.       else PP := 0;
  66.     end;
  67.     OK := PP <> 0;
  68.   end;
  69. end;
  70.  
  71. procedure PrevKey(var IdxF       : IndexFile;
  72.                   var ProcDatRef : Integer;
  73.                   var ProcKey                );
  74. var
  75.   PKey   : TaKeyStr absolute ProcKey;
  76.   R      : Integer;
  77.   PagPtr : TaPagePtr;
  78. begin
  79.   with IdxF do
  80.   begin
  81.     if PP = 0 then 
  82.       R := RR
  83.     else
  84.       with Path[PP] do
  85.       begin
  86.         TaGetPage(IdxF,PageRef,PagPtr); 
  87.         ItemArrIndex := ItemArrIndex - 1;
  88.         if ItemArrIndex = 0 then 
  89.           R := PagPtr^.BckwPageRef
  90.         else R := PagPtr^.ItemArray[ItemArrIndex].PageRef;
  91.       end;
  92.     while R <> 0 do
  93.     begin
  94.       TaGetPage(IdxF,R,PagPtr); 
  95.       PP := PP + 1;
  96.       with Path[PP] do
  97.       begin
  98.         PageRef := R; 
  99.         ItemArrIndex := PagPtr^.ItemsOnPage;
  100.       end;
  101.       with PagPtr^ do 
  102.         R := ItemArray[ItemsOnPage].PageRef;
  103.     end;
  104.     if PP <> 0 then
  105.     begin
  106.       while (PP > 1) and (Path[PP].ItemArrIndex = 0) do
  107.       begin
  108.         PP := PP - 1; 
  109.         TaGetPage(IdxF,Path[PP].PageRef,PagPtr);
  110.       end;
  111.       if Path[PP].ItemArrIndex > 0 then
  112.         with PagPtr^.ItemArray[Path[PP].ItemArrIndex] do
  113.         begin
  114.           PKey := Key; 
  115.           ProcDatRef := DataRef;
  116.         end
  117.       else PP := 0;
  118.     end;
  119.     OK := PP <> 0;
  120.   end;
  121. end;
  122.  
  123. procedure TaFindKey(var IdxF       : IndexFile;
  124.                     var ProcDatRef : Integer;
  125.                     var ProcKey                );
  126. var
  127.   PKey    : TaKeyStr absolute ProcKey;
  128.   PrPgRef,
  129.   C,K,L,R : Integer;
  130.   RKey    : TaKeyStr;
  131.   PagPtr  : TaPagePtr;
  132. begin
  133.   with IdxF do
  134.   begin
  135.     TaXKey(PKey,KeyL); 
  136.     OK := false; 
  137.     PP := 0; 
  138.     PrPgRef := RR;
  139.     while (PrPgRef <> 0) and not OK do
  140.     begin
  141.       PP := PP + 1; 
  142.       Path[PP].PageRef := PrPgRef; 
  143.       TaGetPage(IdxF,PrPgRef,PagPtr);
  144.       with PagPtr^ do
  145.       begin
  146.         L := 1; 
  147.         R := ItemsOnPage;
  148.         repeat
  149.           K := (L + R) div 2;
  150.           C := TaCompKeys(PKey,
  151.                           ItemArray[K].Key,
  152.                           0,
  153.                           ItemArray[K].DataRef,
  154.                           AllowDuplKeys        );
  155.           if C <= 0 then 
  156.             R := K - 1;
  157.           if C >= 0 then 
  158.             L := K + 1;
  159.         until R < L;
  160.         if L - R > 1 then
  161.         begin
  162.           ProcDatRef := ItemArray[K].DataRef; 
  163.           R := K; 
  164.           OK := true;
  165.         end;
  166.         if R = 0 then 
  167.           PrPgRef := BckwPageRef
  168.         else PrPgRef := ItemArray[R].PageRef;
  169.       end;
  170.       Path[PP].ItemArrIndex := R;
  171.     end;
  172.     if not OK and (PP > 0) then
  173.     begin
  174.       while (PP > 1) and (Path[PP].ItemArrIndex = 0) do 
  175.         PP := PP - 1;
  176.       if Path[PP].ItemArrIndex = 0 then 
  177.         PP := 0;
  178.     end;
  179.   end;
  180. end;
  181.  
  182. procedure FindKey(var IdxF       : IndexFile;
  183.                   var ProcDatRef : Integer;
  184.                   var ProcKey                );
  185. var
  186.   PKey    : TaKeyStr absolute ProcKey;
  187.   TempKey : TaKeyStr;
  188. begin
  189.   TaFindKey(IdxF,ProcDatRef,PKey);
  190.   if not OK and IdxF.AllowDuplKeys then
  191.   begin
  192.     TempKey := PKey; 
  193.     NextKey(IdxF,ProcDatRef,PKey);
  194.     OK := OK and (PKey = TempKey);
  195.   end;
  196. end;
  197.  
  198. procedure SearchKey(var IdxF : IndexFile;
  199.                     var ProcDatRef : Integer;
  200.                     var ProcKey);
  201. var
  202.   PKey : TaKeyStr absolute ProcKey;
  203. begin
  204.   TaFindKey(IdxF,ProcDatRef,PKey);
  205.   if not OK then 
  206.     NextKey(IdxF,ProcDatRef,PKey);
  207. end;
  208.  
  209.