1 |
torben |
2671 |
(* ***** BEGIN LICENSE BLOCK *****
|
2 |
|
|
* Version: MPL 1.1
|
3 |
|
|
*
|
4 |
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
5 |
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
6 |
|
|
* the License. You may obtain a copy of the License at
|
7 |
|
|
* http://www.mozilla.org/MPL/
|
8 |
|
|
*
|
9 |
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
10 |
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
11 |
|
|
* for the specific language governing rights and limitations under the
|
12 |
|
|
* License.
|
13 |
|
|
*
|
14 |
|
|
* The Original Code is TurboPower SysTools
|
15 |
|
|
*
|
16 |
|
|
* The Initial Developer of the Original Code is
|
17 |
|
|
* TurboPower Software
|
18 |
|
|
*
|
19 |
|
|
* Portions created by the Initial Developer are Copyright (C) 1996-2002
|
20 |
|
|
* the Initial Developer. All Rights Reserved.
|
21 |
|
|
*
|
22 |
|
|
* Contributor(s):
|
23 |
|
|
*
|
24 |
|
|
* ***** END LICENSE BLOCK ***** *)
|
25 |
|
|
|
26 |
|
|
unit Exregeu1;
|
27 |
|
|
|
28 |
|
|
interface
|
29 |
|
|
|
30 |
|
|
uses
|
31 |
|
|
Windows, Messages, SysUtils, Classes, Graphics, Controls,
|
32 |
|
|
Forms, Dialogs, StdCtrls, Buttons, Gauges,
|
33 |
|
|
|
34 |
|
|
StBase, StRegEx;
|
35 |
|
|
|
36 |
|
|
type
|
37 |
|
|
TForm1 = class(TForm)
|
38 |
|
|
gbOptions: TGroupBox;
|
39 |
|
|
cbSelect: TCheckBox;
|
40 |
|
|
cbIgnoreCase: TCheckBox;
|
41 |
|
|
cbLineNumbers: TCheckBox;
|
42 |
|
|
cbxModOnly: TCheckBox;
|
43 |
|
|
cbxCountOnly: TCheckBox;
|
44 |
|
|
Label1: TLabel;
|
45 |
|
|
edtSourceFile: TEdit;
|
46 |
|
|
Label2: TLabel;
|
47 |
|
|
edtDestFile: TEdit;
|
48 |
|
|
bntSelAvoid: TButton;
|
49 |
|
|
btnMatch: TButton;
|
50 |
|
|
btnReplace: TButton;
|
51 |
|
|
Memo1: TMemo;
|
52 |
|
|
lblSelAvoid: TLabel;
|
53 |
|
|
lblMatch: TLabel;
|
54 |
|
|
lblReplace: TLabel;
|
55 |
|
|
lblLPS: TLabel;
|
56 |
|
|
Button1: TButton;
|
57 |
|
|
sbSource: TSpeedButton;
|
58 |
|
|
sbDest: TSpeedButton;
|
59 |
|
|
OpenDialog1: TOpenDialog;
|
60 |
|
|
Gauge1: TGauge;
|
61 |
|
|
StRegEx1: TStRegEx;
|
62 |
|
|
procedure SelectFile(Sender: TObject);
|
63 |
|
|
procedure bntSelAvoidClick(Sender: TObject);
|
64 |
|
|
procedure btnMatchClick(Sender: TObject);
|
65 |
|
|
procedure btnReplaceClick(Sender: TObject);
|
66 |
|
|
procedure Button1Click(Sender: TObject);
|
67 |
|
|
procedure StRegEx1Progress(Sender: TObject; Percent: Word);
|
68 |
|
|
procedure StRegEx1Match(Sender: TObject; Position: TMatchPosition);
|
69 |
|
|
private
|
70 |
|
|
{ Private declarations }
|
71 |
|
|
public
|
72 |
|
|
{ Public declarations }
|
73 |
|
|
|
74 |
|
|
ACount : Cardinal;
|
75 |
|
|
|
76 |
|
|
|
77 |
|
|
StRegExClass : TStStreamRegEx;
|
78 |
|
|
end;
|
79 |
|
|
|
80 |
|
|
var
|
81 |
|
|
Form1: TForm1;
|
82 |
|
|
|
83 |
|
|
implementation
|
84 |
|
|
|
85 |
|
|
{$R *.DFM}
|
86 |
|
|
|
87 |
|
|
uses
|
88 |
|
|
ExRegEU2,
|
89 |
|
|
StStrS;
|
90 |
|
|
|
91 |
|
|
procedure TForm1.SelectFile(Sender: TObject);
|
92 |
|
|
begin
|
93 |
|
|
if (Sender = sbSource) then begin
|
94 |
|
|
OpenDialog1.Title := 'Source File';
|
95 |
|
|
OpenDialog1.Options := OpenDialog1.Options + [ofFileMustExist];
|
96 |
|
|
if OpenDialog1.Execute then
|
97 |
|
|
edtSourceFile.Text := OpenDialog1.FileName;
|
98 |
|
|
end else begin
|
99 |
|
|
OpenDialog1.Title := 'Destination File';
|
100 |
|
|
OpenDialog1.Options := OpenDialog1.Options - [ofFileMustExist];
|
101 |
|
|
if OpenDialog1.Execute then
|
102 |
|
|
edtDestFile.Text := OpenDialog1.FileName;
|
103 |
|
|
end;
|
104 |
|
|
end;
|
105 |
|
|
|
106 |
|
|
procedure TForm1.bntSelAvoidClick(Sender: TObject);
|
107 |
|
|
begin
|
108 |
|
|
Form2 := TForm2.Create(Self);
|
109 |
|
|
try
|
110 |
|
|
Form2.Memo1.Clear;
|
111 |
|
|
Form2.Memo1.Lines.Assign(StRegEx1.SelAvoidPattern);
|
112 |
|
|
if (Form2.ShowModal = mrOK) then begin
|
113 |
|
|
StRegEx1.SelAvoidPattern.Clear;
|
114 |
|
|
StRegEx1.SelAvoidPattern.Assign(Form2.Memo1.Lines);
|
115 |
|
|
end;
|
116 |
|
|
finally
|
117 |
|
|
Form2.Free;
|
118 |
|
|
Form2 := nil;
|
119 |
|
|
end;
|
120 |
|
|
end;
|
121 |
|
|
|
122 |
|
|
procedure TForm1.btnMatchClick(Sender: TObject);
|
123 |
|
|
begin
|
124 |
|
|
Form2 := TForm2.Create(Self);
|
125 |
|
|
try
|
126 |
|
|
Form2.Memo1.Clear;
|
127 |
|
|
Form2.Memo1.Lines.Assign(StRegEx1.MatchPattern);
|
128 |
|
|
if (Form2.ShowModal = mrOK) then begin
|
129 |
|
|
StRegEx1.MatchPattern.Clear;
|
130 |
|
|
StRegEx1.MatchPattern.Assign(Form2.Memo1.Lines);
|
131 |
|
|
end;
|
132 |
|
|
finally
|
133 |
|
|
Form2.Free;
|
134 |
|
|
Form2 := nil;
|
135 |
|
|
end;
|
136 |
|
|
end;
|
137 |
|
|
|
138 |
|
|
procedure TForm1.btnReplaceClick(Sender: TObject);
|
139 |
|
|
begin
|
140 |
|
|
Form2 := TForm2.Create(Self);
|
141 |
|
|
try
|
142 |
|
|
Form2.Memo1.Clear;
|
143 |
|
|
Form2.Memo1.Lines.Assign(StRegEx1.ReplacePattern);
|
144 |
|
|
if (Form2.ShowModal = mrOK) then begin
|
145 |
|
|
StRegEx1.ReplacePattern.Clear;
|
146 |
|
|
StRegEx1.ReplacePattern.Assign(Form2.Memo1.Lines);
|
147 |
|
|
end;
|
148 |
|
|
finally
|
149 |
|
|
Form2.Free;
|
150 |
|
|
Form2 := nil;
|
151 |
|
|
end;
|
152 |
|
|
end;
|
153 |
|
|
|
154 |
|
|
procedure TForm1.Button1Click(Sender: TObject);
|
155 |
|
|
begin
|
156 |
|
|
ACount := 0;
|
157 |
|
|
|
158 |
|
|
if cbxModOnly.Checked then
|
159 |
|
|
StRegEx1.OutputOptions := StRegEx1.OutputOptions + [ooModified]
|
160 |
|
|
else
|
161 |
|
|
StRegEx1.OutputOptions := StRegEx1.OutputOptions - [ooModified];
|
162 |
|
|
if cbxCountOnly.Checked then
|
163 |
|
|
StRegEx1.OutputOptions := StRegEx1.OutputOptions + [ooCountOnly]
|
164 |
|
|
else
|
165 |
|
|
StRegEx1.OutputOptions := StRegEx1.OutputOptions - [ooCountOnly];
|
166 |
|
|
|
167 |
|
|
if (TrimS(edtSourceFile.Text) = '') or
|
168 |
|
|
((TrimS(edtDestFile.Text) = '') and (not (ooCountOnly in StRegEx1.OutputOptions))) then begin
|
169 |
|
|
MessageDlg('Source and/or Destination file cannot be blank',
|
170 |
|
|
mtError, [mbOK], 0);
|
171 |
|
|
Exit;
|
172 |
|
|
end;
|
173 |
|
|
|
174 |
|
|
if not (FileExists(TrimS(edtSourceFile.Text))) then begin
|
175 |
|
|
MessageDlg('Source file not found', mtError, [mbOK], 0);
|
176 |
|
|
Exit;
|
177 |
|
|
end;
|
178 |
|
|
|
179 |
|
|
if (StRegEx1.SelAvoidPattern.Count = 0) and
|
180 |
|
|
(StRegEx1.MatchPattern.Count = 0) then begin
|
181 |
|
|
MessageDlg('You must specify a SelAvoid or Match Pattern',
|
182 |
|
|
mtError, [mbOK], 0);
|
183 |
|
|
Exit;
|
184 |
|
|
end;
|
185 |
|
|
|
186 |
|
|
StRegEx1.IgnoreCase := cbIgnoreCase.Checked;
|
187 |
|
|
StRegEx1.Avoid := not cbSelect.Checked;
|
188 |
|
|
StRegEx1.LineNumbers := cbLineNumbers.Checked;
|
189 |
|
|
StRegEx1.InputFile := TrimS(edtSourceFile.Text);
|
190 |
|
|
StRegEx1.OutputFile := edtDestFile.Text;
|
191 |
|
|
|
192 |
|
|
lblSelAvoid.Caption := 'Sel/Avoid: 0';
|
193 |
|
|
lblMatch.Caption := 'Match: 0';
|
194 |
|
|
lblReplace.Caption := 'ReplaceL 0';
|
195 |
|
|
lblLPS.Caption := 'Lines/Sec: 0';
|
196 |
|
|
|
197 |
|
|
Screen.Cursor := crHourglass;
|
198 |
|
|
try
|
199 |
|
|
StRegEx1.Execute;
|
200 |
|
|
finally
|
201 |
|
|
Screen.Cursor := crDefault;
|
202 |
|
|
end;
|
203 |
|
|
|
204 |
|
|
Memo1.Clear;
|
205 |
|
|
if (not (ooCountOnly in StRegEx1.OutputOptions)) then
|
206 |
|
|
Memo1.Lines.LoadFromFile(edtDestFile.Text);
|
207 |
|
|
|
208 |
|
|
lblSelAvoid.Caption := 'Sel/Avoid: ' + IntToStr(StRegEx1.LinesSelected);
|
209 |
|
|
lblMatch.Caption := 'Match: ' + IntToStr(StRegEx1.LinesMatched);
|
210 |
|
|
lblReplace.Caption := 'Replace: ' + IntToStr(StRegEx1.LinesReplaced);
|
211 |
|
|
lblLPS.Caption := 'Lines/Sec: ' + IntToStr(StRegEx1.LinesPerSecond);
|
212 |
|
|
end;
|
213 |
|
|
|
214 |
|
|
procedure TForm1.StRegEx1Progress(Sender: TObject; Percent: Word);
|
215 |
|
|
begin
|
216 |
|
|
if ((Percent mod 2) = 0) and (Gauge1.Progress <> Percent) then
|
217 |
|
|
Gauge1.Progress := Percent;
|
218 |
|
|
end;
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
procedure TForm1.StRegEx1Match(Sender: TObject; Position: TMatchPosition);
|
222 |
|
|
begin
|
223 |
|
|
Inc(ACount);
|
224 |
|
|
Caption := IntToStr(Position.LineNum);
|
225 |
|
|
Application.ProcessMessages;
|
226 |
|
|
end;
|
227 |
|
|
|
228 |
|
|
end.
|