Difference between revisions of "isUnicode/de"
From Lazarus wiki
Jump to navigationJump to searchm |
m |
||
Line 42: | Line 42: | ||
Reset(txtQuelldatei); | Reset(txtQuelldatei); | ||
− | while not EOF(txtQuelldatei) or (intI < | + | // Prüft, ob die Datei wenigstens 4 Zeichen enthält, |
+ | // um die BOM bestimmen zu können | ||
+ | if FileSize(conStrQuellDateiname) < 5 then | ||
+ | exit; | ||
+ | |||
+ | while not EOF(txtQuelldatei) or (intI < 5) do | ||
begin | begin | ||
intI := intI + 1; | intI := intI + 1; | ||
Line 53: | Line 58: | ||
if Copy(strBOM, 1, 3) = #$F7#$64#$4C then | if Copy(strBOM, 1, 3) = #$F7#$64#$4C then | ||
Result := conUTF1 | Result := conUTF1 | ||
− | else if Copy(strBOM, 1, | + | else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$38 then |
+ | Result := conUTF7 | ||
+ | else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$39 then | ||
+ | Result := conUTF7 | ||
+ | else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$2B then | ||
+ | Result := conUTF7 | ||
+ | else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$2F then | ||
Result := conUTF7 | Result := conUTF7 | ||
else if Copy(strBOM, 1, 3) = #$EF#$BB#$BF then | else if Copy(strBOM, 1, 3) = #$EF#$BB#$BF then |
Revision as of 09:17, 15 September 2013
│
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 (Byte-Order-Markierung) 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);
// Prüft, ob die Datei wenigstens 4 Zeichen enthält,
// um die BOM bestimmen zu können
if FileSize(conStrQuellDateiname) < 5 then
exit;
while not EOF(txtQuelldatei) or (intI < 5) 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, 4) = #$2B#$2F#$76#$38 then
Result := conUTF7
else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$39 then
Result := conUTF7
else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$2B then
Result := conUTF7
else if Copy(strBOM, 1, 4) = #$2B#$2F#$76#$2F 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)