Algorithms

Compare two files of txt format

Posted in Algorithms

We will read strings from each file while each of files has a data. And we will compare each string from the first file with corresponding string from the second file. All differences we will write in a memo.

procedure TForm1.Button1Click(Sender: TObject);
var
  First, Second: TextFile;
  Str, Str2: string;
begin
  Memo1.Clear;
  if (Edit1.Text<>'')and(Edit2.Text<>'') then
  begin
    AssignFile(First, Edit1.Text);
    AssignFile(Second, Edit2.Text);
    Reset(First);
    Reset(Second);
    while not EOF(First) do
    begin
      if EOF(Second)=True then
      begin
        Memo1.Lines.Add('*** ATTENTION ***');
        Memo1.Lines.Add('SECOND file has finished
                         but FIRST file has had a data yet.');
        Break;
      end;
      Readln(First, Str);
      Readln(Second, Str2);
      if CompareStr(Str, Str2)<>0 then
      begin
        Memo1.Lines.Add('FIRST  - '+Str);
        Memo1.Lines.Add('SECOND - '+Str2);
        Memo1.Lines.Add(' ');
      end;
    end;
    if EOF(Second)=False then
    begin
      Memo1.Lines.Add('*** ATTENTION ***');
      Memo1.Lines.Add('FIRST file has finished
                       but SECOND file has had a data yet.');
    end;
    CloseFile(First);
    CloseFile(Second);
    if Memo1.Lines.Count=0 then
      Memo1.Lines.Add('FIRST file is identical to SECOND file');
  end;
end;

Compare string with pattern

Posted in Algorithms

If you want to check your string with pattern, which has '?' symbol for changing any symbol, then you may use ComparePattern procedure. First parameter - is a mask, second - is your string and Check variable is result. Result is True, if your string matches with pattern.

procedure ComparePattern(St1, St2: string; var Check: Boolean);
var
  Hlp1, Hlp2: string;
  Pos1: Integer;
begin
  Check:=True;
  while Length(St1)>0 do
  begin
    Pos1:=Pos('?', St1);

    if (Pos1=0) then
    begin
      if CompareStr(St1, St2)<>0 then Check:=False;
      Delete(St1, 1, Length(St1));
      Delete(St2, 1, Length(St2));
    end;

    if (Pos1>0) then
    begin
      Hlp1:=Copy(St1,1,Pos1-1);
      Hlp2:=Copy(St2,1,Pos1-1);
      if CompareStr(Hlp1, Hlp2)<>0 then Check:=False;
      Delete(St1,1,Pos1);
      if Pos1>Length(St2) then Check:=False;
      Delete(St2,1,Pos1);
    end;

    if Check=False then Break;

  end;
  if CompareStr(St1, St2)<>0 then Check:=False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  Check: Boolean;
begin
  ComparePattern(Edit1.text, Edit2.Text, Check);
  if Check=True then Caption:='OK'
  else Caption:='Not OK';
end;

Color to string and vice versa

Posted in Algorithms

Use StringToColor and ColorToString functions. These functions are undocumented and contained in graphics module. Notes: ColorToString - if value has a special name, then this name will be display. For example, $FFFFFF - is a clWhite. StringToColor - you may use name of color here. For example, Str:='clYellow'.

procedure TForm1.Button1Click(Sender: TObject);
var
  Str:string;
begin
  Str:='$AC142F';
  Form1.Color:=StringToColor(Str);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Label1.Caption:=ColorToString($FFFFFF);
end;

Check the validity of dates

Posted in Algorithms

Use StrToDate function and try ... except construction:

procedure TForm1.Button1Click(Sender: TObject);
begin
  try
    StrToDate(MaskEdit1.Text);
    Label2.Caption:='Valid Date';
  except
    on EConvertError do Label2.Caption:='Not Valid Date';
  end;
end;