(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTTextReplace;

interface

uses SysUtils,Windows, Messages, Classes,AHMSystemClass;

type
  TAHMBeforeWrite = procedure(Sender: TObject;var Line:String;LineNo : Integer;
                         var WriteLine: Boolean) of object;
  TAHMTextReplace = class(TAHMSystemsComponent)
  private
    FLineNumber : longint;
    FBeforeWrite : TAHMBeforeWrite;
    FFileTo,FFileFrom,FLine: string;
    FNoOfLinesOnly : longint;
    FBusyWriting,FUseNoOfLines : boolean;
    FAfterExecute: TNotifyevent;
  protected
  public
    constructor Create(AOwner : TComponent); override;
    Function Execute : Boolean;
    Procedure AddNewString(MyNewString : String);
    property LineNo : LongInt read FLineNumber;
    property Line : String read FLine;
  published
    property FileFrom : String read FFileFrom write FFileFrom;
    property FileTo  : String read FFileTo write FFileTo;
    property BeforeWrite : TAHMBeforeWrite read FBeforeWrite write FBeforeWrite;
    property UseNoOfLines : Boolean read FUseNoOfLines write FUseNoOfLines default false;
    Property NoOfLinesOnly : Longint read FNoOfLinesOnly write FNoOfLinesOnly;
    property AfterExecute : TNotifyevent read FAfterExecute write FAfterExecute;
 end;

implementation
uses Forms;
var af,bf : Textfile;

constructor TAHMTextReplace.Create(AOwner : TComponent);
begin
  inherited Create(AOwner);
end;

procedure TAHMTextReplace.AddNewString(MyNewString : String);
begin
  if FBusyWriting then writeln(bf,MyNewString);
end;

Function TAHMTextReplace.Execute : Boolean;
begin
  result:=false;
  if (FFileTo<>'') and (FFileFrom<>'') then
  begin
    FLineNumber:=1;
    assignfile(af,FFileFrom);
    reset(af);
    assignfile(bf,FFileTo);
    rewrite(bf);
    while not eof(af) do
    begin
      FBusyWriting:=true;
      readln(af,FLine);
      Application.processmessages;
      if (FUseNoOfLines and (FLineNumber<=FNoOfLinesOnly)) or (FUseNoOfLines=false) then
      begin
        if assigned(FBeforeWrite) then FBeforeWrite(self,FLine,FLineNumber,FBusyWriting);
        if FBusyWriting then writeln(bf,FLine);
        inc(FLineNumber);
        result:=true;
      end;
    end;
    closefile(bf);
    closefile(af);
    if assigned(FAfterExecute) then FAfterExecute(Self);
  end else raise Exception.Create('Filename not supplied');

end;

end.
