home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Simtel MSDOS 1992 September
/
Simtel20_Sept92.cdr
/
msdos
/
txtutl
/
filecmp.arc
/
FILECOMP.PAS
< prev
next >
Wrap
Pascal/Delphi Source File
|
1988-03-03
|
5KB
|
180 lines
program filecomp;
{$I pfilenam}
LABEL FIN2;
CONST
BufSize = 16383; {buffer size -1}
NumBlks = 128; {num 128 byte blocks in buffer}
Blk2N = 14; {buffer size as 2**N}
TYPE
bigary = Array[0..BufSize] of byte;
str2 = String[2];
VAR
file1, file2 : file;
ary1, ary2 : bigary;
SizeH1, SizeL1, SizeH2, SizeL2 : Integer;
ReadCounter : Integer;
NumReads, ExtraBytes : Integer;
PrintCount : Byte;
DifCount : Integer;
Quitx : Boolean;
Name1 : Str8;
Vers1 : Str4;
FUNCTION OpenFile(var filex:file; var sizeH,sizeL:Integer; N:byte):boolean;
LABEL Ofin;
VAR
filename : str80;
DandQ : str64;
fname : str8;
fvers : str4;
filelcl : file of byte;
xx, yy : byte;
BEGIN
OpenFile:=False;
xx := 33;
IF ParamCount>=N THEN
BEGIN
filename:=ParamStr(N);
Write ('NAME OF FILE TO COMPARE IS : ');
WriteLN(filename);
END
ELSE
BEGIN
Write ('ENTER NAME OF FILE TO COMPARE : ');
ReadLN (filename);
END;
yy := WhereY-1;
if length(filename)+xx > 80 then YY:=YY-1;
IF filename='' THEN GOTO Ofin;
if not ParseFileName(filename, DandQ, Fname, Fvers) then
BEGIN
WriteLn('FILE ', filename, ' INVALID'); GOTO Ofin;
END;
if (Fname='*') and (Fvers='') then Fvers:=Vers1;
if Fname='*' then Fname:=Name1;
if Fvers='.*' then Fvers:=Vers1;
FileName := DandQ + Fname + Fvers;
Name1:=Fname;
Vers1:=Fvers;
GotoXY(XX,YY); WriteLN(filename);
Assign(filelcl,Filename);
{$I-}
Reset(Filelcl);
{$I+}
IF IOresult<>0 THEN
BEGIN
WriteLN('FILE ', filename, ' NOT FOUND'); GOTO Ofin;
END;
SizeL:=FileSize(fileLCL) AND $FF;
Close(filelcl);
Assign(filex,filename); Reset(filex);
SizeH:=FileSize(Filex); IF SizeL<>0 THEN SizeH:=SizeH-1; SizeH:=SizeH div 2;
OpenFile:=True;
Ofin: END {OpenFile};
Procedure GetNumReads;
VAR L,H:Integer;
BEGIN
{first set L,H to smaller of size of File1 or File2}
L:=SizeL1; H:=SizeH1;
IF (SizeH2<SizeH1) OR
((SizeH2=SizeH1) AND (SizeL2<SizeL1)) THEN
BEGIN L:=SizeL2; H:=SizeH2; END;
{NumReads is number of 2**14 byte reads,
however in the last read, only ExtraBytes are valid}
H := H + H; {num of 128 byte blocks in the file}
NumReads := H Div NumBlks;
ExtraBytes := (H Mod NumBlks)*128 + L;
IF ExtraBytes > 0 THEN NumReads:=NumReads+1;
END {GetNumReads};
Function HexStr(N:Byte):Str2;
Const Hexary : Array[0..15] of Char = '0123456789ABCDEF';
Var I:Byte;
BEGIN
HexStr:=Hexary[(N shr 4) AND $0F] + Hexary[N AND $0F];
END {HexStr};
Procedure PrintDifference(CharNum:Integer);
VAR ch : char;
Temp : Integer;
BEGIN
{ReadCounter is block number (1..n)
CharNum is count within block (0..16383)}
IF NOT Quitx THEN
BEGIN
Temp:=(ReadCounter-1) shr (16-Blk2N);
Write( HexStr(Temp shr 8)+Hexstr(Temp AND $FF), ':' );
Temp:=((ReadCounter-1) shl Blk2N) + CharNum;
Write( HexStr(Temp shr 8)+Hexstr(Temp AND $FF) );
Writeln(' ', HexStr(Ary1[CharNum]),' ', HexStr(Ary2[CharNum]));
PrintCount:=PrintCount+1;
DifCount :=DifCount +1;
IF PrintCount=15 THEN
BEGIN
PrintCount:=0;
repeat
Write('Continue (Y/N) ? '); Read(kbd,ch); Writeln(ch);
ch:=Upcase(ch);
until ch in ['N','Q','Y'];
QuitX := ch in ['N','Q'];
END;
END;
END {PrintDifference};
Procedure HandleOneRead;
LABEL HORfin;
VAR I,J,N : Integer;
BEGIN
N:=NumBlks; J:=BufSize+1;;
IF ReadCounter=NumReads THEN IF ExtraBytes<>0 THEN
BEGIN
N := (ExtraBytes-1) DIV 128 + 1;
J:=ExtraBytes;
END;
BlockRead(file1,ary1,N);
BlockRead(file2,ary2,N);
J:=J-1;
FOR I:=0 TO J DO
IF Ary1[I]<>Ary2[I] THEN
begin
PrintDifference(I);
IF Quitx THEN goto HORfin;
end;
HORfin: END {HandleOneRead};
BEGIN
Name1:='*'; Vers1:='*';
PrintCount:=0; DifCount:=0; QuitX:=False;
WriteLn('Fast File Compare, by Richard Marks');
IF OpenFile(file1,SizeH1,SizeL1,1) THEN
BEGIN
IF OpenFile(file2,SizeH2,SizeL2,2) THEN
BEGIN
IF (SizeH1>SizeH2) OR
((SizeH1=SizeH2) AND (SizeL1>SizeL2)) THEN
Writeln('File 1 longer than File 2');
IF (SizeH2>SizeH1) OR
((SizeH2=SizeH1) AND (SizeL2>SizeL1)) THEN
Writeln('File 2 longer than File 1');
GetNumReads;
FOR ReadCounter:=1 TO NumReads DO
BEGIN HandleOneRead; IF Quitx THEN GOTO FIN2; END;
Writeln(''); Writeln(DifCount, ' Differences.');
FIN2: Close(File2);
END;
Close(File1);
END;
{set ErrorLevel to Difcount}
Halt(DifCount);
END .