Einzelnen Beitrag anzeigen

Benutzerbild von turboPASCAL
turboPASCAL

Registriert seit: 8. Mai 2005
Ort: Sondershausen
4.274 Beiträge
 
Delphi 6 Personal
 
#1

Dateiformat einer ( Unicode ) Textdatei ermitteln

  Alt 18. Jun 2006, 12:16
Um herauszufinden ob eine Textdatei in einem Unicode Format vorliegt,
kann mit folgender Funktion ermittelt werden:

Delphi-Quellcode:
type
  TTextFileType = (NONE, UTF_8, UNI_LE, UNI_BE, UNI4_LE, UNI4_BE, RTF);

function GetTextfileType(Filename : string): TTextFileType;
var
  F: File;
  FSignatur: Array [0..3] of Byte;
begin
  // Read Filesignature
  try
    AssignFile(F, FileName);
    Reset(F, 1);
    Blockread(F, FSignatur, SizeOf(FSignatur));
  finally
    CloseFile(F);
  end;

  // UniCode (UTF-8)
  if (FSignatur[0] = $EF) and (FSignatur[1] = $BB) and (FSignatur[2] = $BF) then
  begin
    Result := UTF_8;
  end else
  // UniCode4 (Low Endian)
  if (FSignatur[0] = $FF) and (FSignatur[1] = $FE) and
     (FSignatur[2] = $00) and (FSignatur[3] = $00) then
  begin
    Result := UNI4_LE;
  end else
  // UniCode4 (Big Endian)
  if (FSignatur[0] = $00) and (FSignatur[1] = $00) and
     (FSignatur[2] = $FE) and (FSignatur[3] = $FF) then
  begin
    Result := UNI4_BE;
  end else
  // UniCode (Low Endian)
  if (FSignatur[0] = $FF) and (FSignatur[1] = $FE) then
  begin
    Result := UNI_LE;
  end else
  // UniCode (Big Endian)
  if (FSignatur[0] = $FE) and (FSignatur[1] = $FF) then
  begin
    Result := UNI_BE;
  end else
  // RTF Document (Bonus ;-) )
  if (FSignatur[0] = $7B) and (FSignatur[1] = $5C) and (FSignatur[2] = $72) then
  begin
    Result := RTF;
  end else
    Result := NONE; // Any Text or a File of Byte...
end;

Beispielaufruf:
Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
var
  s: String;
begin
  Case GetTextfileType('Text.txt') of
    NONE : s := 'unbkannte';
    UTF_8 : s := 'UTF-8';
    UNI_LE : s := 'UniCode - Low Endian';
    UNI_BE : s := 'UniCode - Big Endian)';
    UNI4_LE : s := 'UniCode 4 - Low Endian';
    UNI4_BE : s := 'UniCode 4 - Big Endian';
    RTF : s := 'Rich-Text (RTF)';
  end;
  MessageBox(Handle, PChar(format('Die Datei hat eine %s Signatur.', [s])),
    'Information', MB_ICONINFORMATION or MB_OK);
end;
Matti
Meine Software-Projekte - Homepage - Grüße vom Rüsselmops -Mops Mopser
  Mit Zitat antworten Zitat