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
|
Type
TTxtFileType = (txTxtOrBin{Default},
txUTF8,
txUNICODE,
txUNIBIG
);
TTxtFileSign = record
Code : TTxtFileType;
Sign : LongWord;
Mask : LongWord;
end;
const
TxtSignsCount = 3;
TxtSign_UTF8 : TTxtFileSign = (Code: txUTF8; Sign:$00BFBBEF; Mask: $00FFFFFF);
TxtSign_UNICODE : TTxtFileSign = (Code: txUNICODE; Sign:$0000FEFF; Mask: $FF00FFFF);
TxtSign_UNIBIG : TTxtFileSign = (Code: txUNIBIG; Sign:$0000FFFE; Mask: $00FFFFFF);
var
TxtFileSigns : array[0..TxtSignsCount-1] of TTxtFileSign;
type
TForm1 = class(TForm)
Button1: TButton;
op: TOpenDialog;
Label1: TLabel;
Label2: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function TypeOfTextfile(const filename : string) : TTxtFileType;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.TypeOfTextfile(const filename: string): TTxtFileType;
var
Flux : TFileStream;
Sign : LongWord;
N : integer;
begin
Flux := TFileStream.Create(FileName, fmOpenRead);
try
{# 1st With BOOM #}
Flux.Read(Sign, 4);
finally
Flux.Free;
end;
{# Default # }
result := txTxtOrBin;
for N := 0 to High(TxtFileSigns) do
begin
if (Sign and TxtFileSigns[N].Mask) = TxtFileSigns[N].Sign then
begin
result := TxtFileSigns[N].Code;
break;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if not op.Execute then exit;
Case TypeOfTextfile(op.FileName) of
txTxtOrBin:Label2.Caption:='TxtOrBin';
txUTF8:Label2.Caption:='UTF8';
txUNICODE:Label2.Caption:='UNICODE';
txUNIBIG:Label2.Caption:='UNIBIG';
end;
end; |
Partager