Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing two files

Status
Not open for further replies.

Valcor

IS-IT--Management
Mar 22, 2001
25
US
Hi All,

I'm trying to create a small source integrity program to replace the one we are currently using as it is way out of date, and doesn't run on all the programming computers any more (doesn't support XP, or Win2k). I have MD5 checks sorted, now I'm trying to find a method of comparing files, and creating a diff file with all the changes (can be outputted to screen I guess as well). My first idea was if the MD5 hash was different with the two files, to load them into list boxes, and then compare each line. This is okay, until you add just one line in the middle of the code, and the rest then becomes different, when all you needed to says was "on line x: newcode". I was considering memo fields, but again, cannot work out a viable solution there. Has anybody got any ideas on how I could go about doing this?

Thanks in advanced --
Jon
 
Make each line in the files unique by adding labels in front of them. For example procedures should have the procedure name and procedure line number added.

Then search file two for each line in file one, if not found mark it as changed. Do the same the opposite direction.

Make a type containing the string, line number and a 'changed' flag.
 
I think I'm understanding that... so an example would be...

Code:
procedure TFrmMain.MainProc;
begin
 {funny stuff here}
end;

That would appear like this in the lookup...

Code:
mp1: procedure TFrmMain.MainProc;
mp2: begin
mp3:   {funny stuff here}
mp4: end;

That right? How would I go about doing that? --
Jon
 
I would use:

procedure TFrmMain.MainProc;
TFrmMain.MainProc1begin
TFrmMain.MainProc2 {funny stuff here}
TFrmMain.MainProc3end;

Actually only lines that are equal in the procedures should have a number added, the numbers are not good id's:

procedure TFrmMain.MainProc;
TFrmMain.MainProc1begin
TFrmMain.MainProc {funny stuff here}
TFrmMain.MainProc1end;
TFrmMain.MainProcif not fun then
TFrmMain.MainProc2begin
TFrmMain.MainProc {not funny stuff here}
TFrmMain.MainProc2end;

How to add labels? Try this in the read loop(will also copy comments, should change it):

if copy(line,1,9)='procedure' then label:=copy(line,11,length(line)-10) else line:=label+line;

Actually all functions, procedures and so on should be mixed into single strings before comparing, you would need no 'labels', but the strings would become very long.

 
Hrm... I think I get it... so it'd probably be easier to dump both files into seperate list boxes, go through the original file, and for each procedure, function, and so on, create a label, and a numeric for that function (etc). Repeat on the second list too, then compare each function (etc) individually? My brain is frazzled ;) I'm not entirely sure how I'd go about doing that... but I think I have a general gist of what you are saying.
--
Jon
 
The idea is to not compare each procedure, but using the simple check:

Code:
for row1:=1 to lastrow1 do
begin
  found:=false;
  for row2:=1 to lastrow2 do
    if file1[row1].data=file2[row2].data then found:=true;
  if not found then file1[row1].changed:=true;
end;

Then the opposite direction. The result are diff-lines marked in both arrays(lists).

But in order to do it that simple, all lines in each file must be unique. The labels can be added during reading the files into arrays. The 'label-engine' maybe should have some 'knowledge' about source code(begin-begin-end-end structures, procedures inside procedures etc), so the labels helps to point out the real changes. The labelling/numbering should make the same result to code in both files individually.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top