home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / library / dos / install / sdefs / sdefs.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1988-12-08  |  9.8 KB  |  296 lines

  1.  
  2. Unit sDefs; { TPU for easy contruction of default installation programs.}
  3.  
  4.             { The program you plan to use SDEFS on must have a constant
  5.               placed were it can serve as a locator for your default
  6.               variable block.  The installation program you build with
  7.               this TPU must have the exact same declarations as the target
  8.               program.  See the SETDEFS and DEMO programs included in this
  9.               archive.
  10.             }
  11.                  {This source code and software is released to the public}
  12.                  {domain for non-commercial users, 1988 John Majkrzak    }
  13.                  {All Rights Reserved.  Request permission for commercial}
  14.                  {use or reprint.                                        }
  15.                  {  John Majkrzak           CIS 76617,264                }
  16.                  {  1880 Todd Dr.           Ph# (612)636-9761            }
  17.                  {  Arden Hills, MN 55112                                }
  18.  
  19.   Interface
  20.     Uses
  21.       DOS;
  22.  
  23.     VAR
  24.       ErSt:       String;     { << Contains error report if FALSE return }
  25.                               { from a function seen below.}
  26.  
  27.       Ptr2DefRec: Pointer;    { << This pointer is set after the call to  }
  28.                               { DefRecFound().  A record pointer could    }
  29.                               { point to this, otherwise use the r/write  }
  30.                               { memory block procedures as in SETDEFS.    }
  31.  
  32.  
  33.  
  34.   Function  PrgFileLoaded(fName: String): Boolean;
  35.   Function DefRecFound(idPtr: Pointer; idSize: byte): Boolean;
  36.   Procedure ReadMemBlk(ToBlk: Pointer; BlkSize: Word);
  37.   Procedure WriteMemBlk(FromBlk: Pointer; BlkSize: Word);
  38.   Function  PrgFileSaved(fName: String): Boolean;
  39.  
  40.   Implementation
  41.  
  42.     Const
  43.       MaxVal        = 32766; {32766 value for block r/write and search.}
  44.     Type
  45.       PtrAryType    = Array[1..$FFFF] of byte; {Subscripts for block area.}
  46.     Var
  47.       J:              Word;       {Global iteration variable.}
  48.       fRec:           SearchRec;
  49.       FileSize:       LongInt;    {Program size.}
  50.       cFilePos:       LongInt;
  51.       AmtRemain:      LongInt;    {Amount of block left to process.}
  52.       cFilePtr:       ^PtrAryType;
  53.       bResult:        Integer;
  54.       bSize:          Integer;    {Block size currently being processed.}
  55.       bPtr:           Pointer;
  56.       ioError:        Integer;
  57.       PrgFtime:       LongInt;    {Original time gets rewritten to new file.}
  58.       FileStart:      Pointer;
  59.       WorkFile:       File;
  60.  
  61.   Function ExistR(Var InFile: File; Name: String): Boolean;
  62.     Var
  63.       Dummy: Word;
  64.     Begin                 {$I-}
  65.     Assign(InFile,Name);
  66.     Reset (InFile, 1);
  67.     If ioResult <> 0 then begin
  68.         Close(InFile);    {$I+}
  69.         ExistR:= False;
  70.       end
  71.     else ExistR:= True;
  72.     Dummy:= ioResult;
  73.   end;
  74.  
  75.   Function ExistW(Var WorkFile: File; Name: String): Boolean;
  76.     Var
  77.       Dummy: Word;
  78.     Begin
  79.     Assign (WorkFile,Name);  {$I-}
  80.     Rewrite(WorkFile, 1);
  81.     If ioResult <> 0 then begin
  82.         Close(WorkFile);     {$I+}
  83.         ExistW:= False;
  84.       end
  85.     else ExistW:= True;
  86.     Dummy:= ioResult;
  87.   end;
  88.  
  89.   Function CloseFile(VAR WorkFile: File; ioError: Integer): Boolean;
  90.     Begin            {$I-}
  91.     Close(WorkFile); {$I+}
  92.     ioError:= ioResult;
  93.     If ioError = 0 then CloseFile:= True else CloseFile:= False;
  94.   end;
  95.  
  96.   Function AnError(VAR ioError: Integer): Boolean;
  97.     Var
  98.       Dummy: Word;
  99.     Begin
  100.     ioError:= ioResult;
  101.     AnError:= False;
  102.     If ioError = 0 then EXIT;
  103.     AnError:= True;  {$I-}
  104.     Close(WorkFile); {$I+}
  105.     Dummy:= ioResult;
  106.   end;
  107.  
  108.   Function bRead(VAR bFile:    File;
  109.                       bPtr:    Pointer;
  110.                       bSize:   Integer;
  111.                  VAR  bResult: Integer): Boolean;
  112.     Begin
  113.     bRead:= False;                           {$I-}
  114.     BlockRead(bFile, bPtr^, bSize, bResult); {$I+}
  115.     If AnError(ioError) then EXIT;
  116.     bRead:= True;
  117.   end;
  118.  
  119.   Function bWrite(VAR bFile:    File;
  120.                        bPtr:    Pointer;
  121.                        bSize:   Integer;
  122.                   VAR  bResult: Integer): Boolean;
  123.     Begin
  124.     bResult:= 0;
  125.     bWrite:= False;                           {$I-}
  126.     BlockWrite(bFile, bPtr^, bSize, bResult); {$I+}
  127.     If AnError(ioError) OR (bResult <> bSize) then EXIT;
  128.     bWrite:= True;
  129.   end;
  130.  
  131.   Function PrgFileLoaded(fName: String): Boolean;
  132.     Var
  133.       DumTF: Boolean;
  134.     Begin
  135.     PrgFileLoaded:= False;
  136.     FindFirst(fName, AnyFile, fRec);
  137.     If DosError <> 0
  138.       then begin ErSt:= 'Unable to locate file.'; EXIT; end;
  139.     If NOT ExistR(WorkFile, fName)
  140.       then begin ErSt:= 'Unable to open file for read.'; EXIT; end;
  141.     GetFtime(WorkFile, PrgFTime);
  142.     FileSize:= fRec.Size;
  143.     bSize:= 4096;{Nice round figure}
  144.     cFilePos:= 0;
  145.     GetMem(FileStart, bSize);
  146.     Inc(cFilePos,bSize);
  147.     While cFilePos < FileSize do begin
  148.       If MaxAvail < bSize then begin
  149.         DumTF:= CloseFile(WorkFile, ioError);
  150.         ErSt:= 'Not enough room on heap.';
  151.         EXIT;
  152.       end;
  153.       GetMem(cFilePtr, bSize);
  154.       Inc(cFilePos,bSize);
  155.     end;
  156.     bSize:= MaxVal;
  157.     cFilePos:= 0;
  158.     cFilePtr:= FileStart;
  159.     bResult:= 0;
  160.     Repeat
  161.       bPtr:= @cFilePtr^[Succ(bResult)];
  162.       cFilePtr:= bPtr;
  163.       If NOT bRead(WorkFile, bPtr, bSize, bResult)
  164.         then begin ErSt:= 'Unable to read file.'; EXIT; end;
  165.       Inc(cFilePos, bResult);
  166.     until (bResult <> bSize) OR (cFilePos >= FileSize);
  167.     If cFilePos <> FileSize
  168.       then begin ErSt:= 'Error, incorrect file read.'; EXIT; end;
  169.     If NOT CloseFile(WorkFile, ioError)
  170.       then begin ErSt:= 'Unable to close file after read.'; EXIT; end;
  171.     PrgFileLoaded:= True;
  172.   end;
  173.  
  174.   Function PrgFileSaved(fName: String): Boolean;
  175.  
  176.     Begin
  177.     PrgFileSaved:= False;
  178.     If NOT ExistW(WorkFile, fName)
  179.       then begin ErSt:= 'Unable to open file for save.'; EXIT; end;
  180.     cFilePos:= 0;
  181.     cFilePtr:= FileStart;
  182.     bResult:= 0;
  183.     Repeat
  184.       AmtRemain:= FileSize - cFilePos;
  185.       If AmtRemain > MaxVal then bSize:= MaxVal else bSize:= AmtRemain;
  186.       bPtr:= @cFilePtr^[Succ(bResult)];
  187.       cFilePtr:= bPtr;
  188.       If NOT bWrite(WorkFile, bPtr, bSize, bResult)
  189.         then begin ErSt:= 'Unable to write file.'; EXIT; end;
  190.       Inc(cFilePos,bResult);
  191.     until (cFilePos >= FileSize);
  192.     If cFilePos <> FileSize
  193.       then begin ErSt:= 'Error, incorrect file save.'; EXIT; end;
  194.     SetFtime(WorkFile, PrgFtime);
  195.     If NOT CloseFile(WorkFile, ioError)
  196.       then begin ErSt:= 'Error on close after write.'; EXIT; end;
  197.     PrgFileSaved:= True;
  198.   end;
  199.  
  200.   (* -----------------------------------------------------------------------
  201.     FUNCTION BlockCompare(): Boolean, true if both blocks same.
  202.       PARAMETERS
  203.         Blk1, Blk2:  Pointers to start of blocks we are comparing.
  204.         BlkSize:     Size of blocks being compared.
  205.       CALLS
  206.         NONE.
  207.   ----------------------------------------------------------------------- *)
  208.   Function BlockCompare(Blk1, Blk2: Pointer; BlkSize: Word): Boolean;
  209.     Type
  210.       Ary = Array[1..4096] of byte; {4096 is an arbitrary maximum.}
  211.     Var
  212.       J: Word;
  213.       P1, P2: ^Ary;
  214.     Begin
  215.     P1:= Blk1; {Point the dynamic arrays to the blocks to be compared.}
  216.     P2:= Blk2; {Subscripts sure are handy.}
  217.     BlockCompare:= False;
  218.     For J:= 1 to BlkSize do If P1^[J] <> P2^[J] then EXIT;
  219.     BlockCompare:= True;
  220.   end;
  221.  
  222.   (* -----------------------------------------------------------------------
  223.     FUNCTION BlkFound(): Boolean, true if found what we were looking for.
  224.       Searches for a block within a block.
  225.       PARAMETERS
  226.         sBlk:  Start of block we are looking for.
  227.         sSize: Size of search block area.
  228.         bBlk:  Start of block we are looking in.
  229.         bSize: Size of block we are looking in.
  230.         InPtr: Returns pointer to what we want.
  231.       CALLS
  232.         BlockCompare;
  233.   ----------------------------------------------------------------------- *)
  234.  
  235.   Function BlkFound(    sBlk:  Pointer; sSize: Word;
  236.                         bBlk:  Pointer; bSize: Word;
  237.                  VAR InPtr: Pointer): Boolean;
  238.     Type
  239.       PtrAryType    = Array[$1..$FFFF] of byte;
  240.     Var
  241.       J: Word;
  242.       PtrAry: ^PtrAryType;
  243.     Begin
  244.     J:= 0;
  245.     PtrAry:= bBlk; {Search area gets subscripts.}
  246.     BlkFound:= True;
  247.     While J < bSize do begin
  248.       Inc(J);
  249.       InPtr:= @PtrAry^[J];
  250.       If BlockCompare(sBlk, InPtr, sSize) then EXIT;
  251.     end;
  252.     BlkFound:= False;
  253.   end;
  254.  
  255.   (* -----------------------------------------------------------------------
  256.     FUNCTION DefRecFound(): Boolean, true if found what we were looking for.
  257.       Searches for a block within a block.
  258.       PARAMETERS
  259.         idPtr: The identification pointer.  You might use `@MyString'.
  260.         idSize: Use SizeOf() to satisfy this parameter whenever possible.
  261.       CALLS
  262.         BlockFound;
  263.   ----------------------------------------------------------------------- *)
  264.  
  265.   Function DefRecFound(idPtr: Pointer; idSize: byte): Boolean;
  266.     Var
  267.       AmtRemain: LongInt;
  268.     Begin
  269.     DefRecFound:= True;
  270.     cFilePos:= 0;
  271.     cFilePtr:= FileStart;
  272.     Repeat
  273.       AmtRemain:= FileSize - cFilePos;
  274.       If AmtRemain < MaxVal then bSize:= AmtRemain else bSize:= MaxVal;
  275.       If BlkFound(idPtr, idSize,
  276.                   cFilePtr, bSize,
  277.                   Ptr2DefRec) then EXIT;
  278.       bPtr:= @cFilePtr^[Succ(bSize)];
  279.       cFilePtr:= bPtr;
  280.     Until AmtRemain < MaxVal;
  281.     ErSt:= 'Default record not found.';
  282.     DefRecFound:= False;
  283.   end;
  284.  
  285.   {$F+}
  286.   Procedure ReadMemBlk(ToBlk: Pointer; BlkSize: Word);
  287.     Begin
  288.     Move(Ptr2DefRec^, ToBlk^, BlkSize);
  289.   end;
  290.  
  291.   Procedure WriteMemBlk(FromBlk: Pointer; BlkSize: Word);
  292.     Begin
  293.     Move(FromBlk^, Ptr2DefRec^, BlkSize);
  294.   end;
  295.  
  296. END.