: Automate and process repetitive tasks with this automation program
// Read: Copyright © Thomas Schulz, all rights reserved.
// Contact: Primary: thomas#micro-sys.dk. Secondary: dk_sz#hotmail.com.
// Can only be used when making modules for "Micro-Sys MultiBatcher".
//------------------------------------------------------------------------------
unit PluginCore_TextReplacer;
{$I AImsCoreDefines.inc}
interface
uses
LUmsCommonAllSharedDeclars
,
LUmsPluginAllSharedBaseCore
;
//------------------------------------------------------------------------------
type
TPlugin_TextReplacer = class(TPluginExternal)
function PluginReturnInfo: PPluginInfo; override;
procedure PluginSetupInit; override;
procedure PluginSetupPreEdit; override;
procedure PluginSetupWorkStart; override;
procedure PluginSetupWorkEnd; override;
procedure PluginWorkPath(PluginPassedReadInProps: PPluginPassedReadInProps; PluginPassedWriteOutProps: PPluginPassedWriteOutProps); override;
//--
procedure PluginPrepareVerifyGridRow(const PropName: string); override;
procedure PluginAddVerifyPropGridValue(const PropName: string; const PropValue: string); override;
procedure PluginEndVerifyGridRow(const PropName: string; ExeCallBack: TProcExeHandleEditPropGridErrorMsg); override;
procedure PluginSetPropGridRowCount(const PropName: string; RowCount: Integer); override;
procedure PluginSetPropGridValue(const PropName: string; const ColID: string; RowIdx: Integer; const FieldValue: string); override;
//--
procedure PluginSetPropChoiceValueCount(const PropName: string; PropValueCount: Integer); override;
procedure PluginSetPropChoiceValue(const PropName: string; const ChoiceID: string); override;
//--
procedure PluginVerifyPropTextBoxValue(const PropName: string; const TextBoxID: string; const PropValue: string; ExeCallBack: TProcExeHandleEditPropTextBoxErrorMsg); override;
procedure PluginSetPropTextBoxValue(const PropName: string; const TextBoxID: string; const PropValue: string); override;
//--
procedure PluginVerifyPropFileValue(const PropName: string; const FullFilePathName: string; ExeCallBack: TProcExeHandleEditPropFileErrorMsg); override;
procedure PluginSetPropFileValue(const PropName: string; const FullFilePathName: string); override;
end;
//------------------------------------------------------------------------------
implementation
uses
SysUtils
,
Classes
;
const
imcPluginInfo: TPluginInfo = (
StrID: 'dk_micro-sys_TextReplacer_DevDemo';
Author: 'Thomas Schulz';
CopyRight: 'Copyright © Thomas Schulz 2002-2010, all rights reserved';
PluginName: 'TextReplacerDemo';
PluginDesc: 'Replaces text (strings) in files.';
HasCompany: 'Micro-Sys ApS';
HasEmail: 'thomas@micro-sys.dk';
HasUrl: 'http://www.micro-sys.dk';
CanLaunchFilesSep: '';
CanLaunchFilesList: '';
PluginLoadingScheme: plsCanDynamicAsNeeded;
HasLogo: '';
SuggestedExts: '.txt';
PluginVersion: '1.02';
InterfaceVersion: incPIV_Major;
PluginCategory: pcFileProcessing;
CanWorkCategory: cwcFiles;
ReleaseState: prsStable;
);
const
imc_P_Grid_Strings = 'Grid';
imc_P_ChoiceSingle = 'ChoiceSingle';
imc_P_ChoiceMulti = 'ChoiceMulti';
imc_P_TextBoxes = 'TextBoxes';
imc_P_File = 'File';
imc_P_Grid_Defaults = 'GridDefaults';
var
imvTmpValidateStrings: TStringList = nil;
imvFilters: TStringList = nil;
imvCaseSensitive: array of Boolean;
imvReplace: TStringList = nil;
imvWith: TStringList = nil;
// plugin special
//------------------------------------------------------------------------------
function TPlugin_TextReplacer.PluginReturnInfo: PPluginInfo;
begin
Result := @imcPluginInfo;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetupInit;
begin
invpExeMakePropName(imc_P_Grid_Strings, 'Real Grid', vptGrid, pprsTextEditorOpen, 'Real property hint text', 'Real property help text');
invpExeSetPropMetasGrid(imc_P_Grid_Strings, pgirlAllRowCellsEmpty);
invpExeMakePropName(imc_P_Grid_Defaults, 'Demo Grid', vptGrid, pprsNull, 'Demo property hint text', 'Demo property help text');
invpExeSetPropMetasGrid(imc_P_Grid_Defaults, pgirlAllRowCellsEmpty);
invpExeMakePropName(imc_P_ChoiceSingle, 'Demo ChoiceSingle', vtpChoiceSingle, pprsNull, '', '');
invpExeMakePropName(imc_P_ChoiceMulti, 'Demo ChoiceMultiple', vtpChoiceMultiple, pprsNull, '', '');
invpExeMakePropName(imc_P_TextBoxes, 'Demo TextBoxes', vptTextBoxes, pprsFilePathInsert or pprsTextEditorOpen, '', '');
invpExeMakePropName(imc_P_File, 'Demo File', vptFile, pprsNull, '', '');
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetupPreEdit;
var
TmpStr_Wide: WideString;
begin
invpExeSetPropGridColumnCount(imc_P_Grid_Strings, 4);
invpExeAddPropGridColumn(imc_P_Grid_Strings, 'TYPE_FILTER', 'Type Filter', '50', pgcp_TypeFilter);
invpExeAddPropGridColumn(imc_P_Grid_Strings, 'CASE_SENSITIVE', 'Case Sensitive Strings', '50+', pgcp_TrueFalse);
invpExeAddPropGridColumn(imc_P_Grid_Strings, 'MATCH', 'Match', '50%', pgcpNull);
invpExeAddPropGridColumn(imc_P_Grid_Strings, 'REPLACE', 'Replace', '100%', pgcpNull);
invpExeSetPropDefaultGridValueCount(imc_P_Grid_Strings, 0);
//--
invpExeSetPropGridColumnCount(imc_P_Grid_Defaults, 2);
invpExeAddPropGridColumn(imc_P_Grid_Defaults, 'PURE_TEXT', 'Pure Text', '', pgcpNull);
invpExeAddPropGridColumn(imc_P_Grid_Defaults, 'DROP_DOWN', 'Drop Down', '', pgcpList);
invpExeSetPropDefaultGridValueCount(imc_P_Grid_Defaults, 2);
invpExeAddPropDefaultGridValue(imc_P_Grid_Defaults, 0, 0, 'default text');
invpExeAddPropDefaultGridValue(imc_P_Grid_Defaults, 1, 0, ',cool,agile,cool,neat');
//--
invpExeSetPropChoiceValueCount(imc_P_ChoiceSingle, 2);
invpExeAddPropChoiceValue(imc_P_ChoiceSingle, 'S_ChoiceOne', 'Demo choice one', True);
invpExeAddPropChoiceValue(imc_P_ChoiceSingle, 'S_ChoiceTwo', 'Demo choice two', False);
//--
invpExeSetPropChoiceValueCount(imc_P_ChoiceMulti, 2);
invpExeAddPropChoiceValue(imc_P_ChoiceMulti, 'M_ChoiceOne', 'Demo choice one', True);
invpExeAddPropChoiceValue(imc_P_ChoiceMulti, 'M_ChoiceTwo', 'Demo choice two', True);
//--
invpExeSetPropTextBoxValueCount(imc_P_TextBoxes, 1);
TmpStr_Wide := invPluginFilePathName;
invpExeSetPropTextBoxValue(imc_P_TextBoxes, 'EditText', 'Demo LabelText', PWideChar(TmpStr_Wide));
//--
invpExeSetPropDefaultFileValue(imc_P_File, 'demo.ini', pfpHighLightIni); // place a file called 'demo.ini' same place as plugin
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetupWorkStart;
begin
imvFilters := TStringList.Create;
imvReplace := TStringList.Create;
imvWith := TStringList.Create;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetupWorkEnd;
begin
imvFilters.Free;
imvReplace.Free;
imvWith.Free;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginWorkPath(PluginPassedReadInProps: PPluginPassedReadInProps; PluginPassedWriteOutProps: PPluginPassedWriteOutProps);
var
TmpPath: string;
TmpContent: string;
TmpPattern: string;
TmpPatFilt: string;
E: Integer;
I: Integer;
TmpReplaceFlags: TReplaceFlags;
TmpReplace: string;
TmpWith: string;
FS: TFileStream;
begin
TmpPath := PluginPassedReadInProps^.WorkPathNameIn;
TmpReplaceFlags := [rfReplaceAll];
//--
if FileExists(TmpPath) then
begin
TmpPattern := ExtractFileExt(TmpPath);
if TmpPattern = '' then
TmpPattern := '.'; // untyped
TmpPattern := '*' + TmpPattern;
end
else Exit;
//--
try
E := imvFilters.Count - 1;
for I := 0 to E do
begin
TmpPatFilt := imvFilters[I];
if (TmpPatFilt = '*.*') or (TmpPattern = TmpPatFilt) then
begin
if imvCaseSensitive[I] = False then
TmpReplaceFlags := TmpReplaceFlags + [rfIgnoreCase]
else TmpReplaceFlags := TmpReplaceFlags - [rfIgnoreCase];
//--
TmpReplace := imvReplace[I];
TmpWith := imvWith[I];
//--
try
FS := TFileStream.Create(TmpPath, fmOpenReadWrite);
try
SetLength(TmpContent, FS.Size);
FS.Read(PChar(TmpContent)^, FS.Size);
//--
TmpContent := StringReplace(TmpContent, imvReplace[I], imvWith[I], TmpReplaceFlags);
//--
FS.Size := Length(TmpContent);
FS.Position := 0;
FS.Write(PChar(TmpContent)^, Length(TmpContent)); { could also use s[1] }
finally
FS.Free;
end;
except
end;
end;
end;
except
on E: Exception do
begin
invpExeHandleWorkPathErrorMsg_Ansi(PChar('Error: ' + E.Message), '');
end
;
end;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginPrepareVerifyGridRow(const PropName: string);
begin
if PropName = imc_P_Grid_Strings then
imvTmpValidateStrings := TStringList.Create;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginAddVerifyPropGridValue(const PropName: string; const PropValue: string);
begin
if PropName = imc_P_Grid_Strings then
imvTmpValidateStrings.Add(PropValue);
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginEndVerifyGridRow(const PropName: string; ExeCallBack: TProcExeHandleEditPropGridErrorMsg);
var
TmpWideStr: WideString;
begin
if PropName = imc_P_Grid_Strings then
begin
if (imvTmpValidateStrings[2] = imvTmpValidateStrings[3]) then
begin
TmpWideStr := 'You can not have same string in both "Replace" and "With" fields.';
ExeCallBack(2, 'Value is wrong.', PWideChar(TmpWideStr));
end;
FreeAndNil(imvTmpValidateStrings);
end;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetPropGridRowCount(const PropName: string; RowCount: Integer);
begin
// not needed here, use for what you want, e.g. init some arrays or whatever
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetPropGridValue(const PropName: string; const ColID: string; RowIdx: Integer; const FieldValue: string);
var
TmpValue: string;
begin
if PropName = imc_P_Grid_Strings then
begin
TmpValue := FieldValue;
//--
if ColID = 'TYPE_FILTER' then
begin
{$IFDEF mymsWINDOWS}
TmpValue := AnsiLowerCaseFileName(TmpValue);
{$ENDIF}
imvFilters.Add(TmpValue);
end
else
if ColID = 'CASE_SENSITIVE' then
begin
TmpValue := Trim(TmpValue);
SetLength(imvCaseSensitive, Length(imvCaseSensitive) + 1);
imvCaseSensitive[Length(imvCaseSensitive) - 1] := StrToBool(TmpValue);
end
else
if ColID = 'MATCH' then
begin
if imvCaseSensitive[Length(imvCaseSensitive) - 1] = False then
TmpValue := AnsiLowerCase(TmpValue);
imvReplace.Add(TmpValue);
end
else
if ColID = 'REPLACE' then
begin
imvWith.Add(TmpValue);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetPropChoiceValueCount(const PropName: string; PropValueCount: Integer);
begin
// if: PluginLoadingScheme = plsRequireKeepInMemory
// then we would need to "reset" values like this:
if PropName = imc_P_ChoiceSingle then
begin
// SomeVariable(s) := DefValue;
end
else
if PropName = imc_P_ChoiceMulti then
begin
// SomeVariable(s) := DefValue;
end
;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetPropChoiceValue(const PropName: string; const ChoiceID: string);
begin
if PropName = imc_P_ChoiceSingle then
begin
if ChoiceID = 'S_ChoiceOne' then
begin
// ...
end
else
if ChoiceID = 'S_ChoiceTwo' then
begin
// ...
end;
end
else
if PropName = imc_P_ChoiceMulti then
begin
// ...
end;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginVerifyPropTextBoxValue(const PropName: string; const TextBoxID: string; const PropValue: string; ExeCallBack: TProcExeHandleEditPropTextBoxErrorMsg);
var
TmpErrorDetected: Boolean;
TmpStrError: string;
TmpStrErrorWide: WideString;
begin
TmpErrorDetected := False;
TmpStrError := '';
if PropName = imc_P_TextBoxes then
begin
if TextBoxID = 'EditText' then
begin
TmpErrorDetected := not FileExists(PropValue);
if TmpErrorDetected then
begin
TmpStrError := 'File path: "' + PropValue + '" does not exist.';
end;
end
end;
if TmpErrorDetected then
begin
TmpStrErrorWide := TmpStrError;
ExeCallBack(PChar(TextBoxID), 'Value is wrong.', PWideChar(TmpStrErrorWide));
end
;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetPropTextBoxValue(const PropName: string; const TextBoxID: string; const PropValue: string);
begin
if PropName = imc_P_TextBoxes then
begin
if TextBoxID = 'EditText' then
begin
// ...
end
end;
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginVerifyPropFileValue(const PropName: string; const FullFilePathName: string; ExeCallBack: TProcExeHandleEditPropFileErrorMsg);
begin
// if wrong, use param like: ExeCallBack(...)
end;
//------------------------------------------------------------------------------
procedure TPlugin_TextReplacer.PluginSetPropFileValue(const PropName: string; const FullFilePathName: string);
begin
if PropName = imc_P_File then
begin
{
e.g:
TmpIniFileRead := TMemIniFile.Create(FullFilePathName);
... read values ...
TmpIniFileRead.Free;
}
end;
end;
//------------------------------------------------------------------------------
initialization
invPluginExternalClass := TPlugin_TextReplacer;
finalization
end.
This file is part of Automation Batch Tools. All rights reserved. See legal.