Автоопределение кодировки ANSI-OEM



- Маша, давай прошвырнемся в кино?
- ?
- Hу давай хоть в кабак сходим.
- ? - А-а, понял, кодировка глючит...


const
l3_csANSI = 0;
{-признак кодировки ANSII}
l3_csOEM = 255;
{-признак кодировки OEM}
type
cc_Graph_CriteriaRange = #176..#223; {-критерий для определения псевдографики}
TChars = set of char;
Long = LongInt;
const
cc_OEM_CriteriaEx = [#128..#175] + [#224..#239];
cc_ANSI_CriteriaEx = [#192..#255];
cc_Graph_Criteria = [Low(cc_Graph_CriteriaRange)..High(cc_Graph_CriteriaRange)];
type
T_cc_GraphCounts = array [cc_Graph_CriteriaRange] of Longint;
procedure l3AnalizeCharSetEx(var Buf: PChar; BufEnd: PChar;
var OEMCount, ANSICount, GraphCount: Long;
var GraphCounts: T_cc_GraphCounts);
var
C : Char;
begin
OEMCount := 0;
ANSICount := 0;
GraphCount := 0;
for C := Low(T_cc_GraphCounts) to High(T_cc_GraphCounts) do GraphCounts[C] := 0;
while (Buf <  BufEnd) do begin
C := Buf^;
Inc(Buf);
if (C in cc_OEM_CriteriaEx) then Inc(OEMCount);
if (C in cc_ANSI_CriteriaEx) then Inc(ANSICount);
if (C in cc_Graph_Criteria) then begin
Inc(GraphCounts[C]);
Inc(GraphCount);
end;
end;{Buf <  BufEnd}
end;
function l3AnalizeCharSetExEx(Buf, BufEnd: PChar): Byte;
var
OEMCount : Long;
ANSICount : Long;
GraphCount : Long;
GraphCount_2: Long;
GraphCounts : T_cc_GraphCounts;
C : Char;
begin
l3AnalizeCharSetEx(Buf, BufEnd, OEMCount, ANSICount, GraphCount,GraphCounts);
if (OEMCount >  ANSICount) then
Result := l3_csOEM
else if (GraphCount > = ANSICount) then begin
Result := 0;
GraphCount_2 := GraphCount div 2;
for C := Low(T_cc_GraphCounts) to High(T_cc_GraphCounts) do begin
If (GraphCounts[C] >  GraphCount_2) then begin
Result := l3_csOEM;
break;
end;{GraphCounts[C] >  ..}
end;{for C}
end else Result := 0;
end;
function l3AnalizeCharSetBuf(Buf: PChar; Len: Long): Byte;
begin
Result := l3AnalizeCharSetExEx(Buf, Buf + Len);
end;


Далее: Как можно автоматически опpеделить кодиpовкy текста »»