unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, eventlog, FileUtil,
Forms, Controls, Graphics, Dialogs,
StdCtrls, ExtCtrls, Grids, StrUtils;
type
PData = ^TData;
TData = record
FLine : String;
FIndex : Integer;
RCDId : String[4];
ICAO : String[8];
//other data if needed
end;
{ TbtnPath }
TbtnPath = class(TForm)
btnPath: TButton;
btnClose: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
selected_file: TEdit;
edICAO: TEdit;
Label1: TLabel;
Label2: TLabel;
ldAptDotDat: TButton;
Path: TOpenDialog;
g1300Grid: TStringGrid;
G1301Grid: TStringGrid;
procedure btnCloseClick(Sender: TObject);
procedure btnPathClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure ldAptDotDatClick(Sender: TObject);
procedure SetGrids;
procedure ProgramSetup;
function LoadDataFromFile(const AFileName: String): Boolean;
function GetRecordCount(const AFileName: string): Longint;
private
public
end;
Const
// Live Data
// C_FName = 'C:\Files\Apt.Dat';
//Test Data
// C_FName = 'C:\FilesTest\Apt.Dat';
X_FName = 'C:\FilesTest\Apt.Dat';
var
btnPath: TbtnPath;
Data: array of TData;
DataCondition : array of TData;
BlocKSize : LongInt = 0;
C_FName : String = '';
FileName : String = '';
implementation
{$R *.lfm}
procedure TbtnPath.FormCreate(Sender: TObject);
Var
Loc : String = '';
PGMName : String = '';
begin
loc := Application.Location;
PGMName := Application.ExeName;
Caption := 'APTDotDat Array Load ' + Loc + PGMName;
end;
procedure TbtnPath.FormShow(Sender: TObject);
Var
Loc : String = '';
PGMName : String = '';
begin
loc := Application.Location;
PGMName := Application.ExeName;
Caption := 'APTDotDat Array Load ' + Loc + PGMName;
ProgramSetup ;
end;
procedure TbtnPath.ProgramSetup;
begin
SetGrids;
end;
procedure TbtnPath.ldAptDotDatClick(Sender: TObject);
var
Len, I: Integer;
Valid : Boolean = False;
// Debug
LowNbr : Longint = 0;
HighNbr : Longint = 0;
Item : String = '';
begin
Data := nil;
DataCondition := nil;
Valid := FileExists(FileName);
if Not Valid then begin
MessageDlg('Cannot load file! ', mtError, [mbOk], 0);
Exit;
end;
Valid := LoadDataFromFile(FileName);
// for some reason you wish to start from
// the last record,
// I saw it in one of your previous thread
for I := Low(Data) to High(Data) do begin
//check for some conditions, if the conditions are met copy
//the record/line to another array
//DataCondition(Listbox2,3 equivalent)
//you can define more arrays/records if needed)
Len := Length(DataCondition);
SetLength(DataCondition, Len + 1);
DataCondition[Len].FLine := Data[I].FLine;
// keep the original index for later reference,
// maybe you wish to delete from the file
DataCondition[Len].FIndex := Data[I].FIndex;
end;
HighNbr := High(Data);
Item := Data[HighNbr].Fline;
Edit1.Text := Item;
end;
function TbtnPath.LoadDataFromFile(const AFileName: String): Boolean;
var
DataFile : TextFile;
Counter : Integer = 0;
Line : String = '';
RCD : String = '';
Len : Integer;
i : Integer;
NeedtoExit : Boolean = False;
begin
Result := True;
AssignFile(DataFile, AFileName);
try
Reset(DataFile);
SetLength(Data, BlockSize); // set here
while not eof(DataFile) do
begin
Readln(DataFile, Line);
Data[Counter].ICAO := '';
Data[Counter].RCDId := Copy2Space(Line);
RCD := Data[Counter].RCDId;
Case RCD of
'1' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']);
'16' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']);
'17' : Data[Counter].ICAO := ExtractWord(5, Line, [' ']);
end;
Data[Counter].FLine := Line;
Data[Counter].FIndex := Len;
Inc(Counter);
Edit3.Text := intToStr(Counter);
Application.ProcessMessages;
end;
CloseFile(DataFile);
except
Result := False;
end;
end;
function TbtnPath.GetRecordCount(const AFileName: string): Longint;
var
DataFile: TextFile;
LstRcd : String = '';
begin
Result := 0;
AssignFile(DataFile, AFileName);
try
Reset(DataFile);
while not eof(DataFile) do begin
// this line has changed, basically it
// reads nothing but is needed otherwise
// we go into an endless loop
Readln(DataFile);
Inc(Result);
end;
BlocKSize := Result;
CloseFile(DataFile);
except
//...
end;
Edit2.Text := IntToStr(BlockSize);
end;
procedure TbtnPath.btnCloseClick(Sender: TObject);
begin
Close;
end;
procedure TbtnPath.SetGrids;
Var i : integer =-1;
Begin
with G1300Grid do begin
Clear;
ColWidths[0] := 50;
//ColWidths[1] := 50;
//ColWidths[2] := 100;
//ColWidths[3] := 100;
//ColWidths[4] := 65;
//ColWidths[5] := 100;
//ColWidths[6] := 200;
// Set the alignments
Columns[0].Alignment := taRightJustify;
// Align[1] := taRightJustify;
// G1300Grid.Align[2] := taRightJustify;
//G1300Grid.Align[3] := taRightJustify;
//G1300Grid.Align[4] := taRightJustify;
//G1300Grid.Align[5] := taRightJustify;
//G1300Grid.Align[6] := taRightJustify;
// Set the header's texts
// Set the header's alignments
for i := 0 to 6 do begin
Columns[i].Title.Alignment := taCenter;
end;
Columns[0].Title.Caption := 'Ref:';
Columns[1].Title.Caption := 'RCID';
Columns[2].Title.Caption := 'Lat';
Columns[3].Title.Caption := 'Lon';
Columns[4].Title.Caption := 'Alt';
Columns[5].Title.Caption := 'Type';
Columns[6].Title.Caption := 'Info';
end;
end;
procedure TbtnPath.btnPathClick(Sender: TObject);
begin
If Path.Execute then begin selected_file.Caption:= Path.FileName; end;
FileName := Path.FileName;
if FileExists(FileName) then begin
BlockSize := GetRecordCount(FileName);
end;
end;
end.