Code Search for Developers
 
 
  

Unit1.pas from Sector-37 at Krugle


Show Unit1.pas syntax highlighted


                       //****************************//
                       //                            //
                       //    Strange Text Replacer    //
                       //                            //
 //********************//****************************//**********************//
 //                                                                          //
 //        This unit is part of a real time 3d space strategy engine         //
 //          (http://www.sector-37.com) but can be used separately           //
 //                                                                          //
 //**************************************************************************//

{
Author  : Alexander Federyakov aka Da Stranger
Version : 1.1.2
Date    : 03 February '2006

Licence:  This library is free software; you can redistribute it and/or modify
          it under the terms this Licence. It is Copyrighted (C) 2004-2006
          by Alexander Federyakov (aka Da Stranger) under the GNU Lesser
          General Public License version 2.1 as published by the Free Software
          Foundation with the following exceptions:

           1) If you used this unit or any part during the development of your
          software, please send me an e-mail to datarget@mail.ru with a link
          to your homepage or to a page, where your soft could be downloaded.
          This is a request for free software and a must for any form of
          commercial.
          You should also state in the appropriate place of you software
          that you used this unit or any part of it during the development of
          your software, name the author (Alexander Federyakov aka Da Stranger)
          and place a link to http://www.sector-37.com .

          2) I have the right to change the license for all future versions of
          this unit and all other units and projects, which are based on it
          or use it in any way.

          3) I am not obliged to publish the source code of any future versions
          of this unit and all other units and projects, which are based on
          it or use it in any way.

          This library is distributed in the hope that it will be useful,
          but WITHOUT ANY WARRANTY; without even the implied warranty of
          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
          Lesser General Public License for more details.

          You should have received a copy of the GNU Lesser General Public
          License along with this library; if not, write to the Free Software
          Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
          02110-1301  USA

Comments: Utility in an "forever beta" state, allows to replace blocks of text
          in a file. I used it wtice to add and replace a "license" section
          in all of my units.

Requirements:  This unit requires TStrangeDragDropDestination component.

Known Bugs and limitations:
          None

Version History:
          v1.0    21 February  '2006  Creation:
          v1.1    23 September '2006  Some bug fixes
                                      Ability to search a dir for *pas files
          v1.1.2  03 February  '2006  Default path is taken from the curren path   

Contacts:
    Web:    http://www.sector-37.com
    E-mail: datarget@mail.ru
    ICQ:    293-963-070
    Odigo:  8077227

}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls,

  StrangeDragDropDestination, StrangeWindowsUtilities;

type
  TForm1 = class(TForm)
    LoadButton: TButton;
    OpenDialog1: TOpenDialog;
    Clear: TButton;
    ExplorerDrop1: TStrangeDragDropDestination;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    assssssssss: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    CheckBox1: TCheckBox;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Edit4: TEdit;
    Label5: TLabel;
    Edit5: TEdit;
    Label6: TLabel;
    Edit6: TEdit;
    Label4: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Edit7: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    Memo1: TMemo;
    Memo2: TMemo;
    FileReplaceListMemo: TMemo;
    StartDirEdit: TEdit;
    Button2: TButton;
    MaskEdit: TEdit;
    procedure LoadButtonClick(Sender: TObject);
    procedure ClearClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure ExplorerDrop1Dropped(Sender: TObject; Files: TStrings; FileCount, X, Y: Integer);
    procedure Edit1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.LoadButtonClick(Sender: TObject);
begin
  if not OpenDialog1.Execute then
    Exit;

  FileReplaceListMemo.Lines.LoadFromFile(OpenDialog1.FileName);
end;

procedure TForm1.ClearClick(Sender: TObject);
begin
  FileReplaceListMemo.Clear;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I, J: Integer;
  Index, changes_number, last_change_number: Integer;
  Temp: TStringList;
