{
main.pas
This file is part of the Diff.pas sample application.
Info at http://flocke.vssd.de/prog/code/pascal/pasdiff/
Copyright (C) 2005, 2006 Volker Siebert <flocke@vssd.de>
All rights reserved.
}
unit main;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Memo1: TMemo;
OpenDialog1: TOpenDialog;
Memo2: TMemo;
Panel1: TPanel;
Edit1: TEdit;
Button1: TButton;
Edit2: TEdit;
Button2: TButton;
Button3: TButton;
CheckBox1: TCheckBox;
CheckBox2: TCheckBox;
CheckBox3: TCheckBox;
CheckBox4: TCheckBox;
ComboBox1: TComboBox;
Label1: TLabel;
procedure FormShow(Sender: TObject);
procedure FormResize(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
Diff;
const
MaxDisplayedBlockOfText = 2;
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenDialog1.FileName := Edit1.Text;
if OpenDialog1.Execute then
Edit1.Text := OpenDialog1.FileName;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
OpenDialog1.FileName := Edit2.Text;
if OpenDialog1.Execute then
Edit2.Text := OpenDialog1.FileName;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
File1, File2: TStringList;
Differ: TStandardTextComparer;
Loops, idx: integer;
Diff: TDifference;
Line1, Line2: integer;
Fmt: string;
Time1, Time2: Int64;
function PrintLines1(Upto: integer): integer;
var
Cnt: integer;
begin
Result := Memo1.Lines.Count;
if Upto - Line1 > 2 * MaxDisplayedBlockOfText + 1 then
begin
for Cnt := 1 to MaxDisplayedBlockOfText do
begin
Memo1.Lines.Add(Format(Fmt, [Line1 + 1]) + File1[Line1]);
inc(Line1);
end;
Memo1.Lines.Add('...');
Line1 := Upto - MaxDisplayedBlockOfText;
end;
while Line1 < Upto do
begin
Memo1.Lines.Add(Format(Fmt, [Line1 + 1]) + File1[Line1]);
inc(Line1);
end;
Result := Memo1.Lines.Count - Result;
end;
function PrintLines2(Upto: integer): integer;
var
Cnt: integer;
begin
Result := Memo2.Lines.Count;
if Upto - Line2 > 2 * MaxDisplayedBlockOfText + 1 then
begin
for Cnt := 1 to MaxDisplayedBlockOfText do
begin
Memo2.Lines.Add(Format(Fmt, [Line2 + 1]) + File2[Line2]);
inc(Line2);
end;
Memo2.Lines.Add('...');
Line2 := Upto - MaxDisplayedBlockOfText;
end;
while Line2 < Upto do
begin
Memo2.Lines.Add(Format(Fmt, [Line2 + 1]) + File1[Line2]);
inc(Line2);
end;
Result := Memo2.Lines.Count - Result;
end;
procedure AddEmpty(Memo: TCustomMemo; cnt: integer);
begin
while cnt > 0 do
begin
Memo.Lines.Add('');
dec(cnt);
end;
end;
begin
try
File1 := TStringList.Create;
File2 := TStringList.Create;
try
File1.LoadFromFile(Edit1.Text);
File2.LoadFromFile(Edit2.Text);
Differ := TStandardTextComparer.Create;
try
Differ.IgnoreCase := CheckBox1.Checked;
Differ.IgnoreSpaces := CheckBox2.Checked;
Differ.SpacesAsOne := CheckBox3.Checked;
if CheckBox4.Checked then
Differ.Heuristic := 50
else
Differ.Heuristic := 0;
Loops := StrToIntDef(Trim(ComboBox1.Items[ComboBox1.ItemIndex]), 10);
QueryPerformanceCounter(Time1);
for idx := 1 to Loops do
begin
Differ.Clear;
Differ.Compare(File1, File2);
end;
QueryPerformanceCounter(Time2);
Time1 := Time2 - Time1;
QueryPerformanceFrequency(Time2);
Time1 := (Time1 * 1000) div Time2;
Memo1.Lines.BeginUpdate;
Memo2.Lines.BeginUpdate;
try
Memo1.Lines.Clear;
Memo2.Lines.Clear;
Memo1.Lines.Add(Format('Durchläufe: %d', [Loops]));
Memo1.Lines.Add(Format('Unterschiede: %d', [Differ.Count]));
Memo1.Lines.Add('');
Memo1.Lines.Add(Format('Datei: %s', [Edit1.Text]));
Memo1.Lines.Add(Format('Zeilen: %d', [File1.Count]));
Memo1.Lines.Add('');
Memo2.Lines.Add(Format('Laufzeit: %d ms', [Time1]));
Memo2.Lines.Add(Format('Durchläufe/s: %d', [Loops * 1000 div Time1]));
Memo2.Lines.Add('');
Memo2.Lines.Add(Format('Datei: %s', [Edit2.Text]));
Memo2.Lines.Add(Format('Zeilen: %d', [File2.Count]));
Memo2.Lines.Add('');
Memo1.Lines.EndUpdate;
Memo2.Lines.EndUpdate;
Application.ProcessMessages;
Memo1.Update;
Memo2.Update;
Application.ProcessMessages;
Memo1.Lines.BeginUpdate;
Memo2.Lines.BeginUpdate;
Application.ProcessMessages;
Line1 := File1.Count;
Line2 := File2.Count;
idx := 1;
while (Line1 > 9) or (Line2 > 9) do
begin
inc(idx);
Line1 := Line1 div 10;
Line2 := Line2 div 10;
end;
Fmt := Format('%%%d.%dd| ', [idx, idx]);
Line1 := 0;
Line2 := 0;
for idx := 0 to Differ.Count - 1 do
begin
Diff := Differ.Diff[idx];
PrintLines1(Diff.Left.Start);
PrintLines2(Diff.Right.Start);
AddEmpty(Memo2, PrintLines1(Diff.Left.Stop));
AddEmpty(Memo1, PrintLines2(Diff.Right.Stop));
Application.ProcessMessages;
end;
PrintLines1(File1.Count);
PrintLines2(File2.Count);
finally
Memo2.Lines.EndUpdate;
Memo1.Lines.EndUpdate;
end;
finally
Differ.Free;
end;
finally
File2.Free;
File1.Free;
end;
except
on E: Exception do
MessageDlg(E.Message, mtError, [mbOk], 0)
else
raise;
end;
end;
procedure TForm1.FormResize(Sender: TObject);
var
d, w, h: integer;
begin
Panel1.Width := ClientWidth;
d := Memo1.Left;
w := (ClientWidth - 3 * d) div 2;
h := ClientHeight - Memo1.Top - Edit1.Top;
Memo1.Width := w;
Memo2.Left := d + w + d;
Memo2.Width := w;
Memo1.Height := h;
Memo2.Height := h;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Constraints.MinWidth := Width;
Constraints.MinHeight := Height;
ComboBox1.ItemIndex := 0;
end;
end. |