isUnicode/de

From Lazarus wiki
Revision as of 06:50, 29 August 2013 by Olaf (talk | contribs) (Created page with "{{isUnicode}} <br> <br> Das Unterprogramm prüft, unabhängig vom zugrunde liegenden Betriebssystem, ob es sich um eine Unicode-Datei handelt.<br> Das Unterprogramm ist nur da...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Deutsch (de)


Das Unterprogramm prüft, unabhängig vom zugrunde liegenden Betriebssystem, ob es sich um eine Unicode-Datei handelt.
Das Unterprogramm ist nur dann erfolgreich, wenn die Unicode-Datei eine BOM (ByteOderMark) hat.
Unicode-Dateien ohne eine BOM können nicht zweifelsfrei als Unicode-Dateien identifiziert werden.

uses
  FileUtil, ...;

  ...

const
  conNoUnicode = 0;
  conUTF1 = 1;
  conUTF7 = 2;
  conUTF8 = 3;
  conUTF16BigEndian = 4;
  conUTF16LittleEndian = 5;
  conUTF32BigEndian = 6;
  conUTF32LittleEndian = 7;
  conUTFEBCDIC = 8;         // Format auf IBM-Großrechnern
  conSCSU = 9;              // Standard Compression Scheme for Unicode
  conBOCU1 = 10;            // Binary Ordered Compression for Unicode
  conGB18030 = 11;          // chinesische Zeichenkodierungsstandard (GB 18030)

  ...

function IsUnicode(const conStrQuellDateiname: string): integer;
var
  txtQuelldatei: file of char;
  chrZeichen: char;
  intI: integer = 0;
  strBOM: string = '';

begin

  Result := conNoUnicode;

  AssignFile(txtQuelldatei, UTF8ToSys(conStrQuellDateiname));
  Reset(txtQuelldatei);

  while not EOF(txtQuelldatei) or (intI < 4) do
  begin
    intI := intI + 1;
    Read(txtQuelldatei, chrZeichen);
    strBOM := strBOM + chrZeichen;
  end;

  closefile(txtQuelldatei);

  if Copy(strBOM, 1, 3) = #$F7#$64#$4C then
    Result := conUTF1
  else if Copy(strBOM, 1, 3) = #$2B#$2F#$76 then
    Result := conUTF7
  else if Copy(strBOM, 1, 3) = #$EF#$BB#$BF then
    Result := conUTF8
  else if Copy(strBOM, 1, 2) = #$FE#$FF then
    Result := conUTF16BigEndian
  else if Copy(strBOM, 1, 2) = #$FF#$FE then
    Result := conUTF16LittleEndian
  else if strBOM = #$00#$00#$FE#$FF then
    Result := conUTF32BigEndian
  else if strBOM = #$FF#$FE#$00#$00 then
    Result := conUTF32LittleEndian
  else if strBOM = #$DD#$73#$66#$73 then
    Result := conUTFEBCDIC
  else if Copy(strBOM, 1, 3) = #$0E#$FE#$FF then
    Result := conSCSU
  else if Copy(strBOM, 1, 3) = #$FB#$EE#$28 then
    Result := conBOCU1
  else if strBOM = #$FB#$EE#$28#$FF then
    Result := conBOCU1
  else if strBOM = #$84#$31#$59#$33 then
    Result := conGB18030
  else
    Result := conNoUnicode;

end;


Aufruf unter Windows:

  ...

  case IsUnicode('E:\Test.txt') of
    ...

  ...


Aufruf unter Linux:

  ...

  case IsUnicode('/home/user/Test.txt') of
    ...

  ...



--Olaf 07:50, 29 August 2013 (CEST)