-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit1.pas
178 lines (159 loc) · 4.21 KB
/
Unit1.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
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtDlgs, Vcl.ComCtrls, Vcl.ToolWin,
Vcl.StdCtrls, System.ImageList, Vcl.ImgList,
TextStream;
type
TForm1 = class(TForm)
Memo1: TMemo;
ToolBar1: TToolBar;
ToolButton1: TToolButton;
OpenTextFileDialog1: TOpenTextFileDialog;
ImageList1: TImageList;
ToolButton2: TToolButton;
Edit1: TEdit;
CheckBox1: TCheckBox;
procedure FormCreate(Sender: TObject);
procedure ToolButton1Click(Sender: TObject);
procedure ToolButton2Click(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
fileName: string;
lastFound: longint;
fileList: TStringList;
procedure openFile(const FName: string);
function findNext(FindIn: TStrings; const SearchFor: string): longint;
procedure debugShow(const s: string);
protected
procedure WMDropFiles(var Msg: TMessage); message WM_DROPFILES;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses ShellAPI;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpenTextFileDialog1.Filter := 'Text files (*.txt)|*.TXT|CSV files (*.csv)|*.CSV|Any file (*.*)|*.*';
fileList := TStringList.Create;
DragAcceptFiles(Handle, True);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
DragAcceptFiles(Handle, False);
end;
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
if OpenTextFileDialog1.Execute(Form1.Handle) then
begin
openFile(OpenTextFileDialog1.FileName);
end;
end;
procedure TForm1.OpenFile(const FName: string);
var
sLine: string;
i: integer;
tsFile: TTextStream;
begin
fileName := FName;
Memo1.Lines.Clear;
tsFile := TTextStream.Create(TFileStream.Create(Filename, fmOpenRead or fmShareDenyWrite));
tsFile.debug := DebugShow;
try
i := 0;
while (tsFile.ReadLn(sLine)) and (i < 1000) do //Limit for 1000 Lines
begin
Memo1.Lines.Add(sLine);
inc(i);
end;
finally
tsFile.Free;
end;
end;
procedure TForm1.ToolButton2Click(Sender: TObject);
var lineNo, linePos : integer;
begin
lineNo := findNext(Memo1.Lines, edit1.Text);
if lineNo <> -1 then
begin
with Memo1 do
begin
if CheckBox1.Checked then
linePos := pos(uppercase(edit1.Text), uppercase(Memo1.Lines[lineNo]))
else
linePos := pos(edit1.Text, Memo1.Lines[lineNo]);
SelStart := Perform(EM_LINEINDEX, LineNo, 0) + linePos -1;
Memo1.SelLength := length(Edit1.Text);
Perform(EM_SCROLLCARET, 0, 0);
Memo1.SetFocus;
end;
end;
end;
procedure TForm1.Edit1Change(Sender: TObject);
begin
lastFound := -1;
end;
function TForm1.findNext(FindIn: TStrings; const SearchFor: string): longint;
var i: longint;
begin
result := -1;
For i := (lastfound + 1) to FindIn.Count - 1 do
begin
if CheckBox1.Checked then
begin
if pos(UpperCase(SearchFor), Uppercase(FindIn.Strings[i]), 1) <> 0 then
begin
lastfound := i;
result := i;
exit;
end;
end
else
begin
if pos(SearchFor, FindIn.Strings[i], 1) <> 0 then
begin
lastfound := i;
result := i;
exit;
end;
end;
end;
if result = -1 then
begin
lastfound := -1;
showmessage('No further instances of "' + searchfor + '" could be found.');
end;
end;
procedure TForm1.WMDropFiles(var Msg: TMessage);
var
hDrop: THandle;
fileCount: Integer;
nameLen: Integer;
i: Integer;
s: string;
begin
hDrop := Msg.wParam;
fileCount:= DragQueryFile (hDrop , $FFFFFFFF, nil, 0);
for i := 0 to fileCount - 1 do begin
nameLen := DragQueryFile(hDrop, I, nil, 0) + 1;
SetLength(s, NameLen);
DragQueryFile(hDrop, I, Pointer(s), nameLen);
fileList.Add(s);
end;
DragFinish(hDrop);
if fileList.Count > 0 then
begin
openFile(FileList.Strings[0]);
fileList.Clear;
end;
end;
procedure TForm1.DebugShow(const s: string);
begin
ShowMessage(s);
end;
end.