home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / PRALOC.ZIP / PREALLOC.PAS
Encoding:
Pascal/Delphi Source File  |  1992-10-16  |  6.2 KB  |  227 lines

  1. {$S-,R-,I-,V-,B-,F-}
  2.  
  3. {$IFNDEF Ver40}
  4.   {$F+,O+,A-}     {Force far calls, allow overlays, avoid byte mismatches}
  5. {$ENDIF}
  6.  
  7. {*********************************************************}
  8. {*                  PREALLOC.PAS 5.40                    *}
  9. {*        Copyright (c) TurboPower Software 1990.        *}
  10. {*                 All rights reserved.                  *}
  11. {*********************************************************}
  12.  
  13. unit PreAlloc;
  14.   {-Routines to preallocate disk space for IsamFileBlocks}
  15.  
  16. interface
  17.  
  18. uses
  19.   Dos,
  20.   Filer;
  21.  
  22. const
  23.   DummyFillChar : Byte = 0;
  24.  
  25. procedure PreAllocateFileblock(IFBPtr : IsamFileBlockPtr;
  26.                                TotalDataRecs : LongInt);
  27.   {-Preallocates space for TotalDataRecs in both data and index file. If the
  28.     specified IFBPtr refers to a FileBlock opened in a Net mode, then the
  29.     FileBlock must be locked before making this call. If it is not locked,
  30.     an IsamError of 10398 will be returned. Only appends to data or
  31.     index if the file in question is smaller than the number specified.}
  32.  
  33. procedure PreAllocateVRecFileBlock(IFBPtr : IsamFileBlockPtr;
  34.                                    TotalSections : LongInt;
  35.                                    TotalIndexRecs : LongInt);
  36. {-Preallocates space for the specified number of sections in the data file
  37.  and the specified number of Index reocrds. Only appends if necessary, never
  38.  truncates. If the specified IFBPtr refers to a FileBlock opened in a Net
  39.  mode, then the FileBlock must be locked before making this call. If it is
  40.  not locked locked, an IsamError of 10398 will be returned.}
  41.  
  42. implementation
  43.  
  44.   procedure IsamFlush ( Var F        : IsamFile;
  45.                         Var WithDUP  : Boolean;
  46.                             NetUsed  : Boolean );
  47.  
  48.   Var
  49.     Regs : Registers;
  50.  
  51.   Begin
  52.     With Regs Do Begin
  53.       AH := $45;
  54.       BX := F.Handle;
  55.       MsDos ( Regs );   {DUP the handle}
  56.       If Not Odd (Flags) Then Begin
  57.         WithDUP := True;
  58.         BX := AX;
  59.         AH := $3E;
  60.         MsDos (Regs);   {Close the DUP handle}
  61.         If Odd (Flags) Then Begin
  62.           IsamOK := False;
  63.           If AX = 6 Then Begin
  64.             IsamError := 9904;
  65.           End Else Begin
  66.             IsamError := 10140;
  67.           End;
  68.         End;
  69.       End Else Begin
  70.         WithDUP := False;
  71.         If NetUsed Then Begin
  72.           IsamOK := False;
  73.           IsamError := 10150;
  74.         End Else Begin
  75.           IsamClose ( F );
  76.           If Not IsamOK Then Exit;
  77.           IsamReset ( F, False, False );
  78.         End;
  79.       End;
  80.     End;
  81.   End;
  82.  
  83.   procedure IsamPutDummyBlock(var F : IsamFile; Ref, Len : LongInt);
  84.  
  85.   const
  86.     EmptyArrLen = 1024;
  87.  
  88.   var
  89.     EmptyArr : array[1..EmptyArrLen] of Char;
  90.  
  91.   begin
  92.     FillChar(EmptyArr, SizeOf(EmptyArr), DummyFillChar);
  93.     IsamLongSeek(F, Ref);
  94.     if not IsamOK then
  95.       Exit;
  96.     while Len > EmptyArrLen do begin
  97.       IsamBlockWrite(F, EmptyArr, SizeOf(EmptyArr));
  98.       if not IsamOK then
  99.         Exit;
  100.       Len := Len-LongInt(SizeOf(EmptyArr));
  101.     end;
  102.     IsamBlockWrite(F, EmptyArr, Word(Len));
  103.   end;
  104.  
  105.   procedure ExpandFile(F : IsamFile; NumBytes : LongInt);
  106.     {-Appends NumBytes to the end of the specified file}
  107.   var
  108.     Ref : LongInt;
  109.     SavFill : Byte;
  110.   begin
  111.     if NumBytes = 0 then
  112.       Exit;
  113.     IsamLongSeekEOF(F, Ref);
  114.     if IsamOK then begin
  115.       SavFill := DummyFillChar;
  116.       DummyFillChar := $FF;
  117.       IsamPutDummyBlock(F, Ref, NumBytes);
  118.       DummyFillChar := SavFill;
  119.     end;
  120.   end;
  121.  
  122.   procedure ExpandFileBlock(IFBPtr : IsamFileBlockPtr;
  123.                             NumBytesData : LongInt;
  124.                             NumBytesIndex : LongInt);
  125.   var
  126.     Dummy : Boolean;
  127.   begin
  128.     with IFBPtr^ do begin
  129.       if NumBytesData > 0 then begin
  130.         ExpandFile(DatF, NumBytesData);
  131.         if not IsamOK then
  132.           Exit;
  133.         IsamFlush(DatF, Dummy, (NSP <> nil) and (BTNetSupported <> NoNet));
  134.         if not IsamOK then
  135.           Exit;
  136.       end;
  137.       if (NumBytesIndex > 0) then begin
  138.         ExpandFile(IndF, NumBytesIndex);
  139.         if not IsamOK then
  140.           Exit;
  141.         IsamFlush(IndF, Dummy, (NSP <> nil) and (BTNetSupported <> NoNet));
  142.       end;
  143.     end;
  144.   end;
  145.  
  146.   procedure PreAllocateFileblock(IFBPtr : IsamFileBlockPtr;
  147.                                  TotalDataRecs : LongInt);
  148.  
  149.   var
  150.     NumData,
  151.     FSize,
  152.     NumIndex : LongInt;
  153.  
  154.   begin
  155.     IsamClearOK;
  156.     if BTIsNetFileBlock(IFBPtr) then
  157.       if not BTFileBlockIsLocked(IFBPtr) then begin
  158.         IsamOK := False;
  159.         IsamError := 10398;
  160.         Exit;
  161.       end;
  162.     NumData := TotalDataRecs*BTDatRecordSize(IFBPtr);
  163.     IsamLongSeekEOF(IFBPtr^.DatF, FSize);
  164.     if not IsamOK then
  165.       Exit;
  166.     if NumData > FSize then
  167.       NumData := NumData-FSize
  168.     else
  169.       NumData := 0;
  170.  
  171.     NumIndex := BTKeyRecordSize(IFBPtr);
  172.     if not IsamOK then
  173.       Exit;
  174.     IsamLongSeekEOF(IFBPtr^.IndF, FSize);
  175.     if not IsamOK then
  176.       Exit;
  177.     NumIndex := ((TotalDataRecs div LongInt(PageSize))+1)*NumIndex;
  178.     if NumIndex > FSize then
  179.       NumIndex := NumIndex-FSize
  180.     else
  181.       NumIndex := 0;
  182.  
  183.     ExpandFileBlock(IFBPtr, NumData, NumIndex);
  184.   end;
  185.  
  186.   procedure PreAllocateVRecFileBlock(IFBPtr : IsamFileBlockPtr;
  187.                                      TotalSections : LongInt;
  188.                                      TotalIndexRecs : LongInt);
  189.   var
  190.     NumData,
  191.     FSize,
  192.     NumIndex : LongInt;
  193.  
  194.   begin
  195.     IsamClearOK;
  196.     if BTIsNetFileBlock(IFBPtr) then
  197.       if not BTFileBlockIsLocked(IFBPtr) then begin
  198.         IsamOK := False;
  199.         IsamError := 10398;
  200.         Exit;
  201.       end;
  202.     NumData := TotalSections*BTDatRecordSize(IFBPtr);
  203.     IsamLongSeekEOF(IFBPtr^.DatF, FSize);
  204.     if not IsamOK then
  205.       Exit;
  206.     if NumData > FSize then
  207.       NumData := NumData-FSize
  208.     else
  209.       NumData := 0;
  210.  
  211.     IsamLongSeekEOF(IFBPtr^.IndF, FSize);
  212.     if not IsamOK then
  213.       Exit;
  214.     NumIndex := BTKeyRecordSize(IFBPtr);
  215.     if not IsamOK then
  216.       Exit;
  217.     NumIndex := ((TotalIndexRecs div LongInt(PageSize))+1)*NumIndex;
  218.     if NumIndex > FSize then
  219.       NumIndex := NumIndex-FSize
  220.     else
  221.       NumIndex := 0;
  222.  
  223.     ExpandFileBlock(IFBPtr, NumData, NumIndex);
  224.   end;
  225.  
  226. end.
  227.