(***********************************************************) 
(*                                                         *) 
(*                TURBO-access version 1.00                *)
(*                                                         *) 
(*                       ADDKEY module                     *) 
(*                                                         *) 
(*                   Copyright (C) 1984 by                 *) 
(*                        BORLAND Int.                     *) 
(*                                                         *) 
(***********************************************************)
 
(*$A+,R-*)

procedure AddKey(var IdxF       : IndexFile;
                 var ProcDatRef : Integer;
                 var ProcKey                );
var
  PKey      : TaKeyStr absolute ProcKey;
  PrPgRef1,
  PrPgRef2,
  C,I,K,L   : Integer;
  PassUp    : Boolean;
  PagePtr1,
  PagePtr2  : TaPagePtr;
  ProcItem1,
  ProcItem2 : TaItem;
 
(*$A-*)

procedure Search(PrPgRef1 : Integer);
var
  R : Integer;
 
(*$A+*)

procedure Insert;
begin
  TaGetPage(IdxF,PrPgRef1,PagePtr1);
  with PagePtr1^ do
  begin
    if ItemsOnPage < PageSize then
    begin
      ItemsOnPage := ItemsOnPage + 1;
      for I := ItemsOnPage downto R + 2 do 
        ItemArray[I] := ItemArray[I - 1];
      ItemArray[R + 1] := ProcItem1; 
      PassUp := false;
    end
    else
    begin
      TaNewPage(IdxF,PrPgRef2,PagePtr2);
      if R <= Order then
      begin
        if R = Order then 
          ProcItem2 := ProcItem1
        else
        begin
          ProcItem2 := ItemArray[Order];
          for I := Order downto R + 2 do 
            ItemArray[I] := ItemArray[I - 1];
          ItemArray[R + 1] := ProcItem1;
        end;
        for I := 1 to Order do 
          PagePtr2^.ItemArray[I] := ItemArray[I + Order];
      end
      else
      begin
        R := R - Order; 
        ProcItem2 := ItemArray[Order + 1];
        for I := 1 to R - 1 do 
          PagePtr2^.ItemArray[I] := ItemArray[I + Order + 1];
        PagePtr2^.ItemArray[R] := ProcItem1;
        for I := R + 1 to Order do 
          PagePtr2^.ItemArray[I] := ItemArray[I + Order];
      end;
      ItemsOnPage := Order;
      PagePtr2^.ItemsOnPage := Order; 
      PagePtr2^.BckwPageRef := ProcItem2.PageRef; 
      ProcItem2.PageRef := PrPgRef2;
      ProcItem1 := ProcItem2; 
      TaUpdatePage(PagePtr2);
    end;
  end;
  TaUpdatePage(PagePtr1);
end;

begin
  if PrPgRef1 = 0 then
  begin
    PassUp := true;
    with ProcItem1 do
    begin
      Key := PKey; 
      DataRef := ProcDatRef; 
      PageRef := 0;
    end;
  end
  else
  begin
    TaGetPage(IdxF,PrPgRef1,PagePtr1);
    with PagePtr1^ do
    begin
      L := 1; 
      R := ItemsOnPage;
      repeat
        K := (L + R) div 2;
        C := TaCompKeys(PKey,
                        ItemArray[K].Key,
                        ProcDatRef,
                        ItemArray[K].DataRef,
                        IdxF.AllowDuplKeys   );
        if C <= 0 then 
          R := K - 1;
        if C >= 0 then 
          L := K + 1;
      until R < L;
      if L - R > 1 then
      begin
        OK := false; 
        PassUp := false;
      end
      else
      begin
	if R = 0 then 
          Search(BckwPageRef)
        else Search(ItemArray[R].PageRef);
	if PassUp then 
          Insert;
      end;
    end;
  end;
end;

begin
  with IdxF do
  begin
    TaXKey(PKey,KeyL); 
    OK := true; 
    Search(RR);
    if PassUp then
    begin
      PrPgRef1 := RR; 
      TaNewPage(IdxF,RR,PagePtr1);
      with PagePtr1^ do
      begin
        ItemsOnPage := 1; 
        BckwPageRef := PrPgRef1; 
        ItemArray[1] := ProcItem1;
      end;
      TaUpdatePage(PagePtr1);
    end;
  end;
end;

