AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

File Types Detection

Ein Thema von mohfa · begonnen am 8. Mai 2009 · letzter Beitrag vom 8. Mai 2009
Antwort Antwort
mohfa

Registriert seit: 11. Feb 2007
97 Beiträge
 
Delphi 7 Enterprise
 
#1

File Types Detection

  Alt 8. Mai 2009, 07:00
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.
Angehängte Dateien
Dateityp: zip file_type_detect_103.zip (4,1 KB, 2x aufgerufen)
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#2

Re: File Types Detection

  Alt 8. Mai 2009, 09:05
Maybe you should read this: http://blogs.msdn.com/oldnewthing/ar...7/2158334.aspx
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 02:31 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