procedure TForm1.FormShow(Sender: TObject);
var
t: TDateTime;
begin
t := Now;
if LoadDatafromFile(C_FileName) then begin
t := Now - t;
ListView1.Items.Count := Length(FData);
ListView1.Invalidate;
Statusbar1.SimpleText := Format('%.0n items read in %s s', [
Length(FData)*1.0, FormatDateTime('s.zzz', t)]);
end else
StatusBar1.SimpleText := 'Error';
DefaultColumWidth;
end;
function TForm1.LoadDataFromFile(const AFileName: String): Boolean;
const
BLOCK_SIZE = 1000; // prepare block for 1000 records
var
DataFile : TextFile;
Line : String;
Counter : Integer = 0;
Bit1 : String;
p : Integer;
Empty : Boolean;
begin
Result := false;
if not FileExists(AFileName) then begin MessageDlg(Format('File "%s" not found.', [AFileName]), mtError, [mbOK], 0); exit; end;
SetLength(FData, 0);
AssignFile(DataFile, AFileName);
try
Reset(Datafile);
try
while not EoF(DataFile) do begin
ReadLn(DataFile, Line);
// When the counter is 0, 1000, 2000, ... the preallocated array is full
// Extend its length for another 1000 records.
if Counter mod BLOCK_SIZE = 0 then
SetLength(FData, Length(FData) + BLOCK_SIZE);
FData[Counter].LIDX := Counter;
Line := Trim(Line);
// Bit1 := Copy2Space(Line); {ICAO}
Bit1 := ExtractWord(1,Line,['|']);
FData[Counter].ICAO := Bit1;
Bit1 := ExtractWord(5,Line,['|']);
Empty := IsEmptyStr(Bit1, [' ']);
if Empty then FData[Counter].City := 'Nil'
else FData[Counter].City := Bit1;
Bit1 := ExtractWord(6,Line,['|']);
Empty := IsEmptyStr(Bit1, [' ']);
if Empty then FData[Counter].Country := 'Nil'
else FData[Counter].Country := Bit1;
Bit1 := ExtractWord(7,Line,['|']);
Empty := IsEmptyStr(Bit1, [' ']);
if Empty then FData[Counter].Region := 'Nil'
else FData[Counter].Region := Bit1;
Bit1 := ExtractWord(3,Line,['|']);
FData[Counter].Lat := Bit1;
Bit1 := ExtractWord(4,Line,['|']);
FData[Counter].Lon := Bit1;
Bit1 := ExtractWord(2,Line,['|']);
FData[Counter].HASH := Bit1;
Inc(Counter);
end;
// Now that all records are read we know their count and we can trim
// the data array to its final length
SetLength(FData, Counter);
Result := true;
except
SetLength(FData, 0);
MessageDlg(Format('Error reading file "%s"', [AFileName]), mtError, [mbOK], 0);
end;
finally
CloseFile(DataFile);
end;
end;