• R/O
  • SSH
  • HTTPS

dprojfilter: Commit


Commit MetaInfo

Revision9 (tree)
Time2018-07-27 00:43:24
Authordummzeuch

Log Message

* Bugfix: AnsiDequoteString did not work as expected -> SimpleDequoteString
* new options to insert new lines

Change Summary

Incremental Difference

--- trunk/src/u_DprojFilterMain.pas (revision 8)
+++ trunk/src/u_DprojFilterMain.pas (revision 9)
@@ -10,10 +10,15 @@
1010 type
1111 TDprojFilterMain = class(TDefaultMain)
1212 private
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);
13+ FDeleteLine: string;
14+ FChangeFrom: string;
15+ FChangeTo: string;
16+ FInsertAfter: string;
17+ FInsert: string;
18+ FInsertAfterAll: Boolean;
19+ FINsertAfterAllowDuplicates: Boolean;
20+ procedure HandleParam(const _Parameter: string);
21+ procedure HandleFile(const _fn: string);
1722 protected
1823 procedure InitCmdLineParser; override;
1924 function doExecute: Integer; override;
@@ -28,8 +33,7 @@
2833
2934 { TDprojFilterMain }
3035
31-procedure TDprojFilterMain.HandleFile(const _fn: string; const _DeleteLine: string;
32- const _ChangeFrom, _ChangeTo: string);
36+procedure TDprojFilterMain.HandleFile(const _fn: string);
3337 var
3438 Orig: TStringList;
3539 Changed: TStringList;
@@ -39,6 +43,8 @@
3943 bakfn: string;
4044 ChangeCnt: Integer;
4145 p: Integer;
46+ InsertAfterDone: Boolean;
47+ doInsert: Boolean;
4248 begin
4349 WriteLn('processing file ', _fn);
4450 InitializeNil(Orig, Changed);
@@ -48,18 +54,43 @@
4854 try
4955 Orig.LoadFromFile(_fn);
5056 ChangeCnt := 0;
57+ InsertAfterDone := False;
5158 for i := 0 to Orig.Count - 1 do begin
5259 Line := Orig[i];
5360 s := Trim(Line);
54- if SameText(s, _DeleteLine) then begin
61+ if SameText(s, FDeleteLine) then begin
5562 Inc(ChangeCnt);
56- end else if SameText(s, _ChangeFrom) then begin
63+ end else if SameText(s, FChangeFrom) then begin
5764 // retain indentation
5865 p := Pos(s, Line);
5966 Assert(p > 0);
60- s := Copy(Line, 1, p - 1) + _ChangeTo;
67+ s := Copy(Line, 1, p - 1) + FChangeTo;
6168 Changed.Add(s);
6269 Inc(ChangeCnt);
70+ end else if SameText(s, FInsertAfter) then begin
71+ Changed.Add(Line);
72+ if not InsertAfterDone then begin
73+ // retain indentation
74+ p := Pos(s, Line);
75+ Assert(p > 0);
76+ doInsert := True;
77+ if i < Orig.Count - 1 then begin
78+ s := Trim(Orig[i + 1]);
79+ if SameText(s, FInsert) then begin
80+ // Line to insert already exists
81+ if not FINsertAfterAllowDuplicates then begin
82+ doInsert := False;
83+ end;
84+ end;
85+ end;
86+ if doInsert then begin
87+ s := Copy(Line, 1, p - 1) + FInsert;
88+ Changed.Add(s);
89+ Inc(ChangeCnt);
90+ end;
91+ if not FInsertAfterAll then
92+ InsertAfterDone := True;
93+ end;
6394 end else begin
6495 Changed.Add(Line);
6596 end;
@@ -85,8 +116,7 @@
85116 end;
86117 end;
87118
88-procedure TDprojFilterMain.HandleParam(const _Parameter: string; const _DeleteLine: string;
89- const _ChangeFrom, _ChangeTo: string);
119+procedure TDprojFilterMain.HandleParam(const _Parameter: string);
90120 var
91121 FileIdx: Integer;
92122 Files: TStringList;
@@ -97,7 +127,7 @@
97127 TSimpleDirEnumerator.EnumFilesOnly(_Parameter, Files, True);
98128 WriteLn(Format('Found %d files matching %s', [Files.Count, _Parameter]));
99129 for FileIdx := 0 to TSimpleDirEnumerator.EnumFilesOnly(_Parameter, Files, True) - 1 do begin
100- HandleFile(Files[FileIdx], _DeleteLine, _ChangeFrom, _ChangeTo);
130+ HandleFile(Files[FileIdx]);
101131 end;
102132 finally
103133 FreeAndNil(Files);
@@ -104,12 +134,21 @@
104134 end;
105135 end;
106136
137+function SimpleDequoteString(const _s: string): string;
138+var
139+ Len: Integer;
140+begin
141+ Result := _s;
142+ Len := Length(Result);
143+ if Result <> '' then begin
144+ if (Result[1] = '"') and (Result[Len] = '"') then
145+ Result := Copy(Result, 2, Len - 2);
146+ end;
147+end;
148+
107149 function TDprojFilterMain.doExecute: Integer;
108150 var
109151 Parameters: TStringList;
110- DeleteLine: string;
111- ChangeFrom: string;
112- ChangeTo: string;
113152 ParamIdx: Integer;
114153 OptionsOK: Boolean;
115154 begin
@@ -118,23 +157,49 @@
118157 FGetOpt.ParamPassed('DprojFile', Parameters);
119158
120159 OptionsOK := False;
121- if FGetOpt.OptionPassed('DeleteLine', DeleteLine) then begin
160+ if FGetOpt.OptionPassed('DeleteLine', FDeleteLine) then begin
122161 OptionsOK := True;
123- DeleteLine := UnquoteString(DeleteLine);
124162 end;
125163
126- if FGetOpt.OptionPassed('ChangeFrom', ChangeFrom) then begin
127- if FGetOpt.OptionPassed('ChangeTo', ChangeTo) then begin
164+ if FGetOpt.OptionPassed('ChangeFrom', FChangeFrom) then begin
165+ if FGetOpt.OptionPassed('ChangeTo', FChangeTo) then begin
128166 OptionsOK := True;
129167 end else
130168 raise Exception.Create('--ChangeFrom also requires --ChangeTo option');
169+ end else begin
170+ if FGetOpt.OptionPassed('ChangeTo', FChangeTo) then
171+ raise Exception.Create('--ChangeTo is not allowed without a --ChangeFrom option');
131172 end;
132173
174+ FINsertAfterAllowDuplicates := False;
175+ FInsertAfterAll := False;
176+ if FGetOpt.OptionPassed('InsertAfter', FInsertAfter) then begin
177+ if FGetOpt.OptionPassed('Insert', FInsert) then begin
178+ OptionsOK := True;
179+ end else
180+ raise Exception.Create('--InsertAfter also requires an --Insert option');
181+ FInsertAfterAll := FGetOpt.OptionPassed('InsertAfterAll');
182+ FINsertAfterAllowDuplicates := FGetOpt.OptionPassed('InsertAfterAllowDuplicates');
183+ end else begin
184+ if FGetOpt.OptionPassed('Insert', FInsert) then
185+ raise Exception.Create('--Insert is not allowed without an --InsertAfter option');
186+ if FGetOpt.OptionPassed('InsertAfterAll') then
187+ raise Exception.Create('--InsertAfterAll is not allowed without an --InsertAfter option');
188+ if FGetOpt.OptionPassed('InsertAfterAllowDuplicates') then
189+ raise Exception.Create('--InsertAfterAllowDuplicates is not allowed without an --InsertAfter option');
190+ end;
191+
133192 if not OptionsOK then
134- raise Exception.Create('You must pass one of the options: --DeleteLine or --ChangeFrom');
193+ raise Exception.Create('You must pass one of the options: --DeleteLine, --ChangeFrom or --InsertAfter');
135194
195+ FDeleteLine := SimpleDequoteString(FDeleteLine);
196+ FChangeFrom := SimpleDequoteString(FChangeFrom);
197+ FChangeTo := SimpleDequoteString(FChangeTo);
198+ FInsertAfter := SimpleDequoteString(FInsertAfter);
199+ FInsert := SimpleDequoteString(FInsert);
200+
136201 for ParamIdx := 0 to Parameters.Count - 1 do begin
137- HandleParam(Parameters[ParamIdx], DeleteLine, ChangeFrom, ChangeTo);
202+ HandleParam(Parameters[ParamIdx]);
138203 end;
139204 finally
140205 FreeAndNil(Parameters);
@@ -146,10 +211,14 @@
146211 begin
147212 inherited;
148213 FGetOpt.RegisterParam('DprojFile', 'Dproj file(s) to process wildcards are allowed', 1, MaxInt);
149- FGetOpt.RegisterOption('DeleteLine', 'Delete a line matching the parameter, wildcards are not allowed. E.g. --DeleteLine="<DCC_DcpOutput>..\..\lib\16</DCC_DcpOutput>"', True);
214+ FGetOpt.RegisterOption('DeleteLine', 'Delete a line matching the parameter. E.g. --DeleteLine="<DCC_DcpOutput>..\..\lib\16</DCC_DcpOutput>"', True);
150215 FGetOpt.RegisterOption('ChangeFrom', 'Change a line matching the parameter to the parameter of --ChangeTo. E.g. --ChangeFrom="bla" --ChangeTo="blub"', True);
151216 FGetOpt.RegisterOption('ChangeTo', 'Gives the new content for the ChangeFrom parameter. E.g. --ChangeFrom="bla" --ChangeTo="blub"', True);
152-// FGetOpt.RegisterOption('DeleteLineRegEx', 'Delete a line matching the parameter, regular expressions are allowed. E.g. --DeleteLine="<DCC_DcpOutput>..\..\lib\16</DCC_DcpOutput>"', 1)
217+ FGetOpt.RegisterOption('InsertAfter', 'Insert a new line after the one matching the parameter. Requires an --Insert option.', True);
218+ FGetOpt.RegisterOption('Insert', 'Gives the line to insert for the --InsertAfter option.', True);
219+ FGetOpt.RegisterOption('InsertAfterAll', 'If given, InsertAfter applies to all matching lines, if not, only to the first. Requires an --Insert option.', False);
220+ FGetOpt.RegisterOption('InsertAfterAllowDuplicates', 'If given, InsertAfter will skip the check if the line to insert already exists. Requires an --Insert option.', False);
221+ FGetOpt.DequoteParams := False;
153222 end;
154223
155224 end.
Show on old repository browser