Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Dateien / Laufwerke (https://www.delphipraxis.net/41-library-dateien-laufwerke/)
-   -   Delphi Dateiformat einer ( Unicode ) Textdatei ermitteln (https://www.delphipraxis.net/71607-dateiformat-einer-unicode-textdatei-ermitteln.html)

turboPASCAL 18. Jun 2006 12:16


Dateiformat einer ( Unicode ) Textdatei ermitteln
 
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;


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:26 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