forked from pyscripter/pyscripter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cFileSearch.pas
321 lines (278 loc) · 9.63 KB
/
cFileSearch.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
{-----------------------------------------------------------------------------
Unit Name: cFileSearch
Author: Kiriakos Vlahos
Date: 29-May-2005
Purpose:
History: Based on GExperts (www.gexperts.org) unit and covered by its licence
GExperts License Agreement
GExperts is copyright 1996-2005 by GExperts, Inc, Erik Berry, and several other
authors who have submitted their code for inclusion. This license agreement only covers code written by GExperts, Inc and Erik Berry. You should contact the other authors concerning their respective copyrights and conditions.
The rules governing the use of GExperts and the GExperts source code are derived
from the official Open Source Definition, available at http://www.opensource.org.
The conditions and limitations are as follows:
* Usage of GExperts binary distributions is permitted for all developers.
You may not use the GExperts source code to develop proprietary or
commercial products including plugins or libraries for those products.
You may use the GExperts source code in an Open Source project, under the
terms listed below.
* You may not use the GExperts source code to create and distribute custom
versions of GExperts under the "GExperts" name. If you do modify and
distribute custom versions of GExperts, the binary distribution must be
named differently and clearly marked so users can tell they are not using
the official GExperts distribution. A visible and unmodified version of
this license must appear in any modified distribution of GExperts.
* Custom distributions of GExperts must include all of the custom changes
as a patch file that can be applied to the original source code. This
restriction is in place to protect the integrity of the original author's
source code. No support for modified versions of GExperts will be provided
by the original authors or on the GExperts mailing lists.
* All works derived from GExperts must be distributed under a license
compatible with this license and the official Open Source Definition,
which can be obtained from http://www.opensource.org/.
* Please note that GExperts, Inc. and the other contributing authors hereby
state that this package is provided "as is" and without any express or
implied warranties, including, but not without limitation, the implied
warranties of merchantability and fitness for a particular purpose. In
other words, we accept no liability for any damage that may result from
using GExperts or programs that use the GExperts source code.
-----------------------------------------------------------------------------}
unit cFileSearch;
interface
uses
SysUtils, Classes, SynRegExpr, SynUnicode;
type
TSearchOption = (soCaseSensitive, soWholeWord, soRegEx);
TSearchOptions = set of TSearchOption;
TFoundEvent = procedure(Sender: TObject; LineNo: Integer; const Line: string; SPos, EPos: Integer) of object;
// ELineTooLong = class(Exception);
// We separate the grep code from the file management code in TSearcher
TBaseSearcher = class(TObject)
private
protected
FOnFound: TFoundEvent;
FOnStartSearch: TNotifyEvent;
procedure SignalStartSearch; virtual;
procedure SignalFoundMatch(LineNo: Integer; const Line: string; SPos, EPos: Integer); virtual;
protected
BLine: string; // The current search line,
FLineNo: Integer;
fSearchLines : TUnicodeStrings;
FNoComments: Boolean;
FSearchOptions: TSearchOptions;
FPattern: string;
FFileName: string;
FRegExpr : TRegExpr;
procedure DoSearch;
procedure PatternMatch;
public
constructor Create;
destructor Destroy; override;
procedure SetPattern(const Value: string);
property NoComments: Boolean read FNoComments write FNoComments;
property Pattern: string read FPattern write SetPattern;
property SearchOptions: TSearchOptions read FSearchOptions write FSearchOptions;
property OnFound: TFoundEvent read FOnFound write FOnFound;
property OnStartSearch: TNotifyEvent read FOnStartSearch write FOnStartSearch;
end;
TSearcher = class(TBaseSearcher)
private
procedure Reset;
protected
procedure SetFileName(const Value: string);
protected
public
constructor Create(const SearchFileName: string);
procedure Execute;
property FileName: string read FFileName write SetFileName;
end;
implementation
uses
Windows, uEditAppIntfs, StringResources,
uCommonFunctions, WideStrUtils, gnugettext;
const
SearchLineSize = 1024;
{ TSearcher }
constructor TSearcher.Create(const SearchFileName: string);
begin
inherited Create;
if SearchFileName <> '' then
SetFileName(SearchFileName);
end;
procedure TSearcher.SetFileName(const Value: string);
function GetFileInterface: Boolean;
var
Encoding : TFileSaveFormat;
begin
Result := False;
if not FileExists(FFileName) then
Exit;
Result := LoadFileIntoWideStrings(fFileName, fSearchLines, Encoding);
end;
function GetModuleInterface: Boolean;
var
Editor : IEditor;
begin
Result := False;
Editor := GI_EditorFactory.GetEditorByNameOrTitle(FFileName);
if Assigned(Editor) then begin
fSearchLines.Assign(Editor.SynEdit.Lines);
// FSearchStream := TStringStream.Create(Editor.SynEdit.Text);
Result := True;
end;
end;
begin
FFileName := Value;
if not GetModuleInterface and not GetFileInterface then
FFileName := '';
if FFileName <> '' then
Reset;
end;
procedure TSearcher.Reset;
begin
FLineNo := 0;
end;
procedure TSearcher.Execute;
begin
Reset;
DoSearch;
end;
{ TBaseSearcher }
constructor TBaseSearcher.Create;
begin
inherited Create;
FSearchLines := TUnicodeStringList.Create;
FRegExpr := nil;
end;
destructor TBaseSearcher.Destroy;
begin
FreeAndNil(FSearchLines);
FreeAndNil(FRegExpr);
inherited Destroy;
end;
procedure TBaseSearcher.DoSearch;
//resourcestring
// SLineLengthError = 'File Search detected a line longer than %d characters in:'+sLineBreak+
// '%s.' +sLineBreak+
// 'Likely, this is an unsupported binary file type.';
var
i: Integer;
LPos: Integer;
PPos : PWideChar;
begin
SignalStartSearch;
for i := 0 to fSearchLines.Count - 1 do begin
BLine := fSearchLines[i];
if BLine = '' then continue;
if FNoComments then begin
PPos := WStrScan(PWideChar(BLine), '#');
if Assigned(PPos) then begin
LPos := PPos - PWideChar(BLine) + 1;
Delete(BLine, LPos, MaxInt);
end;
if BLine = '' then continue;
if Length(BLine) > SearchLineSize then
begin
// Likely to be a binary file
// Just ingnore Issue 74
//MessageDlg(E.Message, mtWarning, [mbOK], 0);
Exit;
end;
end;
FLineNo := Succ(i);
PatternMatch;
end;
end;
procedure TBaseSearcher.SetPattern(const Value: string);
begin
fPattern := Value;
if soRegEx in SearchOptions then begin
if not Assigned(fRegExpr) then
fRegExpr := TRegExpr.Create();
fRegExpr.ModifierI := not (soCaseSensitive in SearchOptions);
try
fRegExpr.Expression := Value;
fRegExpr.Compile;
except
on E: ERegExpr do
raise Exception.CreateFmt(_(SInvalidRegularExpression), [E.Message]);
end;
end else begin
if not(soCaseSensitive in SearchOptions) then
fPattern := WideUpperCase(Value);
end;
end;
procedure TBaseSearcher.PatternMatch;
{
Use RegExpr if RegExpr soRegEx in SearchOptions.
Else use JclFind or JclSearch depending on whether the search is
case sensitive.
}
var
LinePos: Integer;
Found : Boolean;
EndPos : integer;
FoundPos : PWideChar;
Len : integer;
procedure IsFound;
// Deals with soWholeWord Search option
var
Start: Integer;
TestChar: WideChar;
begin
if soWholeWord in SearchOptions then
begin
Start := LinePos - 1; // Point to previous character
if (Start >= 0) then
begin
TestChar := BLine[Start];
if SynIsCharAlphaNumeric(TestChar) or (TestChar = '_') then
Exit;
end;
TestChar := BLine[EndPos+1]; // Next Character
if TestChar <> #0 then
begin
if SynIsCharAlphaNumeric(TestChar) or (TestChar = '_') then
Exit;
end;
end;
SignalFoundMatch(FLineNo, FSearchLines[FLineNo-1], LinePos, EndPos)
end;
begin
if soRegEx in SearchOptions then begin
Found := fRegExpr.Exec(BLine);
while Found do begin
LinePos := fRegExpr.MatchPos[0];
EndPos := LinePos + fRegExpr.MatchLen[0] - 1;
IsFound;
Found := fRegExpr.ExecNext;
end;
end else begin
if not (soCaseSensitive in SearchOptions) then
BLine := WideUpperCase(BLine);
Len := Length(Pattern);
EndPos := 0;
FoundPos := PWideChar(BLine);
Repeat
FoundPos := WStrPos(FoundPos, PWideChar(fPattern));
Found := Assigned(FoundPos);
if Found then begin
LinePos := FoundPos - PWideChar(BLine) + 1;
EndPos := LinePos + Len - 1;
Inc(FoundPos, Len);
IsFound;
end;
Until not Found;
end;
end;
procedure TBaseSearcher.SignalStartSearch;
begin
if Assigned(FOnStartSearch) then
FOnStartSearch(Self);
end;
procedure TBaseSearcher.SignalFoundMatch(LineNo: Integer; const Line: string;
SPos, EPos: Integer);
begin
if Assigned(FOnFound) then
FOnFound(Self, LineNo, Line, SPos, EPos);
end;
end.