procedure HaveBOM(aStream: TStream);
type
TBummer = record Name:String;BOM:RawByteString;end;
const
Bummer: array[0..14] of TBummer =
(
(Name: 'UTF-7' ; BOM: #$2B#$2F#$76#$38#$2D),
(Name: 'UTF-32 (BE)'; BOM: #$00#$00#$FE#$FF),
(Name: 'UTF-32 (LE)'; BOM: #$FF#$FE#$00#$00),
(Name: 'UTF-7' ; BOM: #$2B#$2F#$76#$38),
(Name: 'UTF-7' ; BOM: #$2B#$2F#$76#$39),
(Name: 'UTF-7' ; BOM: #$2B#$2F#$76#$2B),
(Name: 'UTF-7' ; BOM: #$2B#$2F#$76#$2F),
(Name: 'UTF-EBCDIC' ; BOM: #$DD#$73#$66#$73),
(Name: 'GB-18030' ; BOM: #$84#$31#$95#$33),
(Name: 'UTF-8' ; BOM: #$EF#$BB#$BF),
(Name: 'UTF-1' ; BOM: #$F7#$64#$4C),
(Name: 'SCSU' ; BOM: #$0E#$FE#$FF),
(Name: 'BOCU-1' ; BOM: #$FB#$EE#$28),
(Name: 'UTF-16 (BE)'; BOM: #$FE#$FF),
(Name: 'UTF-16 (LE)'; BOM: #$FF#$FE)
);
var
Buffer : AnsiString;
Bum : TBummer;
begin
if AStream.Size > 4 then SetLength(Buffer, 5) else
begin
Print('BOM detection not good but try');
SetLength(Buffer, AStream.Size);
end;
AStream.Read(Buffer[1], Length(Buffer));
for BUM in Bummer do
begin
if Buffer.StartsWith(BUM.BOM) then
begin
Print('file have bom ', BUM.Name);
exit;
end;
end;
Print('file have not BOM');
// no BOM and reset stream
AStream.Position := AStream.Position - Length(Buffer);
end;
procedure procfiles(Filenames: array of string);
var
FileStream : TFileStream;
Filename : string;
Lines : TStringList;
begin
Lines:= TStringList.Create;
for Filename in Filenames do
begin
Print('--------------------------------');
Print('proc file %s', [filename]);
Print('--------------------------------');
FileStream:= TFileStream.Create(Filename, fmOpenRead);
HaveBOM(FileStream);
Lines.Clear;
Lines.LoadFromStream(FileStream);
FileStream.Free;
proclines(lines); // this use example process file JLWest. no here
end;
Lines.Free;
end;
begin
procfiles(['ByAirport.txt','Composite.txt']);
end.