• R/O
  • SSH
  • HTTPS

dprojfilter: Commit


Commit MetaInfo

Revision7 (tree)
Time2018-07-26 21:03:58
Authordummzeuch

Log Message

new options --ChangeFrom and --ChangeTo

Change Summary

Incremental Difference

--- trunk/src/u_DprojFilterMain.pas (revision 6)
+++ trunk/src/u_DprojFilterMain.pas (revision 7)
@@ -10,8 +10,10 @@
1010 type
1111 TDprojFilterMain = class(TDefaultMain)
1212 private
13- procedure HandleParam(const _Parameter, _DeleteLine: string);
14- procedure HandleFile(const _fn, _DeleteLine: string);
13+ procedure HandleParam(const _Parameter: string; const _DeleteLine: string;
14+ const _ChangeFrom, _ChangeTo: string);
15+ procedure HandleFile(const _fn: string; const _DeleteLine: string;
16+ const _ChangeFrom, _ChangeTo: string);
1517 protected
1618 procedure InitCmdLineParser; override;
1719 function doExecute: Integer; override;
@@ -26,14 +28,17 @@
2628
2729 { TDprojFilterMain }
2830
29-procedure TDprojFilterMain.HandleFile(const _fn: string; const _DeleteLine: string);
31+procedure TDprojFilterMain.HandleFile(const _fn: string; const _DeleteLine: string;
32+ const _ChangeFrom, _ChangeTo: string);
3033 var
3134 Orig: TStringList;
3235 Changed: TStringList;
3336 i: Integer;
37+ Line: string;
3438 s: string;
3539 bakfn: string;
36- DelCnt: Integer;
40+ ChangeCnt: Integer;
41+ p: Integer;
3742 begin
3843 WriteLn('processing file ', _fn);
3944 InitializeNil(Orig, Changed);
@@ -42,16 +47,24 @@
4247 Changed := TStringList.Create;
4348 try
4449 Orig.LoadFromFile(_fn);
45- DelCnt := 0;
50+ ChangeCnt := 0;
4651 for i := 0 to Orig.Count - 1 do begin
47- s := Trim(Orig[i]);
52+ Line := Orig[i];
53+ s := Trim(Line);
4854 if SameText(s, _DeleteLine) then begin
49- Inc(DelCnt);
55+ Inc(ChangeCnt);
56+ end else if SameText(s, _ChangeFrom) then begin
57+ // retain indentation
58+ p := Pos(s, Line);
59+ Assert(p > 0);
60+ s := Copy(Line, 1, p - 1) + _ChangeTo;
61+ Changed.Add(s);
62+ Inc(ChangeCnt);
5063 end else begin
51- Changed.Add(Orig[i]);
64+ Changed.Add(Line);
5265 end;
5366 end;
54- if DelCnt > 0 then begin
67+ if ChangeCnt > 0 then begin
5568 bakfn := _fn + '.bak';
5669 if TFileSystem.FileExists(bakfn) then
5770 WriteLn(bakfn, ' already exists, will not overwrite it.')
@@ -59,9 +72,9 @@
5972 WriteLn('Writing backup to ', bakfn);
6073 Orig.SaveToFile(bakfn);
6174 end;
75+ Changed.SaveToFile(_fn);
6276 end;
63- Changed.SaveToFile(_fn);
64- WriteLn(Format('Deleted %d lines.', [DelCnt]));
77+ WriteLn(Format('Changed %d lines.', [ChangeCnt]));
6578 except
6679 on e: Exception do begin
6780 WriteLn(Format('Error processing "%s": %s (%s)', [_fn, e.Message, e.ClassName]));
@@ -72,7 +85,8 @@
7285 end;
7386 end;
7487
75-procedure TDprojFilterMain.HandleParam(const _Parameter: string; const _DeleteLine: string);
88+procedure TDprojFilterMain.HandleParam(const _Parameter: string; const _DeleteLine: string;
89+ const _ChangeFrom, _ChangeTo: string);
7690 var
7791 FileIdx: Integer;
7892 Files: TStringList;
@@ -83,7 +97,7 @@
8397 TSimpleDirEnumerator.EnumFilesOnly(_Parameter, Files, True);
8498 WriteLn(Format('Found %d files matching %s', [Files.Count, _Parameter]));
8599 for FileIdx := 0 to TSimpleDirEnumerator.EnumFilesOnly(_Parameter, Files, True) - 1 do begin
86- HandleFile(Files[FileIdx], _DeleteLine);
100+ HandleFile(Files[FileIdx], _DeleteLine, _ChangeFrom, _ChangeTo);
87101 end;
88102 finally
89103 FreeAndNil(Files);
@@ -94,16 +108,33 @@
94108 var
95109 Parameters: TStringList;
96110 DeleteLine: string;
111+ ChangeFrom: string;
112+ ChangeTo: string;
97113 ParamIdx: Integer;
114+ OptionsOK: Boolean;
98115 begin
99116 Parameters := TStringList.Create;
100117 try
101118 FGetOpt.ParamPassed('DprojFile', Parameters);
102- if not FGetOpt.OptionPassed('DeleteLine', DeleteLine) then
103- raise Exception.Create('You must pass one of the options: --DeleteLine');
104- DeleteLine := UnquoteString(DeleteLine);
119+
120+ OptionsOK := False;
121+ if FGetOpt.OptionPassed('DeleteLine', DeleteLine) then begin
122+ OptionsOK := True;
123+ DeleteLine := UnquoteString(DeleteLine);
124+ end;
125+
126+ if FGetOpt.OptionPassed('ChangeFrom', ChangeFrom) then begin
127+ if FGetOpt.OptionPassed('ChangeTo', ChangeTo) then begin
128+ OptionsOK := True;
129+ end else
130+ raise Exception.Create('--ChangeFrom also requires --ChangeTo option');
131+ end;
132+
133+ if not OptionsOK then
134+ raise Exception.Create('You must pass one of the options: --DeleteLine or --ChangeFrom');
135+
105136 for ParamIdx := 0 to Parameters.Count - 1 do begin
106- HandleParam(Parameters[ParamIdx], DeleteLine);
137+ HandleParam(Parameters[ParamIdx], DeleteLine, ChangeFrom, ChangeTo);
107138 end;
108139 finally
109140 FreeAndNil(Parameters);
@@ -116,6 +147,8 @@
116147 inherited;
117148 FGetOpt.RegisterParam('DprojFile', 'Dproj file(s) to process wildcards are allowed', 1, MaxInt);
118149 FGetOpt.RegisterOption('DeleteLine', 'Delete a line matching the parameter, wildcards are not allowed. E.g. --DeleteLine="<DCC_DcpOutput>..\..\lib\16</DCC_DcpOutput>"', True);
150+ FGetOpt.RegisterOption('ChangeFrom', 'Change a line matching the parameter to the parameter of --ChangeTo. E.g. --ChangeFrom="bla" --ChangeTo="blub"', True);
151+ FGetOpt.RegisterOption('ChangeTo', 'Gives the new content for the ChangeFrom parameter. E.g. --ChangeFrom="bla" --ChangeTo="blub"', True);
119152 // FGetOpt.RegisterOption('DeleteLineRegEx', 'Delete a line matching the parameter, regular expressions are allowed. E.g. --DeleteLine="<DCC_DcpOutput>..\..\lib\16</DCC_DcpOutput>"', 1)
120153 end;
121154
Show on old repository browser