begin
  changes_number := 0;
  last_change_number := 0;
  Temp := TStringList.Create;
  if FileReplaceListMemo.Lines.Count = 0 then
    ShowMessage('First Load Files in da list!!!')
  else
    for I := 0 to FileReplaceListMemo.Lines.Count - 1 do
      try
        Temp.LoadFromFile(FileReplaceListMemo.Lines[I]);

        if PageControl1.TabIndex = 0 then
        begin
          if RadioButton2.Checked then
          begin
            for J := StrToInt(Edit4.Text) to StrToInt(Edit5.Text) do
              Temp.Delete(StrToInt(Edit4.Text));
            for J := 0 to Memo1.Lines.Count - 1 do
              Temp.Insert(StrToInt(Edit4.Text) + J, Memo1.Lines[J]);
            Inc(changes_number);
          end
          else
          begin
            for Index := 0 to Temp.Count - 1 do
              if Temp[Index] = Edit6.Text then
              begin
                for J := Index to Index + StrToInt(Edit7.Text) do
                  Temp.Delete(Index);
                for J := 0 to Memo1.Lines.Count - 1 do
                  Temp.Insert(Index + J, Memo1.Lines[J]);
                Inc(changes_number);
                Break;
              end;
          end;
        end;

        if PageControl1.TabIndex = 1 then
        begin
          Index := StrToInt(Edit1.Text);
          last_change_number := changes_number;
          if Index = -1 then
          begin
            for J := 0 to Temp.Count - 1 do
              if Temp[J] = Edit2.Text then
              begin
                Temp[J] := Edit3.Text;
                Inc(changes_number);
                Break;
              end;
          end
          else
          begin
            if Temp[Index] = Edit2.Text then
            begin
              Temp[Index] := Edit3.Text;
              Inc(changes_number);
            end;
          end;
        end;


        if last_change_number <> changes_number then
          Temp.SaveToFile(FileReplaceListMemo.Lines[I]);

      except
        beep;
        if CheckBox1.Checked then
          Continue
        else
        begin
          Temp.Free;
          ShowMessage('Some Error Occured!!! Process aborted');
          raise;
          Exit;
        end;
      end;
  ShowMessage('Done! Changes made: ' + IntToStr(changes_number));
end;

procedure TForm1.ExplorerDrop1Dropped(Sender: TObject; Files: TStrings; FileCount, X, Y: Integer);
begin
  if Files.Count = 1 then
  begin
    if AnsiLowerCase(ExtractFileExt(Files[0])) = '.txt' then
      FileReplaceListMemo.Lines.LoadFromFile(Files[0]);
  end
  else
  begin
    FileReplaceListMemo.Lines := Files;
  end;
end;

procedure TForm1.Edit1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if ssRight in Shift then
    Edit1.Text := '-1';
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  label6.Enabled := True;
  label5.Enabled := True;
  Edit4.Enabled := True;
  Edit5.Enabled := True;

  label4.Enabled := False;
  label8.Enabled := False;
  label9.Enabled := False;
  Edit6.Enabled := False;
  Edit7.Enabled := False;
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
  label6.Enabled := False;
  label5.Enabled := False;
  Edit4.Enabled := False;
  Edit5.Enabled := False;

  label4.Enabled := True;
  label8.Enabled := True;
  label9.Enabled := True;
  Edit6.Enabled := True;
  Edit7.Enabled := True;
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  FileReplaceListMemo.Lines.Clear;
  ScanDir(FileReplaceListMemo.Lines, StartDirEdit.Text, MaskEdit.Text, True, True, True, True);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StartDirEdit.Text := ExtractFilePath(Application.ExeName);
end;

end.





See more files for this project here

Sector-37

Sector-37 (real time 3D cosmic strategy)

Project homepage: http://sourceforge.net/projects/sector37
Programming language(s): Pascal
License: lgpl21

  StrangeTextReplacer.dpr
  StrangeTextReplacer.res
  Unit1.dfm
  Unit1.pas
  _Cleanup.bat