| 1 |
// +----------------------------------------------------------------------+ |
| 2 |
// | chsdet - Charset Detector Library | |
| 3 |
// +----------------------------------------------------------------------+ |
| 4 |
// | Copyright (C) 2006, Nick Yakowlew http://chsdet.sourceforge.net | |
| 5 |
// +----------------------------------------------------------------------+ |
| 6 |
// | Based on Mozilla sources http://www.mozilla.org/projects/intl/ | |
| 7 |
// +----------------------------------------------------------------------+ |
| 8 |
// | This library is free software; you can redistribute it and/or modify | |
| 9 |
// | it under the terms of the GNU General Public License as published by | |
| 10 |
// | the Free Software Foundation; either version 2 of the License, or | |
| 11 |
// | (at your option) any later version. | |
| 12 |
// | This library is distributed in the hope that it will be useful | |
| 13 |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 15 |
// | See the GNU Lesser General Public License for more details. | |
| 16 |
// | http://www.opensource.org/licenses/lgpl-license.php | |
| 17 |
// +----------------------------------------------------------------------+ |
| 18 |
// |
| 19 |
// $Id: nsUniversalDetector.pas,v 1.7 2013/05/16 15:41:14 ya_nick Exp $ |
| 20 |
|
| 21 |
unit nsUniversalDetector; |
| 22 |
|
| 23 |
interface |
| 24 |
uses |
| 25 |
{$I dbg.inc} |
| 26 |
nsCore, |
| 27 |
CustomDetector; |
| 28 |
|
| 29 |
const |
| 30 |
NUM_OF_CHARSET_PROBERS = 4; |
| 31 |
|
| 32 |
type |
| 33 |
eInputState = ( |
| 34 |
isPureAscii = 0, |
| 35 |
isEscAscii = 1, |
| 36 |
isHighbyte = 2 |
| 37 |
); |
| 38 |
|
| 39 |
TnsUniversalDetector = class(TObject) |
| 40 |
protected |
| 41 |
mInputState: eInputState; |
| 42 |
mDone: Boolean; |
| 43 |
mStart: Boolean; |
| 44 |
mGotData: Boolean; |
| 45 |
mLastChar: AnsiChar; |
| 46 |
mDetectedCharset: eInternalCharsetID; |
| 47 |
mCharSetProbers: array[0..Pred(NUM_OF_CHARSET_PROBERS)] of TCustomDetector; |
| 48 |
mEscCharSetProber: TCustomDetector; |
| 49 |
mDetectedBOM: eBOMKind; |
| 50 |
|
| 51 |
procedure Report(aCharsetID: eInternalCharsetID); |
| 52 |
function CheckBOM(aBuf: pAnsiChar; aLen: integer): integer; |
| 53 |
function GetCharsetID(CodePage: integer): eInternalCharsetID; |
| 54 |
procedure DoEnableCharset(Charset: eInternalCharsetID; SetEnabledTo: Boolean); |
| 55 |
public |
| 56 |
constructor Create; |
| 57 |
destructor Destroy; override; |
| 58 |
|
| 59 |
procedure Reset; |
| 60 |
function HandleData(aBuf: pAnsiChar; aLen: integer): nsResult; |
| 61 |
procedure DataEnd; |
| 62 |
|
| 63 |
function GetDetectedCharsetInfo: nsCore.rCharsetInfo; |
| 64 |
|
| 65 |
function GetKnownCharset(out KnownCharsets: String): integer; |
| 66 |
procedure GetAbout(out About: rAboutHolder); |
| 67 |
procedure DisableCharset(CodePage: integer); |
| 68 |
|
| 69 |
property Done: Boolean read mDone; |
| 70 |
property BOMDetected: eBOMKind read mDetectedBOM; |
| 71 |
end; |
| 72 |
|
| 73 |
implementation |
| 74 |
uses |
| 75 |
SysUtils, |
| 76 |
nsGroupProber, |
| 77 |
nsMBCSMultiProber, |
| 78 |
nsSBCSGroupProber, |
| 79 |
nsEscCharsetProber, |
| 80 |
nsLatin1Prober, |
| 81 |
MBUnicodeMultiProber; |
| 82 |
|
| 83 |
const |
| 84 |
MINIMUM_THRESHOLD: float = 0.20; |
| 85 |
|
| 86 |
AboutInfo: rAboutHolder = ( |
| 87 |
MajorVersionNr: 0; |
| 88 |
MinorVersionNr: 2; |
| 89 |
BuildVersionNr: 8; |
| 90 |
About: 'Charset Detector Library. Copyright (C) 2006 - 2013, Nick Yakowlew. http://chsdet.sourceforge.net'; |
| 91 |
); |
| 92 |
{ TnsUniversalDetector } |
| 93 |
|
| 94 |
constructor TnsUniversalDetector.Create; |
| 95 |
begin |
| 96 |
inherited Create; |
| 97 |
|
| 98 |
mCharSetProbers[0] := TnsMBCSMultiProber.Create; |
| 99 |
mCharSetProbers[1] := TnsSBCSGroupProber.Create; |
| 100 |
mCharSetProbers[2] := TnsLatin1Prober.Create; |
| 101 |
mCharSetProbers[3] := TMBUnicodeMultiProber.Create; |
| 102 |
mEscCharSetProber := TnsEscCharSetProber.Create; |
| 103 |
Reset; |
| 104 |
end; |
| 105 |
|
| 106 |
destructor TnsUniversalDetector.Destroy; |
| 107 |
var |
| 108 |
i: integer; |
| 109 |
begin |
| 110 |
for i := 0 to Pred(NUM_OF_CHARSET_PROBERS) do |
| 111 |
mCharSetProbers[i].Free; |
| 112 |
|
| 113 |
mEscCharSetProber.Free; |
| 114 |
|
| 115 |
inherited; |
| 116 |
end; |
| 117 |
|
| 118 |
procedure TnsUniversalDetector.DataEnd; |
| 119 |
var |
| 120 |
proberConfidence: float; |
| 121 |
maxProberConfidence: float; |
| 122 |
maxProber: int32; |
| 123 |
i: integer; |
| 124 |
begin |
| 125 |
if not mGotData then |
| 126 |
(* we haven't got any data yet, return immediately *) |
| 127 |
(* caller program sometimes call DataEnd before anything has been sent to detector*) |
| 128 |
exit; |
| 129 |
|
| 130 |
if mDetectedCharset <> UNKNOWN_CHARSET then |
| 131 |
begin |
| 132 |
mDone := TRUE; |
| 133 |
Report(mDetectedCharset); |
| 134 |
exit; |
| 135 |
end; |
| 136 |
case mInputState of |
| 137 |
isHighbyte: |
| 138 |
begin |
| 139 |
maxProberConfidence := 0.0; |
| 140 |
maxProber := 0; |
| 141 |
for i := 0 to Pred(NUM_OF_CHARSET_PROBERS) do |
| 142 |
begin |
| 143 |
proberConfidence := mCharSetProbers[i].GetConfidence; |
| 144 |
if proberConfidence > maxProberConfidence then |
| 145 |
begin |
| 146 |
maxProberConfidence := proberConfidence; |
| 147 |
maxProber := i; |
| 148 |
end; |
| 149 |
end; |
| 150 |
(*do not report anything because we are not confident of it, that's in fact a negative answer*) |
| 151 |
if maxProberConfidence > MINIMUM_THRESHOLD then |
| 152 |
Report(mCharSetProbers[maxProber].GetDetectedCharset); |
| 153 |
end; |
| 154 |
isEscAscii: |
| 155 |
begin |
| 156 |
mDetectedCharset := mEscCharSetProber.GetDetectedCharset; |
| 157 |
end; |
| 158 |
else |
| 159 |
begin |
| 160 |
mDetectedCharset := PURE_ASCII_CHARSET; |
| 161 |
end; |
| 162 |
end; {case} |
| 163 |
{$IFDEF DEBUG_chardet} |
| 164 |
AddDump('Universal detector - DataEnd'); |
| 165 |
{$ENDIF} |
| 166 |
end; |
| 167 |
|
| 168 |
function TnsUniversalDetector.HandleData(aBuf: pAnsiChar; aLen: integer): nsResult; |
| 169 |
var |
| 170 |
i: integer; |
| 171 |
st: eProbingState; |
| 172 |
begin |
| 173 |
if mDone then |
| 174 |
begin |
| 175 |
Result := NS_OK; |
| 176 |
exit; |
| 177 |
end; |
| 178 |
if aLen > 0 then |
| 179 |
mGotData := TRUE; |
| 180 |
|
| 181 |
(*If the data starts with BOM, we know it is UTF*) |
| 182 |
if mStart then |
| 183 |
begin |
| 184 |
mStart := FALSE; |
| 185 |
if CheckBOM(aBuf, aLen) > 0 then |
| 186 |
begin |
| 187 |
case mDetectedBOM of |
| 188 |
BOM_UTF8: mDetectedCharset := UTF8_CHARSET; |
| 189 |
BOM_UTF16_LE: mDetectedCharset := UTF16_LE_CHARSET; |
| 190 |
BOM_UTF16_BE: mDetectedCharset := UTF16_BE_CHARSET; |
| 191 |
BOM_UCS4_LE: mDetectedCharset := UTF32_LE_CHARSET; |
| 192 |
BOM_UCS4_BE: mDetectedCharset := UTF32_BE_CHARSET; |
| 193 |
BOM_UCS4_2143: mDetectedCharset := UCS4_LE_CHARSET; |
| 194 |
BOM_UCS4_3412: mDetectedCharset := UCS4_BE_CHARSET |
| 195 |
end; |
| 196 |
mDone := TRUE; |
| 197 |
Result := NS_OK; |
| 198 |
Exit; |
| 199 |
end; |
| 200 |
end; {if mStart} |
| 201 |
|
| 202 |
for i := 0 to Pred(aLen) do |
| 203 |
(*other than 0xa0, if every othe character is ascii, the page is ascii*) |
| 204 |
if (aBuf[i] > #$80) and (aBuf[i] <> #$A0) then |
| 205 |
begin |
| 206 |
(*Since many Ascii only page contains NBSP *) |
| 207 |
(*we got a non-ascii byte (high-byte)*) |
| 208 |
if mInputState <> isHighbyte then |
| 209 |
begin |
| 210 |
(*adjust state*) |
| 211 |
mInputState := isHighbyte; |
| 212 |
end; |
| 213 |
end |
| 214 |
else |
| 215 |
begin |
| 216 |
(*ok, just pure ascii so *) |
| 217 |
if (mInputState = isPureAscii) and |
| 218 |
((aBuf[i] = #$1B) or |
| 219 |
(aBuf[i] = '{') and |
| 220 |
(mLastChar = '~')) then |
| 221 |
(*found escape character or HZ "~{"*) |
| 222 |
mInputState := isEscAscii; |
| 223 |
|
| 224 |
mLastChar := aBuf[i]; |
| 225 |
end; |
| 226 |
|
| 227 |
case mInputState of |
| 228 |
isEscAscii: |
| 229 |
begin |
| 230 |
{$IFDEF DEBUG_chardet} |
| 231 |
AddDump('Universal detector - Escape Detector started'); |
| 232 |
{$ENDIF} |
| 233 |
st := mEscCharSetProber.HandleData(aBuf, aLen); |
| 234 |
if st = psFoundIt then |
| 235 |
begin |
| 236 |
mDone := TRUE; |
| 237 |
mDetectedCharset := mEscCharSetProber.GetDetectedCharset; |
| 238 |
end; |
| 239 |
end; |
| 240 |
isHighbyte: |
| 241 |
begin |
| 242 |
{$IFDEF DEBUG_chardet} |
| 243 |
AddDump('Universal detector - HighByte Detector started'); |
| 244 |
{$ENDIF} |
| 245 |
for i := 0 to Pred(NUM_OF_CHARSET_PROBERS) do |
| 246 |
begin |
| 247 |
st := mCharSetProbers[i].HandleData(aBuf, aLen); |
| 248 |
if st = psFoundIt then |
| 249 |
begin |
| 250 |
mDone := TRUE; |
| 251 |
mDetectedCharset := mCharSetProbers[i].GetDetectedCharset; |
| 252 |
break; |
| 253 |
end; |
| 254 |
end; |
| 255 |
end; |
| 256 |
else |
| 257 |
(*pure ascii*) |
| 258 |
begin |
| 259 |
(*do nothing here*) |
| 260 |
end; |
| 261 |
end; {case} |
| 262 |
Result := NS_OK; |
| 263 |
end; |
| 264 |
|
| 265 |
procedure TnsUniversalDetector.Report(aCharsetID: eInternalCharsetID); |
| 266 |
begin |
| 267 |
|
| 268 |
if (aCharsetID <> UNKNOWN_CHARSET) and |
| 269 |
(mDetectedCharset = UNKNOWN_CHARSET) then |
| 270 |
|
| 271 |
mDetectedCharset := aCharsetID; |
| 272 |
end; |
| 273 |
|
| 274 |
procedure TnsUniversalDetector.Reset; |
| 275 |
var |
| 276 |
i: integer; |
| 277 |
begin |
| 278 |
mDone := FALSE; |
| 279 |
mStart := TRUE; |
| 280 |
mDetectedCharset := UNKNOWN_CHARSET; |
| 281 |
mGotData := FALSE; |
| 282 |
mInputState := isPureAscii; |
| 283 |
mLastChar := #0; (*illegal value as signal*) |
| 284 |
mEscCharSetProber.Reset; |
| 285 |
for i := 0 to Pred(NUM_OF_CHARSET_PROBERS) do |
| 286 |
mCharSetProbers[i].Reset; |
| 287 |
mDetectedBOM := BOM_Not_Found; |
| 288 |
end; |
| 289 |
|
| 290 |
function TnsUniversalDetector.GetDetectedCharsetInfo: nsCore.rCharsetInfo; |
| 291 |
begin |
| 292 |
Result := KNOWN_CHARSETS[mDetectedCharset]; |
| 293 |
end; |
| 294 |
|
| 295 |
function TnsUniversalDetector.GetKnownCharset(out KnownCharsets: String): integer; |
| 296 |
var |
| 297 |
i: eInternalCharsetID; |
| 298 |
begin |
| 299 |
KnownCharsets := ''; |
| 300 |
for i := low(KNOWN_CHARSETS) to high(KNOWN_CHARSETS) do |
| 301 |
KnownCharsets := KnownCharsets + #10 + KNOWN_CHARSETS[i].Name + |
| 302 |
' - ' + IntToStr(KNOWN_CHARSETS[i].CodePage); |
| 303 |
|
| 304 |
Result := Length(KnownCharsets); |
| 305 |
end; |
| 306 |
|
| 307 |
procedure TnsUniversalDetector.GetAbout(out About: rAboutHolder); |
| 308 |
begin |
| 309 |
About := AboutInfo; |
| 310 |
end; |
| 311 |
|
| 312 |
function TnsUniversalDetector.CheckBOM(aBuf: pAnsiChar; aLen: integer): integer; |
| 313 |
var |
| 314 |
bom: eBOMKind; |
| 315 |
i: integer; |
| 316 |
same: Boolean; |
| 317 |
begin |
| 318 |
Result := 0; |
| 319 |
mDetectedBOM := BOM_Not_Found; |
| 320 |
for bom := Succ(low(eBOMKind)) to high(eBomKind) do |
| 321 |
if aLen > KNOWN_BOM[bom].Length then |
| 322 |
begin |
| 323 |
same := true; |
| 324 |
for i := 0 to KNOWN_BOM[bom].Length - 1 do |
| 325 |
if (aBuf[i] <> KNOWN_BOM[bom].BOM[i]) then |
| 326 |
begin |
| 327 |
same := false; |
| 328 |
break; |
| 329 |
end; |
| 330 |
if same then |
| 331 |
begin |
| 332 |
mDetectedBOM := bom; |
| 333 |
Result := KNOWN_BOM[bom].Length; |
| 334 |
exit; |
| 335 |
end; |
| 336 |
end; |
| 337 |
end; |
| 338 |
|
| 339 |
procedure TnsUniversalDetector.DisableCharset(CodePage: integer); |
| 340 |
begin |
| 341 |
DoEnableCharset(GetCharsetID(CodePage), false); |
| 342 |
end; |
| 343 |
|
| 344 |
function TnsUniversalDetector.GetCharsetID(CodePage: integer): eInternalCharsetID; |
| 345 |
var |
| 346 |
i: integer; |
| 347 |
begin |
| 348 |
for i := integer(low(KNOWN_CHARSETS)) + 1 to integer(high(KNOWN_CHARSETS)) do |
| 349 |
if (KNOWN_CHARSETS[eInternalCharsetID(i)].CodePage = CodePage) then |
| 350 |
begin |
| 351 |
Result := eInternalCharsetID(i); |
| 352 |
exit; |
| 353 |
end; |
| 354 |
Result := UNKNOWN_CHARSET; |
| 355 |
end; |
| 356 |
|
| 357 |
procedure TnsUniversalDetector.DoEnableCharset(Charset: eInternalCharsetID; SetEnabledTo: Boolean); |
| 358 |
var |
| 359 |
i: integer; |
| 360 |
begin |
| 361 |
if Charset = UNKNOWN_CHARSET then |
| 362 |
exit; |
| 363 |
for i := 0 to Pred(NUM_OF_CHARSET_PROBERS) do |
| 364 |
begin |
| 365 |
if (mCharSetProbers[i] is TnsGroupProber) then |
| 366 |
begin |
| 367 |
if TnsGroupProber(mCharSetProbers[i]).EnableCharset(Charset, SetEnabledTo) then |
| 368 |
exit; |
| 369 |
end; |
| 370 |
if (mCharSetProbers[i] is TnsEscCharSetProber) then |
| 371 |
begin |
| 372 |
TnsEscCharSetProber(mCharSetProbers[i]).Enabled := SetEnabledTo; |
| 373 |
end; |
| 374 |
if (mCharSetProbers[i] is TCustomDetector) then |
| 375 |
begin |
| 376 |
if TCustomDetector(mCharSetProbers[i]).GetDetectedCharset = Charset then |
| 377 |
TCustomDetector(mCharSetProbers[i]).Enabled := SetEnabledTo; |
| 378 |
end; |
| 379 |
end; |
| 380 |
|
| 381 |
end; |
| 382 |
|
| 383 |
end. |
| 384 |
|