AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Code-Bibliothek Library: Dateien / Laufwerke Delphi Dateiformat einer ( Unicode ) Textdatei ermitteln

Dateiformat einer ( Unicode ) Textdatei ermitteln

Ein Thema von turboPASCAL · begonnen am 18. Jun 2006
Antwort Antwort
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, 13: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
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 11:48 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