unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Dialogs, StdCtrls,
ComCtrls, ExtCtrls, LCLType, LCLIntf;
type
{ TForm1 }
TForm1 = class(TForm)
btnGenerate: TButton;
btnSearch: TButton;
Label1: TLabel;
Memo1: TMemo;
Panel1: TPanel;
ProgressBar1: TProgressBar;
procedure btnGenerateClick(Sender: TObject);
procedure btnSearchClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
function RandomChars(Count: Integer): string;
end;
var
Form1: TForm1;
implementation
const
DataFileName = 'Data.txt';
{$R *.lfm}
{ TForm1 }
procedure TForm1.btnGenerateClick(Sender: TObject);
const
MaxRecordCount = 10000000;
var
OutputFile : TextFile;
i : LongInt;
begin
// Preparing
btnSearch.Enabled := False;
Panel1.Caption := 'Generating ' + MaxRecordCount.ToString + ' items';
Panel1.Visible := True;
ProgressBar1.Min := 0;
ProgressBar1.Max := MaxRecordCount;
ProgressBar1.Style := pbstNormal;
AssignFile(OutputFile, DataFileName);
Rewrite(OutputFile);
// Generating
for i := 1 to MaxRecordCount do
begin
WriteLn(OutputFile, 'Item' + i.ToString + ' ' + RandomChars(8));
ProgressBar1.Position := i;
if (i mod 50) <> 0 then Continue; // this line is for better performance
Application.ProcessMessages;
if GetKeyState(VK_ESCAPE) < 0 then Break; // Allow user cancellation
end;
// Finishing
CloseFile(OutputFile);
ShowMessage(ProgressBar1.Position.ToString + ' items generated.');
Panel1.Visible := False;
btnSearch.Enabled := True;
Memo1.Clear;
end;
procedure TForm1.btnSearchClick(Sender: TObject);
var
InputFile : TextFile;
Count : LongInt;
S : string;
begin
if not(FileExists(DataFileName)) then
begin
ShowMessage('Data file not found.' + LineEnding +
'Please generate the data first.');
Exit;
end;
// Preparing
btnGenerate.Enabled := False;
Panel1.Caption := 'Searching';
Panel1.Visible := True;
ProgressBar1.Min := 0;
ProgressBar1.Max := 100;
ProgressBar1.Style := pbstMarquee;
Memo1.Visible := False;
Memo1.Clear;
AssignFile(InputFile, DataFileName);
Reset(InputFile);
Count := 0;
// Searching
while not EOF(InputFile) do
begin
ReadLn(InputFile, S);
if Pos('hi', S) > 0 then
begin
Memo1.Append(S);
Inc(Count);
if (Count mod 50) <> 0 then Continue; // this line is for better performance
Application.ProcessMessages;
if GetKeyState(VK_ESCAPE) < 0 then Break; // Allow user cancellation
end;
end;
// Finishing
CloseFile(InputFile);
ShowMessage('Found ' + Count.ToString + ' records containing ''hi''');
Memo1.Visible := True;
Panel1.Visible := False;
btnGenerate.Enabled := True;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Memo1.Clear ;
Memo1.ScrollBars := ssAutoVertical;
ProgressBar1.BorderSpacing.Around := 4;
ProgressBar1.Align := alBottom;
Panel1.Align := alBottom;
Panel1.Visible := False;
end;
function TForm1.RandomChars(Count: Integer): string;
var
S: string;
i: Integer;
begin
if Count < 1 then Count := 1;
if Count > 20 then Count := 20;
S := '';
for i := 1 to Count do
S := S + chr(Ord('a')+Random(26));
Result := S;
end;
end.