Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi File Types Detection (https://www.delphipraxis.net/133762-file-types-detection.html)

mohfa 8. Mai 2009 07:00


File Types Detection
 
Liste der Anhänge anzeigen (Anzahl: 1)
To detect What a File ( Plain Text Files for Example)Type and Encoding I use the Following .

Is this Function Correct ? any other improvements .

Delphi-Quellcode:
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 }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


function TypeOfTextfile(const filename: string): TTxtFileType;
var
  Flux : TFileStream;
  Sign : LongWord;
  N   : integer;
begin

  Flux := TFileStream.Create(FileName, fmOpenRead);
  try
    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;

end.

Luckie 8. Mai 2009 09:05

Re: File Types Detection
 
Maybe you should read this: http://blogs.msdn.com/oldnewthing/ar...7/2158334.aspx


Alle Zeitangaben in WEZ +1. Es ist jetzt 01:35 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz