//Advanced Delphi Systems Code: ads_SearchReplace
unit ads_SearchReplace;
{Copyright(c)2016 Advanced Delphi Systems

 Richard Maley
 Advanced Delphi Systems
 12613 Maidens Bower Drive
 Potomac, MD 20854 USA
 phone 301-840-1554
 dickmaley@advdelphisys.com

 The code herein can be used or modified by anyone.  Please retain references
 to Richard Maley at Advanced Delphi Systems.  If you make improvements to the
 code please send your improvements to dickmaley@advdelphisys.com so that the
 entire Delphi community can benefit.  All comments are welcome.
}
(*
UnitIndex Master Index Implementation Section Download Units
Description: ads_SearchReplace.pas
This unit contains the following routines.

FileSearchReplace  StrSearchReplace   ToPoundChar  

*)
interface

Function FileSearchReplace(FileSource,FileDest,StrFrom,StrTo:String;out errMsg: String;ShowMsgs:Boolean): Boolean;
Function StrSearchReplace(StrSource,StrFrom,StrTo:String;out errMsg: String;ShowMsgs:Boolean): String;
function ToPoundChar(s: String): String;

implementation

Uses
  Dialogs,SysUtils,ads_File;

//
Unit Description UnitIndex Master Index
Function FileSearchReplace(FileSource,FileDest,StrFrom,StrTo:String;out errMsg: String;ShowMsgs:Boolean): Boolean;
Var
  sgFile     : String;
  sgFileFrom : String;
  sgFileTo   : String;
  sgFrom     : String;
  sgTo       : String;
begin
  Result     :=False;
  Try
    errMsg     := '';
    sgFileFrom := FileSource;
    sgFileTo   := FileDest;
    sgFrom     := StrFrom;
    sgTo       := StrTo;
    If sgFileFrom='' Then
    Begin
      errMsg:='Select a Source file first!';
      If ShowMsgs Then ShowMessage(errMsg);
      Exit;
    End;
    If Not FileExists(sgFileFrom) Then
    Begin
      errMsg:='Source file does not exist!';
      If ShowMsgs Then ShowMessage(errMsg);
      Exit;
    End;
    If sgFileTo='' Then
    Begin
      errMsg:='Select a Destination file!';
      If ShowMsgs Then ShowMessage(errMsg);
      Exit;
    End;
    Try
      sgFile := FileToStr(sgFileFrom);
    Except
      errMsg:='There was a problem opening '+sgFileFrom;
      If ShowMsgs Then ShowMessage(errMsg);
    End;
    sgfile:=StrSearchReplace(sgFile,sgFrom,SgTo,errMsg,ShowMsgs);
    If errMsg<>'' Then Exit;
    If sgFileFrom=sgFileTo Then CopyFile(sgFileFrom,sgFileFrom+'.'+FormatDateTime('yymmddhhnnss',now()));
    Try
    If FileExists(sgFileTo) Then CopyFile(sgFileTo,sgFileTo+'.'+FormatDateTime('yymmddhhnnss',now()));
     StrToFile(sgfile,sgFileTo);
     errMsg:='';
     If ShowMsgs Then ShowMessage('Done');
    Except
      errMsg:='There was a problem saving '+sgFileTo;
      If ShowMsgs Then ShowMessage(errMsg);
    End;
    Result:=True;
  Except
    Result:=False;
  End;
end;

//
Unit Description UnitIndex Master Index
Function StrSearchReplace(StrSource,StrFrom,StrTo:String;out errMsg: String;ShowMsgs:Boolean): String;
Var
  sgFrom     : String;
  sgTo       : String;
begin
  Result     :=StrSource;
  Try
    errMsg     := '';
    sgFrom     := StrFrom;
    sgTo       := StrTo;
    If Result='' Then
    Begin
      errMsg:='The source string was empty!';
      If ShowMsgs Then ShowMessage(errMsg);
      Exit;
    End;
    If sgFrom='' Then
    Begin
      errMsg:='No Text to replace was provided!';
      If ShowMsgs Then ShowMessage(errMsg);
      Exit;
    End;
    sgFrom:=ToPoundChar(sgFrom);
    SgTo  :=ToPoundChar(SgTo);
    Result:= StringReplace(Result,sgFrom,SgTo,[rfReplaceAll]);
    If ShowMsgs Then ShowMessage('Done');
  Except
  End;
end;

//
Unit Description UnitIndex Master Index
function ToPoundChar(s: String): String;
Var
  ch       : Char;
  i        : Integer;
  inPos    : Integer;
  sgAfter  : String;
  sgBefore : String;
  sgMid    : String;
  sgNew    : String;
  sgNum    : String;
  sgWas    : String;
begin
  Result:=s;
  sgNew    := '';
  sgWas    := s;
  inPos:=Pos('#',sgWas);
  If inPos=0 Then Exit;
  While inPos<>0 Do
  Begin
    sgBefore:= Copy(sgWas,1,inPos-1);
    sgAfter := Copy(sgWas,inPos+1,Length(sgWas)-(inPos+1)+1);
    sgNum   := '';
    sgMid   := '';
    For i:=1 To Length(sgAfter) Do
    Begin
      ch:=PChar(Copy(sgAfter,i,1))[0];
      Case ord(ch) of
      48..57:sgNum:=sgNum+chr(ord(ch));
      Else
        If sgNum='' Then
        Begin
          sgMid:='#'
        End
        Else
        Begin
          sgMid:=chr(StrToInt(sgNum));
          sgAfter:=Copy(sgAfter,i,Length(sgAfter)-i+1);
        End;
        Break;
      End;
    End;
    sgNew:=sgNew+sgBefore+sgMid;
    sgWas:=sgAfter;
    inPos:=Pos('#',sgWas);
    If inPos=0 Then
    Begin
      sgNew:=sgNew+sgWas;
      Break;
    End;
  End;
  Result:=sgNew;
end;

end.
//